Skip to content

Commit 64c6298

Browse files
authored
feat: add support for allowTrailingCommas option in JSON (#96)
1 parent 218d5a5 commit 64c6298

File tree

6 files changed

+83
-76
lines changed

6 files changed

+83
-76
lines changed

package-lock.json

Lines changed: 34 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@codemirror/language": "^6.10.3",
4242
"@eslint/css": "^0.2.0",
4343
"@eslint/js": "^9.9.0",
44-
"@eslint/json": "^0.4.1",
44+
"@eslint/json": "^0.12.0",
4545
"@eslint/markdown": "^6.4.0",
4646
"@lezer/highlight": "^1.2.1",
4747
"@radix-ui/react-accordion": "^1.2.0",

src/components/options.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,39 @@ import type {
3333
const JSONPanel: React.FC = () => {
3434
const explorer = useExplorer();
3535
const { jsonOptions, setJsonOptions } = explorer;
36-
const { jsonMode } = jsonOptions;
36+
const { jsonMode, allowTrailingCommas } = jsonOptions;
3737
return (
38-
<LabeledSelect
39-
id="jsonMode"
40-
label="Mode"
41-
value={jsonMode}
42-
onValueChange={(value: string) => {
43-
const jsonMode = value as JsonMode;
44-
setJsonOptions({ ...jsonOptions, jsonMode });
45-
}}
46-
items={jsonModes}
47-
placeholder="Mode"
48-
/>
38+
<>
39+
<LabeledSelect
40+
id="jsonMode"
41+
label="Mode"
42+
value={jsonMode}
43+
onValueChange={(value: string) => {
44+
const jsonMode = value as JsonMode;
45+
setJsonOptions({ ...jsonOptions, jsonMode });
46+
}}
47+
items={jsonModes}
48+
placeholder="Mode"
49+
/>
50+
51+
{jsonMode === "jsonc" && (
52+
<div className="flex items-center gap-1.5">
53+
<Switch
54+
id="allowTrailingCommas"
55+
checked={allowTrailingCommas}
56+
onCheckedChange={(value: boolean) => {
57+
setJsonOptions({
58+
...jsonOptions,
59+
allowTrailingCommas: value,
60+
});
61+
}}
62+
/>
63+
<Label htmlFor="allowTrailingCommas">
64+
Allow Trailing Commas
65+
</Label>
66+
</div>
67+
)}
68+
</>
4969
);
5070
};
5171

src/hooks/use-ast.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,21 @@ export function useAST() {
4141
}
4242

4343
case "json": {
44-
const { jsonMode } = jsonOptions;
44+
const { jsonMode, allowTrailingCommas } = jsonOptions;
4545
const language = json.languages[jsonMode];
46-
astParseResult = language.parse({
47-
body: code.json,
48-
path: "",
49-
physicalPath: "",
50-
bom: false,
51-
});
46+
astParseResult = language.parse(
47+
{
48+
body: code.json,
49+
path: "",
50+
physicalPath: "",
51+
bom: false,
52+
},
53+
{
54+
languageOptions: {
55+
allowTrailingCommas,
56+
},
57+
},
58+
);
5259
break;
5360
}
5461

src/hooks/use-explorer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type JsOptions = {
3838

3939
export type JsonOptions = {
4040
jsonMode: JsonMode;
41+
allowTrailingCommas: boolean;
4142
};
4243

4344
export type MarkdownOptions = {

src/lib/const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ export const defaultJsOptions: JsOptions = {
392392

393393
export const defaultJsonOptions: JsonOptions = {
394394
jsonMode: "jsonc",
395+
allowTrailingCommas: false,
395396
};
396397

397398
export const defaultMarkdownOptions: MarkdownOptions = {

0 commit comments

Comments
 (0)