diff --git a/cli.js b/cli.js index a12b8d0c..da507c39 100755 --- a/cli.js +++ b/cli.js @@ -13,6 +13,7 @@ const cli = meow(` Options --fix Automagically fix issues + --fix-dry-run Automatically fix problems without saving the changes to the file system --reporter Reporter to use --env Environment preset [Can be set multiple times] --global Global variable [Can be set multiple times] @@ -53,6 +54,9 @@ const cli = meow(` fix: { type: 'boolean', }, + fixDryRun: { + type: 'boolean', + }, reporter: { type: 'string', }, diff --git a/lib/options-manager.js b/lib/options-manager.js index 8f411509..fba07cce 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -309,6 +309,13 @@ const buildESLintConfig = options => config => { }; } + // ESLint does not expose a `fixDryRun` option in the programmatic API. + // Since ESLint programmatic API's `fix` option does not mean saving the changes to the file system, unlike ESLint CLI's one. + // So, the dry-run can be implemented by passing `fix` option to ESLint's API, and not calling `outputFixes`. + if (options.fixDryRun) { + config.fix = true; + } + return { ...config, ...pick(options, ['cwd', 'filePath', 'fix']), diff --git a/readme.md b/readme.md index 0980da30..68084be3 100644 --- a/readme.md +++ b/readme.md @@ -57,6 +57,7 @@ $ xo --help Options --fix Automagically fix issues + --fix-dry-run Automatically fix problems without saving the changes to the file system --reporter Reporter to use --env Environment preset [Can be set multiple times] --global Global variable [Can be set multiple times] diff --git a/test/cli.js b/test/cli.js index 43db20d4..65880b98 100644 --- a/test/cli.js +++ b/test/cli.js @@ -26,6 +26,21 @@ test('fix option with stdin', async t => { t.is(stdout, 'console.log();'); }); +test('fix-dry-run option should not overwrite source file', async t => { + const cwd = await fs.promises.mkdtemp(path.join(__dirname, './temp/')); + const filepath = path.join(cwd, 'x.js'); + await fs.promises.writeFile(filepath, 'console.log()\n'); + await main(['--fix-dry-run', filepath], {cwd}); + t.is(fs.readFileSync(filepath, 'utf8'), 'console.log()\n'); +}); + +test('fix-dry-run option with stdin', async t => { + const {stdout} = await main(['--fix-dry-run', '--stdin', '--reporter', 'json'], { + input: 'console.log()', + }); + t.is(JSON.parse(stdout)[0].output, 'console.log();\n'); +}); + test('stdin-filename option with stdin', async t => { const {stdout} = await main(['--stdin', '--stdin-filename=unicorn-file'], { input: 'console.log()\n',