Skip to content

Commit

Permalink
Fix replacement-js flag when referencing matched groups when matching…
Browse files Browse the repository at this point in the history
… multiple files (#513)

Co-authored-by: Pedro Pinho <[email protected]>
  • Loading branch information
Rockdell and Pedro Pinho authored Sep 11, 2024
1 parent 87e0575 commit 73fe5f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ function doReplacement(_file_rr: string, _config_rr: any, _data_rr: string) {

// Variables to be accessible from js.
if (_config_rr.replacementJs) {
_config_rr.replacement = dynamicReplacement(_file_rr, _config_rr, _data_rr);
_config_rr.replacementDynamic = dynamicReplacement(_file_rr, _config_rr, _data_rr);
}

// Main regexp of the whole thing
const result = _data_rr.replace(_config_rr.regex, _config_rr.replacement);
const result = _data_rr.replace(
_config_rr.regex,
_config_rr.replacementJs ? _config_rr.replacementDynamic : _config_rr.replacement
);

// The output of matched strings is done from the replacement, so no need to continue
if (_config_rr.outputMatch) {
Expand Down
5 changes: 5 additions & 0 deletions test/cli/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ assert "rexreplace 'foobar' \"['filename:'+filename,'name:'+name,'ext:'+ext,
reset
assert "rexreplace 'foo((b)ar)' '€1+€2' my.file -o --replacement-js" 'barb'

## Test replacement-js on multiple files
reset
assert "rexreplace 'foo((b)ar)' '€1+€2' *.file -o --replacement-js" 'barb\nabc123'

reset
assert "rexreplace 'a(.+)' '\"_replace_\"+€1' *.file -o --replacement-js" 'foob_replace_r\n_replace_bc123'

# Content manually testes
# todo: automate test of content
Expand Down

0 comments on commit 73fe5f7

Please sign in to comment.