Skip to content

Commit a7f2841

Browse files
committed
fix: harden regex and replacement patterns in sync script
Use replacer functions in String.replace() to prevent $-character interpretation, and use non-greedy regex for array matching to handle potential ] characters in comments.
1 parent eaaa9f1 commit a7f2841

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

scripts/sync-provider-models.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ function addToArray(
336336
arrayRef: string,
337337
): string {
338338
// Match the array declaration: export const ARRAY_NAME = [...] as const
339+
// Uses [\s\S]*? (non-greedy) instead of [^\]]* to handle ] inside comments
339340
const pattern = new RegExp(
340-
`(export const ${arrayName} = \\[[^\\]]*)(\\] as const)`,
341-
's',
341+
`(export const ${arrayName} = \\[\\s*[\\s\\S]*?)(\\] as const)`,
342342
)
343343
const match = pattern.exec(content)
344344
if (!match) {
@@ -349,7 +349,8 @@ function addToArray(
349349
const newEntries = entries
350350
.map((constName) => ` ${constName}${arrayRef},`)
351351
.join('\n')
352-
return content.replace(pattern, `${match[1]}\n${newEntries}\n${match[2]}`)
352+
// Use replacer function to prevent $-character interpretation in replacement string
353+
return content.replace(pattern, () => `${match[1]}\n${newEntries}\n${match[2]}`)
353354
}
354355

355356
/**
@@ -374,7 +375,8 @@ function addToTypeMap(
374375
}
375376

376377
const newEntries = entries.join('\n')
377-
return content.replace(pattern, `${match[1]}\n${newEntries}${match[2]}`)
378+
// Use replacer function to prevent $-character interpretation in replacement string
379+
return content.replace(pattern, () => `${match[1]}\n${newEntries}${match[2]}`)
378380
}
379381

380382
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)