diff --git a/bin/sync-models.mjs b/bin/sync-models.mjs index 6e307cab2..c5b1f7dc0 100755 --- a/bin/sync-models.mjs +++ b/bin/sync-models.mjs @@ -415,7 +415,8 @@ function setJsoncPath(text, keys, value) { } // Removes the member at `keys` by deleting its lines (and its comma). Comments -// on other lines are kept. +// on other lines are kept; an inline comment on the removed member is refused +// rather than silently discarded. function removeJsoncPath(text, keys) { let object = parseJsoncTree(text); for (const key of keys.slice(0, -1)) { @@ -428,6 +429,16 @@ function removeJsoncPath(text, keys) { if (text.slice(lineStart, member.keyStart).trim() !== '') { throw new Error(`cannot remove ${keys.join('/')}: it shares a line with other JSON`); } + const lineEnd = text.indexOf('\n', member.value.end); + const memberLineEnd = lineEnd < 0 ? text.length : lineEnd; + const trailing = text.slice(member.value.end, memberLineEnd); + if (trailing.includes('//') || trailing.includes('/*')) { + const error = new Error( + `cannot remove ${keys.join('/')}: inline comment on the member line would be lost`, + ); + error.name = 'JsoncCommentRemovalError'; + throw error; + } let end = member.value.end; while (text[end] === ' ' || text[end] === '\t') end++; if (text[end] === ',') end++; diff --git a/bin/sync-models.test.mjs b/bin/sync-models.test.mjs index f81a56585..fe4bbaed3 100644 --- a/bin/sync-models.test.mjs +++ b/bin/sync-models.test.mjs @@ -17,12 +17,19 @@ import { fileURLToPath } from 'node:url'; const binDir = path.dirname(fileURLToPath(import.meta.url)); const source = fs.readFileSync(path.join(binDir, 'sync-models.mjs'), 'utf-8'); -const start = source.indexOf('function parseJsonc(text) {'); -assert.notEqual(start, -1, 'parseJsonc not found in sync-models.mjs'); -const end = source.indexOf('\n}\n', start); -assert.notEqual(end, -1, 'parseJsonc body not terminated as expected'); -const parseJsonc = new Function( - `${source.slice(start, end + 3)}\nreturn parseJsonc;`, +function extractFunction(name) { + const start = source.indexOf(`function ${name}(`); + assert.notEqual(start, -1, `${name} not found in sync-models.mjs`); + const end = source.indexOf('\n}\n', start); + assert.notEqual(end, -1, `${name} body not terminated as expected`); + return source.slice(start, end + 3); +} + +const { parseJsonc, parseJsoncTree, removeJsoncPath } = new Function( + `${extractFunction('parseJsonc')}\n` + + `${extractFunction('parseJsoncTree')}\n` + + `${extractFunction('removeJsoncPath')}\n` + + 'return { parseJsonc, parseJsoncTree, removeJsoncPath };', )(); test('parses a config with a multi-line block comment', () => { @@ -71,6 +78,54 @@ test('still throws on a genuinely malformed config', () => { assert.throws(() => parseJsonc('{ "a": 1')); }); +const inlineCommentCases = [ + ['after the comma', `{ + "remove": true, // keep this comment + "keep": true +}`], + ['after a last-member value', `{ + "remove": true // keep this comment +}`], + ['between the value and comma', `{ + "remove": true /* keep this comment */, + "keep": true +}`], + ['before a comma on the next line', `{ + "remove": true // keep this comment + , + "keep": true +}`], +]; + +for (const [placement, text] of inlineCommentCases) { + test(`refuses to remove a member with an inline comment ${placement}`, () => { + assert.throws( + () => removeJsoncPath(text, ['remove']), + error => error.name === 'JsoncCommentRemovalError' && + /remove.*inline comment/i.test(error.message), + ); + }); +} + +test('removes a comment-free member and leaves parseable JSONC', () => { + const output = removeJsoncPath(`{ + "remove": true, + "keep": true +}`, ['remove']); + assert.deepEqual(parseJsonc(output), { keep: true }); + assert.equal(parseJsoncTree(output).members.length, 1); +}); + +test('keeps a standalone comment when removing the following member', () => { + const output = removeJsoncPath(`{ + // standalone note + "remove": true, + "keep": true +}`, ['remove']); + assert.match(output, /\/\/ standalone note/); + assert.deepEqual(parseJsonc(output), { keep: true }); +}); + // Strict argv validation (#3297): unknown arguments are a usage error, // reported before any fetch/discovery/write. These probes run the real // script with an isolated HOME and an explicit OPENCODE_CONFIG_PATH so the