Skip to content

Feature/ jsonc #11

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Jsonc
- Refactor read comments function
- Added additional unit tests for branch coverage
  • Loading branch information
Sykander committed Mar 3, 2023
commit 4513650e3f99ffcb24a7dfb0ff85483d9cf57746
51 changes: 22 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,36 @@ exports.parse = function (source, _, options) {
var nextChar = getChar();
commentStr += nextChar;

if (nextChar === '/') {
// read until `\n`
singleLineComment: {
while (true) {
nextChar = getChar();

if (nextChar === '\n') {
line++;
break singleLineComment;
}
var singleLineComment = nextChar === '/';
var multiLineComment = nextChar === '*';

commentStr += nextChar;
}
}
} else if (nextChar === '*') {
// read until `*/`
multiLineComment: {
while (true) {
nextChar = getChar();
if (!singleLineComment && !multiLineComment)
wasUnexpectedToken();

if (nextChar === '\n')
line++;
if (multiLineComment && source[pos] === '*')
getChar();

if (nextChar === '*') {
commentStr += nextChar;
nextChar = getChar();
commentStr += nextChar;
readComment: {
while (true) {
nextChar = getChar();

if (nextChar === '/')
break multiLineComment;
}
if (nextChar === '\n') {
line++;
if (singleLineComment)
break readComment;
}

if (multiLineComment && nextChar === '*') {
commentStr += nextChar;
nextChar = getChar();
commentStr += nextChar;

if (nextChar === '/')
break readComment;
}

commentStr += nextChar;
}
} else {
wasUnexpectedToken();
}

return commentStr;
Expand Down
8 changes: 5 additions & 3 deletions spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ describe('parse', function() {

const badJsonc = `{ "prop1": "test" / }`;

const jsonWithTrailingComma = `{ "prop1": "test1", "prop2": [1, 2, 3,] , }`;
const jsonWithTrailingComma = `{ "prop1": "test1", "prop2": [1, 2, 3,] }`;
const jsonObjWithTrailingComma = `{ "prop1": "test1", "prop2": [1, 2, 3] , }`;

it("Should parse jsonc comments as whitespace and execute as normal if jsonc true", () => {
assert.deepStrictEqual(jsonMap.parse(jsonc, null, { jsonc: true }).data, { "prop1": "test", "prop2": "test2", "prop3": [123, "456", 789], "prop4": "test3" });
Expand All @@ -320,11 +321,12 @@ describe('parse', function() {
assert.throws(() => jsonMap.parse(badJsonc, null, { jsonc: true }), /Unexpected token[ ]{3}in JSON at position 19/, "Didn't throw error for unterminated multiline string");
});

it("Should throw errors for invalid json if jsonc option false or not given and json contains comments", () => {
it("Should throw errors for invalid json if jsonc option false or not given and json contains comments or trailing commas", () => {
assert.throws(() => jsonMap.parse(jsonc, null, { jsonc: false }), /Unexpected token [/] in JSON at position 4/, "Didn't throw error when jsonc option false and comments in json.");
assert.throws(() => jsonMap.parse(jsonc, null, {}), /Unexpected token [/] in JSON at position 4/, "Didn't throw error when empty options given and comments in json.");
assert.throws(() => jsonMap.parse(jsonc), /Unexpected token [/] in JSON at position 4/, "Didn't throw error when jsonc not given and comments in json.");
assert.throws(() => jsonMap.parse(jsonWithTrailingComma), /Unexpected token ] in JSON at position 38/, "Didn't throw error when jsonc not given and trailing commas in json.");
assert.throws(() => jsonMap.parse(jsonWithTrailingComma), /Unexpected token ] in JSON at position 38/, "Didn't throw error when jsonc not given and trailing comma in array.");
assert.throws(() => jsonMap.parse(jsonObjWithTrailingComma), /Unexpected token } in JSON at position 45/, "Didn't throw error when jsonc not given and trailing comma in object.");
});
});

Expand Down