From c00db14474df2250af9bde73a52164b674922b13 Mon Sep 17 00:00:00 2001 From: Mathias Wulff Date: Fri, 23 Oct 2026 21:03:12 +1100 Subject: [PATCH] Warn when --js code is not run with --output-match --- README.md | 2 +- src/cli.ts | 2 +- src/engine.ts | 4 ++++ test/engine/output-match.test.js | 9 +++++++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eefacc19..2255212a 100644 --- a/README.md +++ b/README.md @@ -330,7 +330,7 @@ If planned renames clash, the process is halted and no change is applied to any | `-r` | **`--rename`** Search and replace in the name of each file, and rename the file to the result. The folder and the content are not touched. If planned renames clash the process is halted and no change is applied to any file. [boolean] | | | **`--rename-path`** Like --rename, but on the whole path of each file, so a / in the replacement moves the file. Folders that are not there are made. [boolean] | | | **`--rename-force`** With --rename or --rename-path, write over a file that is there already. [boolean] | -| `-m` | **`--output-match`** Output each match on a new line. Will not replace any content but you still need to provide a dummy value (like `_`) as replacement parameter. If search pattern does not contain matching groups the full match will be outputted. If search pattern does contain matching groups only matching groups will be outputted (same line with no delimiter). [boolean] | +| `-m` | **`--output-match`** Output each match on a new line. Will not replace any content but you still need to provide a dummy value (like `_`) as replacement parameter. The replacement is not used, so --js code is not run. If search pattern does not contain matching groups the full match will be outputted. If search pattern does contain matching groups only matching groups will be outputted (same line with no delimiter). [boolean] | | `-T` | **`--trim-pipe`** Trim piped data before processing. If piped data only consists of chars that can be trimmed (new line, space, tabs...) it will become an empty string. [boolean] | | | **`--js-pipe`** Treat the replacement as javascript, and give the code the piped data as `pipe`. [boolean] | | `-g` | **`--glob-pipe`** Each line of the piped data is a path or a glob of the files to work on. They are used together with the ones given as arguments. [boolean] | diff --git a/src/cli.ts b/src/cli.ts index 58360644..4fc796c7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -288,7 +288,7 @@ const yargs = require('yargs') .describe( 'm', `Output each match on a new line. ` + - `Will not replace any content but you still need to provide a dummy value (like \`_\`) as replacement parameter. ` + + `Will not replace any content but you still need to provide a dummy value (like \`_\`) as replacement parameter. The replacement is not used, so --js code is not run. ` + `If search pattern does not contain matching groups the full match will be outputted. ` + `If search pattern does contain matching groups only matching groups will be outputted (same line with no delimiter). ` + `` diff --git a/src/engine.ts b/src/engine.ts index 7d8faa54..22edce16 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -100,6 +100,10 @@ function newStatus(config) { async function run(config) { validateEncoding(config.encoding); + if (config.replacementJs && config.outputMatch) { + warn('The code is not run with --output-match, as only the matches are printed.'); + } + if (config.expectZeroDiff && (config.expectDiff || config.expectDiffAll)) { panic('--expect-zero-diff can not be used with --expect-diff or --expect-diff-all'); } diff --git a/test/engine/output-match.test.js b/test/engine/output-match.test.js index d7ed3b5e..11590b46 100644 --- a/test/engine/output-match.test.js +++ b/test/engine/output-match.test.js @@ -89,9 +89,14 @@ describe('--output-match', () => { ).toBe('a.\n'); }); - test('with --js the code is not run', async () => { + test('with --js the code is not run, and a warning tells so', async () => { const config = {pipedData: 'foobar', outputMatch: true, replacementJs: true}; - expect((await rr('[fb].', 'throw new Error("should not run")', config)).out).toBe('fo\nba\n'); + expect(await rr('[fb].', 'throw new Error("should not run")', config)).toEqual({ + out: 'fo\nba\n', + err: ' 🟡 The code is not run with --output-match, as only the matches are printed.\n\n', + exit: null, + }); + expect((await rr('[fb].', '_', {...config, quiet: true})).err).toBe(''); }); test('with --replacement-pipe the piped replacement is not used', async () => {