sync-help: ensure trailing comma before inserting new help line

Multi-line field whose last property had no trailing comma got
corrupted into invalid JS:

  civiField: `${G0}.X`
  help: "...",

(JS requires the comma between properties.) The insertion path now
checks the last non-whitespace, non-comma char of the property line
just above the closing brace; if it isn't already a comma, one is
inserted before the new help line is spliced in.
This commit is contained in:
Joel Brock
2026-05-21 13:21:54 -07:00
parent d4a6709149
commit d2d733a1d6
+14 -1
View File
@@ -233,12 +233,25 @@ function rewriteText(text, changes) {
} else { } else {
// Multi-line: insert a new help: line just before the closing `}` line, // Multi-line: insert a new help: line just before the closing `}` line,
// using the indent of the first property after `{`. // using the indent of the first property after `{`.
//
// The previous-property line may or may not end with a trailing comma
// (JS allows the last property to drop the comma). When it doesn't,
// inserting a new `help:` line below it produces `prevProp <NL> help:`
// which is a syntax error. So: if the line right before the closing
// `}` line doesn't end with a comma (ignoring trailing whitespace),
// append one before we splice the new line in.
const indentM = block.match(/\{\s*\n([ \t]+)\S/); const indentM = block.match(/\{\s*\n([ \t]+)\S/);
const indent = indentM ? indentM[1] : " "; const indent = indentM ? indentM[1] : " ";
const closeIdx = f.blockEnd - 1; const closeIdx = f.blockEnd - 1;
const lastNL = out.lastIndexOf("\n", closeIdx); const lastNL = out.lastIndexOf("\n", closeIdx);
const beforeClose = out.slice(0, lastNL); let beforeClose = out.slice(0, lastNL);
const afterNL = out.slice(lastNL); const afterNL = out.slice(lastNL);
const prevPropTail = beforeClose.match(/([^\s,])\s*$/);
if (prevPropTail) {
// Insert a `,` right after the last non-whitespace, non-comma char.
const insertAt = beforeClose.length - prevPropTail[0].length + 1;
beforeClose = beforeClose.slice(0, insertAt) + "," + beforeClose.slice(insertAt);
}
out = beforeClose + "\n" + indent + newLiteral + "," + afterNL; out = beforeClose + "\n" + indent + newLiteral + "," + afterNL;
} }
} }