Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AST support of JSONC (for theme check, language features, etc.) #656

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

charlespwd
Copy link
Contributor

In this PR

  • 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

What did you learn?

  • Theme Check did not lint JSONC
  • AugmentedJSONSourceCode had a Error whenever there was a comment in the JSON file

Before you deploy

  • I included a minor bump changeset

Comment on lines +2 to +47
export type JSONNodeTypes = 'Object' | 'Array' | 'Property' | 'Identifier' | 'Literal';
export type JSONNode = ArrayNode | IdentifierNode | LiteralNode | ObjectNode | PropertyNode;
export type ValueNode = ObjectNode | ArrayNode | LiteralNode;

export interface Position {
offset: number;
}

export interface Location {
start: Position;
end: Position;
}

export interface ASTNode {
type: string;
// Modified from @types/json-to-ast make this not-optional
loc: Location;
}

export interface ObjectNode extends ASTNode {
type: 'Object';
children: PropertyNode[];
}

export interface PropertyNode extends ASTNode {
type: 'Property';
key: IdentifierNode;
value: ValueNode;
}

export interface IdentifierNode extends ASTNode {
type: 'Identifier';
value: string;
raw: string;
}

export interface ArrayNode extends ASTNode {
type: 'Array';
children: ValueNode[];
}

export interface LiteralNode extends ASTNode {
type: 'Literal';
value: string | number | boolean | null;
raw: string;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer those types to the ones from jsonc-parser. The ones in jsonc-parser weren't made for discriminated unions and would have made the whole visitor logic not work nice.

The ones from jsonc-parser are here

- 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
@charlespwd charlespwd force-pushed the fix/654-jsonc-to-ast branch from 7d47a8d to c371b48 Compare December 6, 2024 14:51
Copy link
Contributor

@albchu albchu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@jamesmengo jamesmengo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎩 + live review w/ Albert LGTM

@albchu albchu merged commit d01e657 into main Dec 9, 2024
6 checks passed
@albchu albchu deleted the fix/654-jsonc-to-ast branch December 9, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toSourceCode of JSON files should support JSONC
3 participants