Skip to content

Commit b9f44e4

Browse files
authored
fix: don't allow trailing commas in JSON mode (#604)
1 parent d89eec4 commit b9f44e4

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎e2e-tests/code-editing-and-ast-interaction.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,34 @@ test.describe("AST node expansion", () => {
593593
page.getByRole("button", { name: "6. RBrace" }),
594594
).toBeVisible();
595595
});
596+
597+
test("Allow Trailing Commas: true, then Mode: JSON", async ({
598+
page,
599+
}) => {
600+
// `Mode`: `JSONC`
601+
await setComboboxOption(page, "Mode", "jsonc");
602+
603+
// `Allow Trailing Commas`: `true`
604+
await setSwitchOption(page, "Allow Trailing Commas", true);
605+
606+
// `Mode`: `JSON`, which hides the `Allow Trailing Commas` switch.
607+
await setComboboxOption(page, "Mode", "json");
608+
609+
// Hide the settings menu to ensure it doesn't interfere with the test.
610+
await page.keyboard.press("Escape");
611+
612+
// Fill a JSON sample with a trailing comma into the editor.
613+
await page
614+
.getByRole("textbox", { exact: true, name: "Code Editor" })
615+
.fill('{"foo":"bar",}');
616+
617+
// Verify that the trailing comma is still a parse error in JSON mode.
618+
await expect(
619+
page.getByText("Error: Unexpected token RBrace found. (1:14)", {
620+
exact: true,
621+
}),
622+
).toBeVisible();
623+
});
596624
});
597625

598626
test.describe("Language: Markdown", () => {

‎src/hooks/use-ast.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ export function useAST() {
5757
bom: false,
5858
},
5959
{
60-
languageOptions: {
61-
allowTrailingCommas,
62-
},
60+
languageOptions:
61+
jsonMode === "jsonc" ? { allowTrailingCommas } : {},
6362
},
6463
);
6564
break;

0 commit comments

Comments
 (0)