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

BREAKING(jsonc): remove allowTrailingComma option #5119

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions jsonc/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
* import { assertEquals } from "@std/assert/assert-equals";
*
* assertEquals(parse('{"foo": "bar", } // comment'), { foo: "bar" });
*
* assertEquals(parse('{"foo": "bar", } /* comment *\/'), { foo: "bar" });
*
* assertEquals(
* parse('{"foo": "bar" } // comment', { allowTrailingComma: false }),
* { foo: "bar" }
* );
* ```
*
* @module
Expand Down
36 changes: 7 additions & 29 deletions jsonc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
import type { JsonValue } from "@std/json/types";
export type { JsonValue } from "@std/json/types";

/** Options for {@linkcode parse}. */
export interface ParseOptions {
/** Allow trailing commas at the end of arrays and objects.
*
* @default {true}
*/
allowTrailingComma?: boolean;
}

/**
* Converts a JSON with Comments (JSONC) string into an object.
*
Expand All @@ -24,23 +15,18 @@ export interface ParseOptions {
* assertEquals(parse('{"foo": "bar"}'), { foo: "bar" });
* assertEquals(parse('{"foo": "bar", }'), { foo: "bar" });
* assertEquals(parse('{"foo": "bar", } /* comment *\/'), { foo: "bar" });
* assertEquals(parse('{"foo": "bar" } // comment', { allowTrailingComma: false }), { foo: "bar" });
* ```
*
* @throws {SyntaxError} If the JSONC string is invalid.
* @param text A valid JSONC string.
* @param options Options for parsing.
* @returns The parsed JsonValue from the JSONC string.
*/
export function parse(
text: string,
options?: ParseOptions,
): JsonValue {
const { allowTrailingComma = true } = { ...options };
export function parse(text: string): JsonValue {
if (new.target) {
throw new TypeError("parse is not a constructor");
}
return new JSONCParser(text, { allowTrailingComma }).parse();
return new JSONCParser(text).parse();
}

type TokenType =
Expand Down Expand Up @@ -77,12 +63,10 @@ class JSONCParser {
#text: string;
#length: number;
#tokenized: Generator<Token, void>;
#options: ParseOptions;
constructor(text: string, options: ParseOptions) {
constructor(text: string) {
this.#text = `${text}`;
this.#length = this.#text.length;
this.#tokenized = this.#tokenize();
this.#options = options;
}
parse(): JsonValue {
const token = this.#getNext();
Expand Down Expand Up @@ -238,12 +222,9 @@ class JSONCParser {
// │ │ │ │ │ │ ┌─────token3
// │ │ │ │ │ │ │ ┌─token4
// { "key" : value , "key" : value }
for (let isFirst = true;; isFirst = false) {
while (true) {
const token1 = this.#getNext();
if (
(isFirst || this.#options.allowTrailingComma) &&
token1.type === "EndObject"
) {
if (token1.type === "EndObject") {
return target;
}
if (token1.type !== "String") {
Expand Down Expand Up @@ -290,12 +271,9 @@ class JSONCParser {
// │ │ ┌─────token1
// │ │ │ ┌─token2
// [ value , value ]
for (let isFirst = true;; isFirst = false) {
while (true) {
const token1 = this.#getNext();
if (
(isFirst || this.#options.allowTrailingComma) &&
token1.type === "EndArray"
) {
if (token1.type === "EndArray") {
return target;
}
target.push(this.#parseJsonValue(token1));
Expand Down
17 changes: 8 additions & 9 deletions jsonc/parse_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { parse, type ParseOptions } from "./parse.ts";
import { parse } from "./parse.ts";
import {
assert,
assertEquals,
Expand All @@ -14,23 +14,18 @@ import "./testdata/test262/test.ts";

// The test code for the jsonc module can also be found in the testcode directory.

function assertValidParse(
text: string,
expected: unknown,
options?: ParseOptions,
) {
assertEquals(parse(text, options), expected);
function assertValidParse(text: string, expected: unknown) {
assertEquals(parse(text), expected);
}

function assertInvalidParse(
text: string,
// deno-lint-ignore no-explicit-any
ErrorClass: new (...args: any[]) => Error,
msgIncludes?: string,
options?: ParseOptions,
) {
assertThrows(
() => parse(text, options),
() => parse(text),
ErrorClass,
msgIncludes,
);
Expand Down Expand Up @@ -231,3 +226,7 @@ Deno.test({
);
},
});

Deno.test("parse() handles lone continuation byte in key and tailing comma", () => {
assertEquals(parse('{"�":"0",}'), { "�": "0" });
});
2 changes: 1 addition & 1 deletion jsonc/testdata/JSONTestSuite/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ for await (
JSON.parse(text);
});
const [hasJsoncError, jsoncError, jsoncResult] = getError(() => {
JSONC.parse(text, { allowTrailingComma: false });
JSONC.parse(text);
});

// If an error occurs in JSON.parse() but no error occurs in JSONC.parse(), or vice versa, an error is thrown.
Expand Down

This file was deleted.

18 changes: 2 additions & 16 deletions jsonc/testdata/node-jsonc-parser/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ import { assertEquals, assertThrows } from "../../../assert/mod.ts";
function assertValidParse(
text: string,
expected: unknown,
options?: JSONC.ParseOptions,
) {
assertEquals(JSONC.parse(text, options), expected);
assertEquals(JSONC.parse(text), expected);
}
function assertInvalidParse(
text: string,
// deno-lint-ignore no-explicit-any
ErrorClass: (new (...args: any[]) => Error),
msgIncludes?: string,
options?: JSONC.ParseOptions,
) {
assertThrows(
() => JSONC.parse(text, options),
() => JSONC.parse(text),
ErrorClass,
msgIncludes,
);
Expand Down Expand Up @@ -83,9 +81,6 @@ Deno.test("[jsonc] parse node-jsonc-parser:arrays", () => {

Deno.test("[jsonc] parse node-jsonc-parser:objects with errors", () => {
assertInvalidParse("{,}", SyntaxError);
assertInvalidParse('{ "foo": true, }', SyntaxError, undefined, {
allowTrailingComma: false,
});
assertInvalidParse('{ "bar": 8 "xoo": "foo" }', SyntaxError);
assertInvalidParse('{ ,"bar": 8 }', SyntaxError);
assertInvalidParse('{ ,"bar": 8, "foo" }', SyntaxError);
Expand Down Expand Up @@ -119,13 +114,4 @@ Deno.test("[jsonc] parse node-jsonc-parser:trailing comma", () => {
);
assertValidParse("[ 1, 2, ]", [1, 2]);
assertValidParse("[ 1, 2 ]", [1, 2]);

assertInvalidParse('{ "hello": [], }', SyntaxError, undefined, options);
assertInvalidParse(
'{ "hello": [], "world": {}, }',
SyntaxError,
undefined,
options,
);
assertInvalidParse("[ 1, 2, ]", SyntaxError, undefined, options);
});