Skip to content

Commit dae70e0

Browse files
committed
Added test cases and refactored some tests that create JSONLanguageService instances
1 parent feb66fd commit dae70e0

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

.changeset/lazy-seas-collect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme-language-server-common': minor
3+
---
4+
5+
Added document linking inside schema tags, which enhances developer experience

packages/theme-language-server-common/src/json/JSONLanguageService.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ describe('Module: JSONLanguageService', () => {
120120
(uri: string) => Promise.resolve(uri.includes('tae') ? 'app' : 'theme'),
121121
() => Promise.resolve([]),
122122
async () => undefined,
123+
async () => 'file:///test-root',
123124
);
124125

125126
await jsonLanguageService.setup({

packages/theme-language-server-common/src/json/completions/providers/BlockTypeCompletionProvider.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('Unit: BlockTypeCompletionProvider', () => {
6464
async () => 'theme',
6565
async () => blockNames,
6666
async () => undefined,
67+
async () => 'file:///test-root',
6768
);
6869

6970
await jsonLanguageService.setup({
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { DocumentManager } from '../../documents';
3+
import { createJSONDocumentLinksVisitor } from './DocumentLinksProvider';
4+
import { toJSONAST, visit } from '@shopify/theme-check-common';
5+
import { URI } from 'vscode-uri';
6+
7+
describe('JSON Document Links in Schema Tags', () => {
8+
let documentManager: DocumentManager;
9+
let rootUri: string;
10+
let uriString: string;
11+
12+
beforeEach(() => {
13+
documentManager = new DocumentManager();
14+
});
15+
16+
it('should return document links for block types in schema tags', async () => {
17+
uriString = 'file:///path/to/sections/header.liquid';
18+
rootUri = 'file:///path/to/project';
19+
20+
const schemaContent = `
21+
{
22+
"name": "Header",
23+
"blocks": [
24+
{ "type": "logo" },
25+
{ "type": "navigation" }
26+
]
27+
}
28+
`;
29+
30+
const liquidContent = `
31+
<div>Header content</div>
32+
33+
{% schema %}
34+
${schemaContent}
35+
{% endschema %}
36+
`;
37+
38+
documentManager.open(uriString, liquidContent, 1);
39+
40+
const textDocument = documentManager.get(uriString)!.textDocument;
41+
const schemaOffset = liquidContent.indexOf(schemaContent);
42+
43+
const visitor = createJSONDocumentLinksVisitor(textDocument, URI.parse(rootUri), schemaOffset);
44+
const ast = toJSONAST(schemaContent);
45+
46+
if (ast instanceof Error) throw ast;
47+
const result = visit(ast, visitor);
48+
49+
expect(result).toHaveLength(2);
50+
expect(result[0].target).toBe('file:///path/to/project/blocks/logo.liquid');
51+
expect(result[1].target).toBe('file:///path/to/project/blocks/navigation.liquid');
52+
});
53+
54+
it('should handle section types in schema tags', async () => {
55+
uriString = 'file:///path/to/sections/main-collection.liquid';
56+
rootUri = 'file:///path/to/project';
57+
58+
const schemaContent = `
59+
{
60+
"name": "Collection",
61+
"blocks": [
62+
{
63+
"type": "text",
64+
"settings": []
65+
}
66+
],
67+
"presets": [
68+
{
69+
"name": "Default",
70+
"blocks": [
71+
{ "type": "text" }
72+
]
73+
}
74+
]
75+
}
76+
`;
77+
78+
const liquidContent = `
79+
<div>Collection content</div>
80+
81+
{% schema %}
82+
${schemaContent}
83+
{% endschema %}
84+
`;
85+
86+
documentManager.open(uriString, liquidContent, 1);
87+
88+
const textDocument = documentManager.get(uriString)!.textDocument;
89+
const schemaOffset = liquidContent.indexOf(schemaContent);
90+
91+
const visitor = createJSONDocumentLinksVisitor(textDocument, URI.parse(rootUri), schemaOffset);
92+
const ast = toJSONAST(schemaContent);
93+
94+
if (ast instanceof Error) throw ast;
95+
const result = visit(ast, visitor);
96+
97+
// Should find both block type references (in blocks array and in presets)
98+
expect(result).toHaveLength(2);
99+
expect(result[0].target).toBe('file:///path/to/project/blocks/text.liquid');
100+
expect(result[1].target).toBe('file:///path/to/project/blocks/text.liquid');
101+
});
102+
103+
it('should not create links for special block types in schema tags', async () => {
104+
uriString = 'file:///path/to/sections/app-blocks.liquid';
105+
rootUri = 'file:///path/to/project';
106+
107+
const schemaContent = `
108+
{
109+
"name": "App Blocks",
110+
"blocks": [
111+
{ "type": "@app" },
112+
{ "type": "@theme" },
113+
{ "type": "custom-block" }
114+
]
115+
}
116+
`;
117+
118+
const liquidContent = `
119+
<div>App blocks section</div>
120+
121+
{% schema %}
122+
${schemaContent}
123+
{% endschema %}
124+
`;
125+
126+
documentManager.open(uriString, liquidContent, 1);
127+
128+
const textDocument = documentManager.get(uriString)!.textDocument;
129+
const schemaOffset = liquidContent.indexOf(schemaContent);
130+
131+
const visitor = createJSONDocumentLinksVisitor(textDocument, URI.parse(rootUri), schemaOffset);
132+
const ast = toJSONAST(schemaContent);
133+
134+
if (ast instanceof Error) throw ast;
135+
const result = visit(ast, visitor);
136+
137+
// Should only create link for custom-block, not @app or @theme
138+
expect(result).toHaveLength(1);
139+
expect(result[0].target).toBe('file:///path/to/project/blocks/custom-block.liquid');
140+
});
141+
});

0 commit comments

Comments
 (0)