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:
@@ -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 <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 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user