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 <noreply@anthropic.com>
This commit is contained in:
Sebastian DanielssonandClaude Opus 5.5 authored and sharevb committed 2026-10-03 21:11:50 +02:00
1 parent 3e7f8459ff
commit b688f528bd
2 files changed
+17 -3

No files matched your search

+13
View File
@@ -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'] };
+4 -3
View File
@@ -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 '';
}