Skip to content

Commit 3b20751

Browse files
committed
Extract makeGetLiquidDocDefinition into theme-check-common
1 parent 8eadb24 commit 3b20751

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

packages/theme-check-common/src/context-utils.ts

+28
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import {
88
SourceCodeType,
99
Theme,
1010
Translations,
11+
LiquidDocDefinition,
12+
LiquidDocParameter,
1113
} from './types';
1214
import { isError } from './utils';
15+
import { visit } from './visitor';
16+
import { LiquidDocParamNode, LiquidHtmlNode } from '@shopify/liquid-html-parser';
1317

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

@@ -211,3 +215,27 @@ export const makeGetMetafieldDefinitions = (fs: AbstractFileSystem) =>
211215
return definitions;
212216
}
213217
};
218+
219+
export function makeGetLiquidDocDefinitions(ast: LiquidHtmlNode, snippetName: string) {
220+
return async function getLiquidDocDefinitions(): Promise<LiquidDocDefinition> {
221+
if (isError(ast)) {
222+
return { name: snippetName, parameters: [] };
223+
}
224+
225+
const liquidDocAnnotations: LiquidDocParameter[] = [];
226+
visit(ast, {
227+
LiquidDocParamNode(node: LiquidDocParamNode) {
228+
liquidDocAnnotations.push({
229+
name: node.paramName.value,
230+
description: node.paramDescription?.value || undefined,
231+
type: node.paramType?.value || undefined,
232+
});
233+
},
234+
});
235+
236+
return {
237+
name: snippetName,
238+
parameters: liquidDocAnnotations,
239+
};
240+
};
241+
}

packages/theme-language-server-common/src/server/startServer.ts

+6-29
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import {
44
findRoot as findConfigFileRoot,
55
isError,
66
LiquidDocDefinition,
7-
LiquidDocParameter,
87
makeFileExists,
98
makeGetDefaultSchemaTranslations,
109
makeGetDefaultTranslations,
1110
makeGetMetafieldDefinitions,
11+
makeGetLiquidDocDefinitions,
1212
memoize,
1313
parseJSON,
1414
path,
1515
recursiveReadDirectory,
1616
SourceCodeType,
17-
visit,
1817
} from '@shopify/theme-check-common';
1918
import {
2019
Connection,
@@ -174,7 +173,6 @@ export function startServer(
174173
return getDefaultSchemaTranslations();
175174
};
176175

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

186-
if (!snippet || isError(snippet)) return { name: snippetName };
187-
188-
if (snippet.type !== SourceCodeType.LiquidHtml) return { name: snippetName };
189-
190-
const ast = snippet.ast;
191-
if (isError(ast)) return { name: snippetName };
192-
193-
const liquidDocAnnotations = visit<SourceCodeType.LiquidHtml, LiquidDocParameter>(ast, {
194-
LiquidDocParamNode: (node) => {
195-
return {
196-
name: node.paramName.value,
197-
description: node.paramDescription?.value,
198-
type: node.type,
199-
};
200-
},
201-
});
184+
if (!snippet || snippet.type !== SourceCodeType.LiquidHtml || isError(snippet.ast)) {
185+
return { name: snippetName };
186+
}
202187

203-
const parameters: LiquidDocParameter[] = [];
204-
liquidDocAnnotations.forEach((annotation) => {
205-
if (annotation.type === 'LiquidDocParamNode') {
206-
parameters.push(annotation);
207-
}
208-
});
188+
const getLiquidDocDefinitions = makeGetLiquidDocDefinitions(snippet.ast, snippetName);
209189

210-
return {
211-
name: snippetName,
212-
parameters: parameters,
213-
};
190+
return getLiquidDocDefinitions();
214191
};
215192

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

0 commit comments

Comments
 (0)