Skip to content

Commit 6ad37f6

Browse files
committed
[LiquidDoc] Return undefined for invalid snippet definitions
1 parent 681ba72 commit 6ad37f6

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

packages/theme-language-server-common/src/documents/DocumentManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class DocumentManager {
175175
}),
176176
/** Lazy and only computed once per file version */
177177
getLiquidDoc: memo(async (snippetName: string) => {
178-
if (isError(sourceCode.ast)) return { name: snippetName };
178+
if (isError(sourceCode.ast)) return undefined;
179179

180180
return getSnippetDefinition(sourceCode.ast, snippetName);
181181
}),

packages/theme-language-server-common/src/documents/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type AugmentedJsonSourceCode = _AugmentedSourceCode<SourceCodeType.JSON>;
2424
*/
2525
export type AugmentedLiquidSourceCode = _AugmentedSourceCode<SourceCodeType.LiquidHtml> & {
2626
getSchema: () => Promise<SectionSchema | ThemeBlockSchema | AppBlockSchema | undefined>;
27-
getLiquidDoc: (snippetName: string) => Promise<SnippetDefinition>;
27+
getLiquidDoc: (snippetName: string) => Promise<SnippetDefinition | undefined>;
2828
};
2929

3030
/**

packages/theme-language-server-common/src/hover/providers/RenderSnippetHoverProvider.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
99
private getSnippetDefinitionForURI: (
1010
uri: string,
1111
snippetName: string,
12-
) => Promise<SnippetDefinition>,
12+
) => Promise<SnippetDefinition | undefined>,
1313
) {}
1414

1515
async hover(
@@ -32,6 +32,10 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
3232
snippetName,
3333
);
3434

35+
if (!snippetDefinition) {
36+
return null;
37+
}
38+
3539
const liquidDoc = snippetDefinition.liquidDoc;
3640

3741
if (!liquidDoc) {

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ import { LiquidDocParamNode } from '@shopify/liquid-html-parser';
77
export type GetSnippetDefinitionForURI = (
88
uri: string,
99
snippetName: string,
10-
) => Promise<SnippetDefinition>;
11-
12-
export type LiquidDocParameter = {
13-
name: string;
14-
description: string | null;
15-
type: string | null;
16-
};
10+
) => Promise<SnippetDefinition | undefined>;
1711

1812
export type SnippetDefinition = {
1913
name: string;
@@ -24,27 +18,33 @@ type LiquidDocDefinition = {
2418
parameters?: LiquidDocParameter[];
2519
};
2620

21+
export type LiquidDocParameter = {
22+
name: string;
23+
description: string | null;
24+
type: string | null;
25+
};
26+
2727
export function getSnippetDefinition(
2828
snippet: LiquidHtmlNode,
2929
snippetName: string,
3030
): SnippetDefinition {
31-
const liquidDocParameters: LiquidDocParameter[] = visit<
32-
SourceCodeType.LiquidHtml,
33-
LiquidDocParameter
34-
>(snippet, {
35-
LiquidDocParamNode(node: LiquidDocParamNode) {
36-
return {
37-
name: node.paramName.value,
38-
description: node.paramDescription?.value ?? null,
39-
type: node.paramType?.value ?? null,
40-
};
31+
const parameters: LiquidDocParameter[] = visit<SourceCodeType.LiquidHtml, LiquidDocParameter>(
32+
snippet,
33+
{
34+
LiquidDocParamNode(node: LiquidDocParamNode) {
35+
return {
36+
name: node.paramName.value,
37+
description: node.paramDescription?.value ?? null,
38+
type: node.paramType?.value ?? null,
39+
};
40+
},
4141
},
42-
});
42+
);
4343

4444
return {
4545
name: snippetName,
4646
liquidDoc: {
47-
parameters: liquidDocParameters,
47+
parameters: parameters,
4848
},
4949
};
5050
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ import { snippetName } from '../utils/uri';
4343
import { VERSION } from '../version';
4444
import { CachedFileSystem } from './CachedFileSystem';
4545
import { Configuration } from './Configuration';
46-
import { LiquidHtmlNode } from '@shopify/liquid-html-parser';
47-
import { getSnippetDefinition, SnippetDefinition } from '../liquidDoc';
46+
import { SnippetDefinition } from '../liquidDoc';
4847

4948
const defaultLogger = () => {};
5049

@@ -176,13 +175,13 @@ export function startServer(
176175
const getSnippetDefinitionForURI = async (
177176
uri: string,
178177
snippetName: string,
179-
): Promise<SnippetDefinition> => {
178+
): Promise<SnippetDefinition | undefined> => {
180179
const rootUri = await findThemeRootURI(uri);
181180
const snippetURI = path.join(rootUri, 'snippets', `${snippetName}.liquid`);
182181
const snippet = documentManager.get(snippetURI);
183182

184183
if (!snippet || snippet.type !== SourceCodeType.LiquidHtml || isError(snippet.ast)) {
185-
return { name: snippetName };
184+
return undefined;
186185
}
187186

188187
return snippet.getLiquidDoc(snippetName);

0 commit comments

Comments
 (0)