Skip to content
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
5 changes: 4 additions & 1 deletion src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export function alias(config) {
};

config.pattern = swap(config.pattern);
config.replacement = swap(config.replacement);
// With --replacement-file the replacement is the name of a file, unless --js code gives the name
if (!config.replacementFile || config.replacementJs) {
config.replacement = swap(config.replacement);
}
config.excludeRe = config.excludeRe.map(swap);
config.delimiter = swap(config.delimiter);
config.nameDelimiter = swap(config.nameDelimiter);
Expand Down
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ const unescape = (key, name) => {
}
};

if (!config.replacementJs) {
// The path of --replacement-file is the name of a file, so it has no escapes
if (!config.replacementJs && !config.replacementFile) {
unescape('replacement', 'replacement');
}

Expand Down
32 changes: 32 additions & 0 deletions test/fix/817.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://github.com/mathiasrw/rexreplace/issues/817
// The path of --replacement-file went through the escapes and the € and § aliases of a typed replacement,
// so a backslash in a Windows path was lost, and price€.txt was looked for as price$.txt.
import {describe, test, expect} from 'bun:test';
import fs from 'fs';
import {$} from 'bun';
import {cli, sh} from '../helper.js';

describe('#817 the path of --replacement-file', () => {
test('keeps a backslash', async () => {
fs.mkdirSync('my\\dir');
fs.writeFileSync('my\\dir/new.txt', 'NEW');
fs.writeFileSync('page.txt', 'X');
const result = await sh($`bun ${cli} X ${'my\\dir/new.txt'} page.txt -o --replacement-file`);
expect(result).toEqual({out: 'NEW\n', err: 'page.txt\n', exit: 0});
});

test('keeps € and §, as the aliases are not used in file names', async () => {
fs.writeFileSync('price€§.txt', 'PRICE');
fs.writeFileSync('page.txt', 'X');
const result = await sh($`bun ${cli} X ${'price€§.txt'} page.txt -o --replacement-file`);
expect(result.out).toBe('PRICE\n');
});

test('still uses the aliases in the code of --js, which gives the path', async () => {
fs.writeFileSync('new.txt', 'NEW');
fs.writeFileSync('page.txt', 'X');
const code = '"new" + €0.replace("X", ".txt")';
const result = await sh($`bun ${cli} X ${code} page.txt -o --replacement-file --js`);
expect(result.out).toBe('NEW\n');
});
});
Loading