Skip to content

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

@Rockdell

Description

@Rockdell

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 "

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions