Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix replacement-js flag when referencing matched groups when matching multiple files #513

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
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