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

refactor(jsonc): align additional error messages #5799

Merged
merged 3 commits into from
Aug 24, 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
14 changes: 10 additions & 4 deletions jsonc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export type { JsonValue };
*/
export function parse(text: string): JsonValue {
if (new.target) {
throw new TypeError("parse is not a constructor");
throw new TypeError(
"Cannot create an instance: parse is not a constructor",
);
}
return new JSONCParser(text).parse();
}
Expand Down Expand Up @@ -83,7 +85,9 @@ class JSONCParser {
#getNext(): Token {
const { done, value } = this.#tokenized.next();
if (done) {
throw new SyntaxError("Unexpected end of JSONC input");
throw new SyntaxError(
"Cannot parse JSONC: unexpected end of JSONC input",
);
}
return value;
}
Expand All @@ -106,7 +110,9 @@ class JSONCParser {
}
}
if (!hasEndOfComment) {
throw new SyntaxError("Unexpected end of JSONC input");
throw new SyntaxError(
"Cannot parse JSONC: unexpected end of JSONC input",
);
}
i++;
continue;
Expand Down Expand Up @@ -362,5 +368,5 @@ function buildErrorMessage({ type, sourceText, position }: Token): string {
: sourceText;
break;
}
return `Unexpected token ${token} in JSONC at position ${position}`;
return `Cannot parse JSONC: unexpected token "${token}" in JSONC at position ${position}`;
}
16 changes: 8 additions & 8 deletions jsonc/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,37 @@ Deno.test({
assertInvalidParse(
`:::::`,
SyntaxError,
"Unexpected token : in JSONC at position 0",
'Cannot parse JSONC: unexpected token ":" in JSONC at position 0',
);
assertInvalidParse(
`[`,
SyntaxError,
"Unexpected end of JSONC input",
"Cannot parse JSONC: unexpected end of JSONC input",
);
assertInvalidParse(
`[]100`,
SyntaxError,
"Unexpected token 100 in JSONC at position 2",
'Cannot parse JSONC: unexpected token "100" in JSONC at position 2',
);
assertInvalidParse(
`[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]`,
SyntaxError,
"Unexpected token aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... in JSONC at position 1",
'Cannot parse JSONC: unexpected token "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." in JSONC at position 1',
);
assertInvalidParse(
`}`,
SyntaxError,
"Unexpected token } in JSONC at position 0",
'Cannot parse JSONC: unexpected token "}" in JSONC at position 0',
);
assertInvalidParse(
`]`,
SyntaxError,
"Unexpected token ] in JSONC at position 0",
'Cannot parse JSONC: unexpected token "]" in JSONC at position 0',
);
assertInvalidParse(
`,`,
SyntaxError,
"Unexpected token , in JSONC at position 0",
'Cannot parse JSONC: unexpected token "," in JSONC at position 0',
);
},
});
Expand Down Expand Up @@ -163,7 +163,7 @@ Deno.test({
// deno-lint-ignore no-explicit-any
undefined as any,
SyntaxError,
"Unexpected token undefined in JSONC at position 0",
'Cannot parse JSONC: unexpected token "undefined" in JSONC at position 0',
);
// deno-lint-ignore no-explicit-any
assertValidParse(0 as any, 0);
Expand Down