diff --git a/scripts/sync-help-from-civi.mjs b/scripts/sync-help-from-civi.mjs index 4fc3cd8..3f5d302 100644 --- a/scripts/sync-help-from-civi.mjs +++ b/scripts/sync-help-from-civi.mjs @@ -233,12 +233,25 @@ function rewriteText(text, changes) { } else { // Multi-line: insert a new help: line just before the closing `}` line, // 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 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 indent = indentM ? indentM[1] : " "; const closeIdx = f.blockEnd - 1; const lastNL = out.lastIndexOf("\n", closeIdx); - const beforeClose = out.slice(0, lastNL); + let beforeClose = out.slice(0, 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; } }