Skip to content

Commit d871185

Browse files
committed
Add --fix-dry-run flag
Fixes #663
1 parent 23b5972 commit d871185

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

cli.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const cli = meow(
2020
2121
Options
2222
--fix Automagically fix issues
23+
--fix-dry-run Automagically fix issues without saving the changes to the file system
2324
--reporter Reporter to use
2425
--space Use space indent instead of tabs [Default: 2]
2526
--config Path to a XO configuration file
@@ -51,6 +52,10 @@ const cli = meow(
5152
type: 'boolean',
5253
default: false,
5354
},
55+
fixDryRun: {
56+
type: 'boolean',
57+
default: false,
58+
},
5459
reporter: {
5560
type: 'string',
5661
},
@@ -116,7 +121,7 @@ const baseXoConfigOptions: XoConfigOptions = {
116121
};
117122

118123
const linterOptions: LinterOptions = {
119-
fix: cliOptions.fix,
124+
fix: cliOptions.fix || cliOptions.fixDryRun,
120125
cwd: (cliOptions.cwd && path.resolve(cliOptions.cwd)) ?? process.cwd(),
121126
quiet: cliOptions.quiet,
122127
ts: true,
@@ -201,7 +206,7 @@ if (cliOptions.stdin) {
201206
}
202207
}
203208

204-
if (cliOptions.fix) {
209+
if (linterOptions.fix) {
205210
const xo = new Xo(linterOptions, baseXoConfigOptions);
206211
const {results: [result]} = await xo.lintText(stdin, {
207212
filePath: cliOptions.stdinFilename,

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ $ xo --help
5858
5959
Options
6060
--fix Automagically fix issues
61+
--fix-dry-run Automagically fix issues without saving the changes to the file system
6162
--reporter Reporter to use
6263
--space Use space indent instead of tabs [Default: 2]
6364
--config Path to a XO configuration file

test/cli.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ test('xo --fix', async t => {
4343
t.is(fileContent, dedent`console.log('hello');\n`);
4444
});
4545

46+
test('xo --fix-dry-run', async t => {
47+
const filePath = path.join(t.context.cwd, 'test.js');
48+
const original = dedent`console.log('hello')\n`;
49+
await fs.writeFile(filePath, original, 'utf8');
50+
await t.notThrowsAsync($`node ./dist/cli --cwd ${t.context.cwd} --fix-dry-run`);
51+
const fileContent = await fs.readFile(filePath, 'utf8');
52+
t.is(fileContent, original, 'File should not be modified with --fix-dry-run');
53+
});
54+
4655
test('xo --fix --space', async t => {
4756
const filePath = path.join(t.context.cwd, 'test.js');
4857
await fs.writeFile(filePath, dedent`function test() {\n return true;\n}\n`, 'utf8');
@@ -162,6 +171,11 @@ test('xo --stdin --fix', async t => {
162171
t.is(stdout, 'const x = true;');
163172
});
164173

174+
test('xo --stdin --fix-dry-run', async t => {
175+
const {stdout} = await $`echo ${'const x = true'}`.pipe`node ./dist/cli --cwd=${t.context.cwd} --stdin --fix-dry-run`;
176+
t.is(stdout, 'const x = true;');
177+
});
178+
165179
test('xo --stdin --stdin-filename=test.js', async t => {
166180
const {stdout} = await $`echo ${'const x = true'}`.pipe`node ./dist/cli --cwd=${t.context.cwd} --stdin --stdin-filename=test.js`;
167181
t.true(stdout.includes('test.js'));

0 commit comments

Comments
 (0)