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

Using --replacement-js flag when referencing matched groups while matching multiple files with glob notation #512

Open
Rockdell opened this issue Sep 9, 2024 · 1 comment
Labels

Comments

@Rockdell
Copy link
Contributor

Rockdell commented Sep 9, 2024

While making use of the --replacement-js flag and trying to use a matched group for the replacement string on multiples files it ends up giving an undefined error, even if the file contents do not match the given pattern.

For example running the following command with 2 existing files inside a "test" folder:

  • test1.txt with content: "test:1"
  • test2.txt with content: "noMatch:2"
root@fdde7fe2dc62:/workspaces# npx rexreplace 'test:(.*)' 'dirpath + €1' -A -j --debug 'test/*'
"2 files found "
"Open sync: test/test1.txt "
"Work on content from: test/test1.txt"
"Write new content to: test/test1.txt"
test/test1.txt 
"Open sync: test/test2.txt "
"Work on content from: test/test2.txt"
undefined:1
dirpath + $1
          ^

ReferenceError: $1 is not defined
    at eval (eval at <anonymous> (eval at dynamicReplacement (/root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:328:257)), <anonymous>:1:11)
    at eval (eval at dynamicReplacement (/root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:328:257), <anonymous>:3:258)
    at dynamicReplacement (/root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:359:17)
    at doReplacement (/root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:118:35)
    at openFile (/root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:102:17)
    at /root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:96:27
    at Array.forEach (<anonymous>)
    at engine (/root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:96:7)
    at /root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:555:10
    at /root/.npm/_npx/eb65943d65d3ffaf/node_modules/rexreplace/bin/rexreplace.cli.js:578:4

Node.js v20.17.0

This error occurs with the -o flag as well and with or without the -A flag.

However, with the -m flag it seems to find the matches correctly.

root@fdde7fe2dc62:/workspaces# npx rexreplace 'test:(.*)' 'dirpath + €1' -A -j -m --debug 'test/*'
"2 files found "
"Open sync: test/test1.txt "
"Work on content from: test/test1.txt"
1
"Open sync: test/test2.txt "
"Work on content from: test/test2.txt"

When not using a glob to find files and providing the exact file path it functions as expected:

root@fdde7fe2dc62:/workspaces# npx rexreplace 'test:(.*)' 'dirpath + €1' -A -j --debug 'test/test1.txt'
"1 files found "
"Open sync: test/test1.txt "
"Work on content from: test/test1.txt"
"Write new content to: test/test1.txt"
test/test1.txt 
root@fdde7fe2dc62:/workspaces# npx rexreplace 'test:(.*)' 'dirpath + €1' -A -j --debug 'test/test2.txt'
"1 files found "
"Open sync: test/test2.txt "
"Work on content from: test/test2.txt"
"Nothing changed in: test/test2.txt "
@Rockdell Rockdell changed the title Issue using --replacement-js flag when referencing matched groups while matching multiple files with glob notation Using --replacement-js flag when referencing matched groups while matching multiple files with glob notation Sep 9, 2024
@Rockdell
Copy link
Contributor Author

After a bit of analyzing it seems the issue was that the replacement string was being lost after the parsing the first file. I made a PR #513 fixing this issue. Not sure which branch to merge it to so I just used the default main branch.
I would very much appreciate your review 😄

@mathiasrw mathiasrw added the bug label Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants