Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ function openFile(file, config) {
function doReplacement(_file_rr: string, _config_rr: any, _data_rr: string) {
chat('Work on content from: ' + _file_rr);

// Main regexp of the whole thing
// Main regexp of the whole thing. With --output-match the replacement prints the matches, so the code of --replacement-js is not run.
const result = _data_rr.replace(
_config_rr.regex,
_config_rr.replacementJs
_config_rr.replacementJs && !_config_rr.outputMatch
? dynamicReplacement(_file_rr, _config_rr, _data_rr)
: _config_rr.replacement
);
Expand Down Expand Up @@ -568,9 +568,10 @@ function dynamicReplacement(_file_rr, _config_rr, _data_rr) {
_size = readableSize(_bytes);
}

// Run only once if no captured groups (replacement cant change)
// Run only once if no captured groups (replacement cant change).
// The result is given back from a function, so `$&` and `$$` in it are inserted as they are and not expanded.
if (!/\$\d/.test(_config_rr.replacement)) {
return dynamicContent(
const result = dynamicContent(
require,
fs,
globs,
Expand Down Expand Up @@ -619,6 +620,7 @@ function dynamicReplacement(_file_rr, _config_rr, _data_rr) {
_,
code_rr
);
return () => result;
}
// Capture groups used, so need to run once per match
return function () {
Expand Down
11 changes: 4 additions & 7 deletions test/js/replacement-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,10 @@ describe('--replacement-js', () => {
});
});

test.todo(
'BUG #689: the result of the code is inserted as it is (now $& and $$ in it are expanded)',
async () => {
expect(await js('o', '"$" + "&"', 'fo')).toBe('f$&');
expect(await js('o', '"$" + "$"', 'fo')).toBe('f$$');
}
);
test('the result of the code is inserted as it is, also $& and $$ (#689)', async () => {
expect(await js('o', '"$" + "&"', 'fo')).toBe('f$&');
expect(await js('o', '"$" + "$"', 'fo')).toBe('f$$');
});

describe('combined', () => {
test('with --void-euro there is only the $ to get to the groups', async () => {
Expand Down
Loading