From b688f528bd308a11aa153e487a2ee805f42c270f Mon Sep 17 00:00:00 2001 From: Sebastian Danielsson Date: Sun, 27 Sep 2026 23:50:18 +0200 Subject: [PATCH] fix(objgen): comment detection and whitespace stripping The comment check passed its pattern as a string, so every `\s` became a literal `s`. Lines starting with `s` that contained `//` or ended in `/` (e.g. `site = https://example.com`) were dropped as comments. Lines are already trimmed, so only `//` and lone `/` lines need skipping. `spacesRegx` and `newLineRegx` lacked the global flag: prop names kept all but their first space, and whitespace-only lines were treated as content. Fixes CodeQL js/useless-regexp-character-escape and js/incomplete-sanitization alerts in src/utils/objgen.ts. Co-Authored-By: Claude Opus 5.5 --- src/utils/objgen.test.ts | 13 +++++++++++++ src/utils/objgen.ts | 7 ++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils/objgen.test.ts b/src/utils/objgen.test.ts index 56fab781..3e74316b 100644 --- a/src/utils/objgen.test.ts +++ b/src/utils/objgen.test.ts @@ -96,6 +96,19 @@ person expect(JSON.parse(ObjGen2Json('this.that s = xxx'))).to.deep.eq(dots); }); + it('should keep lines starting with "s" that contain slashes', () => { + const model = '// a comment\n' + 'site = https://example.com\n' + 'src = assets/'; + expect(JSON.parse(ObjGen2Json(model))).to.deep.eq({ site: 'https://example.com', src: 'assets/' }); + }); + + it('should strip every space from prop names', () => { + expect(JSON.parse(ObjGen2Json('name =\nid = 1'))).to.deep.eq({ name: '', id: '1' }); + }); + + it('should ignore whitespace-only lines', () => { + expect(JSON.parse(ObjGen2Json('id = 1\n \nname = test'))).to.deep.eq({ id: '1', name: 'test' }); + }); + it('should generate an array of strings', () => { const model = 'a[] = 1, 2, 3'; const strings = { a: ['1', '2', '3'] }; diff --git a/src/utils/objgen.ts b/src/utils/objgen.ts index a3d2a903..6eee9d40 100644 --- a/src/utils/objgen.ts +++ b/src/utils/objgen.ts @@ -4,8 +4,8 @@ // ! license : AGPL-3.0 const rawLineRegx = /^.*$|\n|$/g; -const newLineRegx = /\n/; -const spacesRegx = /\s/; +const newLineRegx = /\n/g; +const spacesRegx = /\s/g; const arrayRegx = /\[\s*?([0-9]{1,100})?\s*?\]/; const typesRegx = /^(\w+)(\s+)(\w+).*?$/; @@ -146,7 +146,8 @@ export function ObjGen2Json(val: string, options?: { numSpaces?: number }) { // parse the raw inbound lines, to find individual input lines parseLines(val, options, (line, depth) => { - if (line.match('^\s+$/|^\/$|^\/\/|^\s.*\/$|^\s.*\/\/') !== null) { + // lines are already trimmed: skip `//` comments and lone `/` lines + if (/^\/\/|^\/$/.test(line)) { return ''; }