Skip to content

Commit 829655f

Browse files
committed
Wrap optional parameters name in [] in hover tooltip
----- Behaviour diverges slightly from JSDoc here. JSDoc does not indicate that it's optional, rather it implicitly adds `| undefined` to the types
1 parent 35d7e8f commit 829655f

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ describe('Module: RenderSnippetHoverProvider', async () => {
1515
name: 'title',
1616
description: 'The title of the product',
1717
type: 'string',
18+
required: true,
1819
},
1920
{
2021
name: 'border-radius',
2122
description: 'The border radius in px',
2223
type: 'number',
24+
required: false,
2325
},
2426
],
2527
},
@@ -47,13 +49,6 @@ describe('Module: RenderSnippetHoverProvider', async () => {
4749
});
4850

4951
describe('hover', () => {
50-
it('should return snippet definition with all parameters', async () => {
51-
await expect(provider).to.hover(
52-
`{% render 'product-car█d' %}`,
53-
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `border-radius`: number - The border radius in px',
54-
);
55-
});
56-
5752
it('should return null if no LiquidDocDefinition found', async () => {
5853
getSnippetDefinition = async () => ({ name: 'unknown-snippet', liquidDoc: undefined });
5954
provider = createProvider(getSnippetDefinition);
@@ -70,5 +65,12 @@ describe('Module: RenderSnippetHoverProvider', async () => {
7065
await expect(provider).to.hover(`{% assign asdf = 'snip█pet' %}`, null);
7166
await expect(provider).to.hover(`{{ 'snip█pet' }}`, null);
7267
});
68+
69+
it('should return snippet definition with all parameters', async () => {
70+
await expect(provider).to.hover(
71+
`{% render 'product-car█d' %}`,
72+
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `[border-radius]`: number - The border radius in px',
73+
);
74+
});
7375
});
7476
});

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
5050
const parts = [`### ${snippetDefinition.name}`];
5151

5252
if (liquidDoc.parameters?.length) {
53-
const parameters = liquidDoc.parameters
54-
?.map(
55-
({ name, type, description }: LiquidDocParameter) =>
56-
`- \`${name}\`${type ? `: ${type}` : ''} ${description ? `- ${description}` : ''}`,
57-
)
58-
.join('\n');
59-
53+
const parameters = this.buildParameters(liquidDoc.parameters);
6054
parts.push('', '**Parameters:**', parameters);
6155
}
6256

@@ -67,4 +61,15 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
6761
},
6862
};
6963
}
64+
65+
private buildParameters(parameters: LiquidDocParameter[]) {
66+
return parameters
67+
.map(({ name, type, description, required }: LiquidDocParameter) => {
68+
const nameStr = required ? `\`${name}\`` : `\`[${name}]\``;
69+
const typeStr = type ? `: ${type}` : '';
70+
const descStr = description ? ` - ${description}` : '';
71+
return `- ${nameStr}${typeStr}${descStr}`;
72+
})
73+
.join('\n');
74+
}
7075
}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { toSourceCode } from '@shopify/theme-check-common';
44
import { describe } from 'vitest';
55
import { getSnippetDefinition } from './liquidDoc';
66

7-
describe('Unit: makeGetLiquidDocDefinitions', () => {
7+
describe('Unit: getSnippetDefinition', () => {
88
function toAST(code: string) {
99
return toSourceCode('/tmp/foo.liquid', code).ast as LiquidHtmlNode;
1010
}
@@ -31,6 +31,7 @@ describe('Unit: makeGetLiquidDocDefinitions', () => {
3131
{% doc %}
3232
@param {String} firstParam - The first param
3333
@param {Number} secondParam - The second param
34+
@param {String} [optionalParam] - The optional param
3435
@param paramWithNoType - param with no type
3536
@param paramWithOnlyName
3637
{% enddoc %}
@@ -45,21 +46,31 @@ describe('Unit: makeGetLiquidDocDefinitions', () => {
4546
name: 'firstParam',
4647
description: 'The first param',
4748
type: 'String',
49+
required: true,
4850
},
4951
{
5052
name: 'secondParam',
5153
description: 'The second param',
5254
type: 'Number',
55+
required: true,
56+
},
57+
{
58+
name: 'optionalParam',
59+
description: 'The optional param',
60+
type: 'String',
61+
required: false,
5362
},
5463
{
5564
name: 'paramWithNoType',
5665
description: 'param with no type',
5766
type: null,
67+
required: true,
5868
},
5969
{
6070
name: 'paramWithOnlyName',
6171
description: '',
6272
type: null,
73+
required: true,
6374
},
6475
],
6576
},

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

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type LiquidDocParameter = {
2222
name: string;
2323
description: string | null;
2424
type: string | null;
25+
required: boolean;
2526
};
2627

2728
export function getSnippetDefinition(
@@ -36,6 +37,7 @@ export function getSnippetDefinition(
3637
name: node.paramName.value,
3738
description: node.paramDescription?.value ?? null,
3839
type: node.paramType?.value ?? null,
40+
required: node.required,
3941
};
4042
},
4143
},

0 commit comments

Comments
 (0)