diff --git a/src/engine.ts b/src/engine.ts index 9446cc37..16feff68 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -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 ); @@ -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, @@ -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 () { diff --git a/test/js/replacement-js.test.js b/test/js/replacement-js.test.js index a3f045da..2bead76f 100644 --- a/test/js/replacement-js.test.js +++ b/test/js/replacement-js.test.js @@ -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 () => {