Skip to content

Commit

Permalink
Add AST support of JSONC (for theme check, language features, etc.)
Browse files Browse the repository at this point in the history
- Rip out `json-to-ast`
- Inline the `json-to-ast` types in `theme-check-common/src/jsonc`
- Make a little `jsonc-parser#Node` -> `json-to-ast#ASTNode` adapter

Fixes #654
  • Loading branch information
charlespwd committed Dec 6, 2024
1 parent 606304f commit 57db721
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-chairs-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-check-common': minor
---

Add JSONC support to the AST parser
25 changes: 25 additions & 0 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The theme-language-server project incorporates third party material from the pro
3. @lezer/json (https://github.com/lezer-parser/json)
4. @codemirror/lang-json (https://github.com/codemirror/lang-json)
5. monaco-editor (https://github.com/microsoft/monaco-editor)
6. json-to-ast (https://github.com/vtrushin/json-to-ast)

%% vscode-languageserver-node NOTICES, INFORMATION, AND LICENSE BEGIN HERE
=========================================
Expand Down Expand Up @@ -133,3 +134,27 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=========================================
END OF monaco-editor NOTICES, INFORMATION, AND LICENSE

%% json-to-ast NOTICES, INFORMATION, AND LICENSE BEGIN HERE
=========================================
Copyright (C) 2016 by Vlad Trushin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=========================================
END OF json-to-ast NOTICES, INFORMATION, AND LICENSE
2 changes: 0 additions & 2 deletions packages/theme-check-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"dependencies": {
"@shopify/liquid-html-parser": "2.1.2",
"cross-fetch": "^4.0.0",
"json-to-ast": "^2.1.0",
"jsonc-parser": "^3.2.0",
"line-column": "^1.0.2",
"lodash-es": "^4.17.21",
Expand All @@ -37,7 +36,6 @@
"vscode-uri": "^3.0.7"
},
"devDependencies": {
"@types/json-to-ast": "^2.1.2",
"@types/line-column": "^1.0.0",
"@types/lodash-es": "^4.17.12"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { JSONNode, LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';
import { toJSONAST } from '../../to-source-code';
import {
JSONNode,
LiquidCheckDefinition,
LiteralNode,
Severity,
SourceCodeType,
} from '../../types';
import { visit } from '../../visitor';
import { LiteralNode } from 'json-to-ast';

export const LiquidFreeSettings: LiquidCheckDefinition = {
meta: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PropertyNode } from 'json-to-ast';
import {
JSONCheckDefinition,
JSONNode,
JSONSourceCode,
Severity,
SourceCodeType,
PropertyNode,
} from '../../types';

const PLURALIZATION_KEYS = new Set(['zero', 'one', 'two', 'few', 'many', 'other']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { ArrayNode } from 'json-to-ast';
import { getLocEnd, getLocStart, nodeAtPath } from '../../json';
import { basename } from '../../path';
import { isBlock, isSection } from '../../to-schema';
import { JSONNode, LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';
import {
ArrayNode,
Context,
JSONNode,
LiquidCheckDefinition,
Severity,
SourceCodeType,
} from '../../types';
import { Preset } from '../../types/schemas/preset';
import { ThemeBlock } from '../../types/schemas/theme-block';
import { Context } from '../../types';

export const SchemaPresetsBlockOrder: LiquidCheckDefinition = {
meta: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { JSONNode, Preset, Section, SourceCodeType, Theme, ThemeBlock } from '../../types';
import { LiteralNode } from 'json-to-ast';
import {
JSONNode,
LiteralNode,
Preset,
Section,
SourceCodeType,
Theme,
ThemeBlock,
} from '../../types';
import { getLocEnd, getLocStart, nodeAtPath } from '../../json';
import { Context } from '../../types';
import { doesFileExist } from '../../utils/file-utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
Preset,
Section,
ThemeBlock,
LiteralNode,
} from '../../types';
import { LiteralNode } from 'json-to-ast';
import { nodeAtPath } from '../../json';
import { getSchema } from '../../to-schema';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SourceCodeType, JSONCheckDefinition, Severity, Problem, JSONNode } from '../../types';
import { SourceCodeType, JSONCheckDefinition, Severity, Problem, LiteralNode } from '../../types';
import { toLiquidHtmlAST } from '@shopify/liquid-html-parser';
import { Location, LiteralNode } from 'json-to-ast';

export const ValidHTMLTranslation: JSONCheckDefinition = {
meta: {
Expand Down Expand Up @@ -31,7 +30,7 @@ export const ValidHTMLTranslation: JSONCheckDefinition = {
try {
toLiquidHtmlAST(node.value);
} catch (error) {
const loc = node.loc as Location;
const loc = node.loc;

const problem: Problem<SourceCodeType.JSON> = {
message: `${error}.`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { LiquidCheckDefinition, Preset, Severity, SourceCodeType, Section } from '../../types';
import { LiteralNode } from 'json-to-ast';
import {
LiquidCheckDefinition,
Preset,
Severity,
SourceCodeType,
Section,
LiteralNode,
} from '../../types';
import { nodeAtPath } from '../../json';
import { getSchema } from '../../to-schema';
import { isBlock, isSection } from '../../to-schema';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LiteralNode } from 'json-to-ast';
import { getLocEnd, getLocStart } from '../../json';
import { Preset, ThemeBlock, Section, Context, SourceCodeType } from '../../types';
import { Preset, ThemeBlock, Section, Context, SourceCodeType, LiteralNode } from '../../types';

type BlockNodeWithPath = {
node: Section.Block | ThemeBlock.Block | Preset.Block;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LiteralNode } from 'json-to-ast';
import { getLocEnd, getLocStart, nodeAtPath } from '../../json';
import { getSchema } from '../../to-schema';
import { LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';
import { LiquidCheckDefinition, LiteralNode, Severity, SourceCodeType } from '../../types';
import { deepGet } from '../../utils';

const MAX_SCHEMA_NAME_LENGTH = 25;
Expand Down
118 changes: 118 additions & 0 deletions packages/theme-check-common/src/jsonc/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { describe, expect, it } from 'vitest';
import { toJSONNode, location } from './parse';

describe('module: toJSONNode', () => {
it('should parse basic key-value pairs', () => {
// 0123456789012345
const ast = toJSONNode(`{"key": "value"}`);
expect(ast).toEqual({
type: 'Object',
loc: location(0, 16),
children: [
{
type: 'Property',
loc: location(1, 15),
key: {
type: 'Identifier',
value: 'key',
raw: '"key"',
loc: location(1, 6),
},
value: {
type: 'Literal',
value: 'value',
raw: '"value"',
loc: location(8, 15),
},
},
],
});
});

it('should parse basic array of literals', () => {
// 0123456789012345678901234
const ast = toJSONNode(`["one", true, null, 10]`);
expect(ast).toEqual({
type: 'Array',
loc: location(0, 23),
children: [
{
type: 'Literal',
value: 'one',
raw: '"one"',
loc: location(1, 6),
},
{
type: 'Literal',
value: true,
raw: 'true',
loc: location(8, 12),
},
{
type: 'Literal',
value: null,
raw: 'null',
loc: location(14, 18),
},
{
type: 'Literal',
value: 10,
raw: '10',
loc: location(20, 22),
},
],
});
});

it('should not break on block comments', () => {
// 01234567890123456789012
const ast = toJSONNode(`/** block comment */10`);
expect(ast).toEqual({
type: 'Literal',
value: 10,
raw: '10',
loc: location(20, 22),
});
});

it('should not break on line comments', () => {
const source = `// line comment
10`;
const ast = toJSONNode(source);
expect(ast).toEqual({
type: 'Literal',
value: 10,
raw: '10',
loc: location(source.indexOf('10'), source.indexOf('10') + 2),
});
});

it('should not break on trailing commas', () => {
const source = `{
"key": "value",
}`;
const ast = toJSONNode(source);
expect(ast).toEqual({
type: 'Object',
loc: location(0, source.length),
children: [
{
type: 'Property',
loc: location(source.indexOf('"key"'), source.indexOf('"value"') + '"value"'.length),
key: {
type: 'Identifier',
value: 'key',
raw: '"key"',
loc: expect.anything(),
},
value: {
type: 'Literal',
value: 'value',
raw: '"value"',
loc: expect.anything(),
},
},
],
});
});
});
Loading

0 comments on commit 57db721

Please sign in to comment.