Skip to content

Commit

Permalink
fix(templates): use re2 for replace helper (#26019)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Nov 29, 2023
1 parent 8c78b5a commit 6af2be7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/util/template/index.spec.ts
Expand Up @@ -114,6 +114,15 @@ describe('util/template/index', () => {
expect(output).toBe('CUSTOM_FOO is foo');
});

it('replace', () => {
const userTemplate =
"{{ replace '[a-z]+\\.github\\.com' 'ghc' depName }}{{ replace 'some' 'other' depType }}";
const output = template.compile(userTemplate, {
depName: 'some.github.com/dep',
});
expect(output).toBe('ghc/dep');
});

describe('proxyCompileInput', () => {
const allowedField = 'body';
const allowedArrayField = 'prBodyNotes';
Expand Down
8 changes: 3 additions & 5 deletions lib/util/template/index.ts
Expand Up @@ -3,6 +3,7 @@ import handlebars from 'handlebars';
import { GlobalConfig } from '../../config/global';
import { logger } from '../../logger';
import { getChildEnv } from '../exec/utils';
import { regEx } from '../regex';

handlebars.registerHelper('encodeURIComponent', encodeURIComponent);
handlebars.registerHelper('decodeURIComponent', decodeURIComponent);
Expand All @@ -11,11 +12,8 @@ handlebars.registerHelper('stringToPrettyJSON', (input: string): string =>
JSON.stringify(JSON.parse(input), null, 2),
);

// istanbul ignore next
handlebars.registerHelper(
'replace',
(find, replace, context) =>
(context || '').replace(new RegExp(find, 'g'), replace), // TODO #12873
handlebars.registerHelper('replace', (find, replace, context) =>
(context ?? '').replace(regEx(find, 'g'), replace),
);

handlebars.registerHelper('lowercase', (str: string) => str?.toLowerCase());
Expand Down

0 comments on commit 6af2be7

Please sign in to comment.