refactor: replace cjson with internal comment-stripping JSON parser#10662
refactor: replace cjson with internal comment-stripping JSON parser#10662joehan wants to merge 1 commit into
Conversation
### Description Removed the external third-party `cjson` dependency and replaced it with a robust custom comment-stripping parser in `src/loadCJSON.ts`. This reduces production dependency weight. ### Scenarios Tested - Added a new `complex.cjson` test fixture with trailing, inline, and string-embedded comments. - Run `npx mocha src/loadCJSON.spec.ts` successfully. ### Sample Commands - `npx mocha src/loadCJSON.spec.ts`
There was a problem hiding this comment.
Code Review
This pull request replaces the external 'cjson' dependency with a custom comment-stripping implementation using regular expressions. A critical bug was identified in the new implementation where empty strings are incorrectly stripped from the JSON, leading to syntax errors during parsing. The feedback recommends fixing this by explicitly checking for undefined and suggests replacing comments with whitespace to preserve line numbers for improved error reporting.
| const stripped = content.replace( | ||
| /("([^"\\]|\\.)*")|(\/\*[\s\S]*?\*\/|\/\/.*)/g, | ||
| (match, string) => { | ||
| if (string) return string; | ||
| return ""; | ||
| }, | ||
| ); |
There was a problem hiding this comment.
Issue: Critical Bug with Empty Strings ""
The current implementation of the comment-stripping parser has a critical bug when parsing empty strings "" in JSON.
In the replacement callback:
(match, string) => {
if (string) return string;
return "";
}The parameter string corresponds to the first capture group ("([^"\\]|\\.)*"). When the parser encounters an empty string "", the captured value is "". Since "" is a falsy value in JavaScript, if (string) evaluates to false, causing the function to return "" (which is treated as a comment removal).
This results in the empty string being completely stripped from the JSON, leading to invalid JSON and a SyntaxError during JSON.parse. For example, {"key": ""} becomes {"key": }, which fails to parse.
Suggestion: Preserve Line Numbers & Fix Empty Strings
We can fix this bug by explicitly checking if quotedString is not undefined.
Additionally, we can improve error reporting by replacing comments with spaces/newlines instead of an empty string "". This preserves the exact line and column numbers of the original file, making any JSON parsing error messages much more accurate and helpful.
const stripped = content.replace(
/("([^"\\]|\\.)*")|(\/\*[\s\S]*?\*\/|\/\/.*)/g,
(match, quotedString) => {
if (quotedString !== undefined) {
return quotedString;
}
return match.replace(/[^\r\n]/g, " ");
},
);
Description
Removed the external third-party
cjsondependency and replaced it with a robust custom comment-stripping parser insrc/loadCJSON.ts. This reduces production dependency weight.Scenarios Tested
complex.cjsontest fixture with trailing, inline, and string-embedded comments.npx mocha src/loadCJSON.spec.tssuccessfully.Sample Commands
npx mocha src/loadCJSON.spec.ts