Skip to content

Commit

Permalink
fix: escaping of \{\{2\}\} did not work (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Dec 8, 2023
1 parent cc6a9b6 commit d2d6ea1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [unreleased]

### Fix

- escaping of `\{\{2\}\}` did not work (#244)

## [6.10.0] (2023-11-13)

### Feature
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/core/replacer/escapeVariableReplacer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as models from '../../../models';
import { escapeVariableInterceptor } from './escapeVariableReplacer';
import { HookTriggerContext } from 'hookpoint';

describe('escapeVariableReplacer', () => {
const escape = (val: string) => {
const context = {
args: [val],
results: [],
} as unknown as HookTriggerContext<[unknown, string, models.ProcessorContext], unknown>;
escapeVariableInterceptor.afterLoop(context);
return context.results.length > 0 ? context.results[0] : undefined;
};
it('returns escaped value', () => {
// eslint-disable-next-line no-useless-escape
expect(escape(String.raw`\{\{1\}\} - \{\{2\}\} - \{\{3\}\}`)).toBe('{{1}} - {{2}} - {{3}}');
});
it('returns same value', () => {
expect(escape('foo {{ asdf } asdf')).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion src/plugins/core/replacer/escapeVariableReplacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const escapeVariableInterceptor = {
): Promise<boolean> {
const [text] = hookContext.args;
if (isString(text)) {
const escapeRegex = /(?:\\\{){2}([^}{2}]+)(?:\\\}){2}/gu;
const escapeRegex = /(?:\\\{){2}([^}]+?)(?:\\\}){2}/gu;
let match: RegExpExecArray | null;
let result = text;
while (isString(result) && (match = escapeRegex.exec(text)) !== null) {
Expand Down

0 comments on commit d2d6ea1

Please sign in to comment.