Skip to content

Commit

Permalink
Extract makeGetLiquidDocDefinition into theme-check-common
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Jan 15, 2025
1 parent 8eadb24 commit 3b20751
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
28 changes: 28 additions & 0 deletions packages/theme-check-common/src/context-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import {
SourceCodeType,
Theme,
Translations,
LiquidDocDefinition,
LiquidDocParameter,
} from './types';
import { isError } from './utils';
import { visit } from './visitor';
import { LiquidDocParamNode, LiquidHtmlNode } from '@shopify/liquid-html-parser';

export type FileExists = (uri: string) => Promise<boolean>;

Expand Down Expand Up @@ -211,3 +215,27 @@ export const makeGetMetafieldDefinitions = (fs: AbstractFileSystem) =>
return definitions;
}
};

export function makeGetLiquidDocDefinitions(ast: LiquidHtmlNode, snippetName: string) {
return async function getLiquidDocDefinitions(): Promise<LiquidDocDefinition> {
if (isError(ast)) {
return { name: snippetName, parameters: [] };
}

const liquidDocAnnotations: LiquidDocParameter[] = [];
visit(ast, {
LiquidDocParamNode(node: LiquidDocParamNode) {
liquidDocAnnotations.push({
name: node.paramName.value,
description: node.paramDescription?.value || undefined,
type: node.paramType?.value || undefined,
});
},
});

return {
name: snippetName,
parameters: liquidDocAnnotations,
};
};
}
35 changes: 6 additions & 29 deletions packages/theme-language-server-common/src/server/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import {
findRoot as findConfigFileRoot,
isError,
LiquidDocDefinition,
LiquidDocParameter,
makeFileExists,
makeGetDefaultSchemaTranslations,
makeGetDefaultTranslations,
makeGetMetafieldDefinitions,
makeGetLiquidDocDefinitions,
memoize,
parseJSON,
path,
recursiveReadDirectory,
SourceCodeType,
visit,
} from '@shopify/theme-check-common';
import {
Connection,
Expand Down Expand Up @@ -174,7 +173,6 @@ export function startServer(
return getDefaultSchemaTranslations();
};

// todo - put this stuff in theme check common
const getLiquidDocDefinitionsForURI = async (
uri: string,
snippetName: string,
Expand All @@ -183,34 +181,13 @@ export function startServer(
const snippetURI = path.join(rootUri, 'snippets', `${snippetName}.liquid`);
const snippet = documentManager.get(snippetURI);

if (!snippet || isError(snippet)) return { name: snippetName };

if (snippet.type !== SourceCodeType.LiquidHtml) return { name: snippetName };

const ast = snippet.ast;
if (isError(ast)) return { name: snippetName };

const liquidDocAnnotations = visit<SourceCodeType.LiquidHtml, LiquidDocParameter>(ast, {
LiquidDocParamNode: (node) => {
return {
name: node.paramName.value,
description: node.paramDescription?.value,
type: node.type,
};
},
});
if (!snippet || snippet.type !== SourceCodeType.LiquidHtml || isError(snippet.ast)) {
return { name: snippetName };
}

const parameters: LiquidDocParameter[] = [];
liquidDocAnnotations.forEach((annotation) => {
if (annotation.type === 'LiquidDocParamNode') {
parameters.push(annotation);
}
});
const getLiquidDocDefinitions = makeGetLiquidDocDefinitions(snippet.ast, snippetName);

return {
name: snippetName,
parameters: parameters,
};
return getLiquidDocDefinitions();
};

const snippetFilter = ([uri]: FileTuple) => /\.liquid$/.test(uri) && /snippets/.test(uri);
Expand Down

0 comments on commit 3b20751

Please sign in to comment.