From 7b819253f28e82a8eeb1c58db27ee05f970fe5d8 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sat, 14 May 2022 11:04:07 +0900 Subject: [PATCH 1/6] Add `--fix-dry-run` option --- cli.js | 4 ++++ lib/options-manager.js | 4 ++++ readme.md | 1 + 3 files changed, 9 insertions(+) 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..4658e87b 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -308,6 +308,10 @@ const buildESLintConfig = options => config => { ...options.parserOptions, }; } + + if (options.fixDryRun) { + config.fix = true; + } return { ...config, 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] From 74d843ca6c47806d8314f14447c4ba2f2f33f34c Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sat, 14 May 2022 11:24:40 +0900 Subject: [PATCH 2/6] Add test --- lib/options-manager.js | 4 ++-- test/cli.js | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/options-manager.js b/lib/options-manager.js index 4658e87b..82449992 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -308,9 +308,9 @@ const buildESLintConfig = options => config => { ...options.parserOptions, }; } - + if (options.fixDryRun) { - config.fix = true; + config.fix = true; } return { diff --git a/test/cli.js b/test/cli.js index 43db20d4..49b08fc6 100644 --- a/test/cli.js +++ b/test/cli.js @@ -26,6 +26,13 @@ test('fix option with stdin', async t => { t.is(stdout, 'console.log();'); }); +test('fix-dry-run option', 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', From 7412dd1b287e26d7d610758012c6197e551bbaa6 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sat, 14 May 2022 11:34:59 +0900 Subject: [PATCH 3/6] Update test --- test/cli.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/cli.js b/test/cli.js index 49b08fc6..65880b98 100644 --- a/test/cli.js +++ b/test/cli.js @@ -26,7 +26,15 @@ test('fix option with stdin', async t => { t.is(stdout, 'console.log();'); }); -test('fix-dry-run option', async t => { +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()', }); From cef476b45d784b3d60cb56bc20df403b9b762d0f Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Sun, 22 May 2022 14:07:44 +0900 Subject: [PATCH 4/6] Add descriptive comment --- lib/options-manager.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/options-manager.js b/lib/options-manager.js index 82449992..fbb6727c 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -309,6 +309,10 @@ const buildESLintConfig = options => config => { }; } + // eslint doesn't expose `fixDryRun` option in programmatic API. + // Instead of using `fixDryRun` itself, the option is internally translated to `fix` option in eslint's CLI module. + // (Ref: https://github.com/eslint/eslint/blob/901ce0f1e32ea1e9e10ce4d8b37c0d750007a3c5/lib/cli.js#L96) + // xo also could handle this in the same way. if (options.fixDryRun) { config.fix = true; } From 6b7f964d671ceb827385c6144ac85962d82bbc8d Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 29 May 2022 15:32:38 +0700 Subject: [PATCH 5/6] Update options-manager.js --- lib/options-manager.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/options-manager.js b/lib/options-manager.js index fbb6727c..8cb3834d 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -309,10 +309,9 @@ const buildESLintConfig = options => config => { }; } - // eslint doesn't expose `fixDryRun` option in programmatic API. - // Instead of using `fixDryRun` itself, the option is internally translated to `fix` option in eslint's CLI module. - // (Ref: https://github.com/eslint/eslint/blob/901ce0f1e32ea1e9e10ce4d8b37c0d750007a3c5/lib/cli.js#L96) - // xo also could handle this in the same way. + // ESLint does not expose a `fixDryRun` option in the programmatic API. + // Instead of using `fixDryRun` itself, the option is internally translated to `fix` option in ESLint's CLI module. + // https://github.com/eslint/eslint/blob/901ce0f1e32ea1e9e10ce4d8b37c0d750007a3c5/lib/cli.js#L96 if (options.fixDryRun) { config.fix = true; } From be70732a61f7ffe5d47892edf6fce6e4674fd652 Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Thu, 2 Jun 2022 12:23:01 +0900 Subject: [PATCH 6/6] Improve comment description --- lib/options-manager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/options-manager.js b/lib/options-manager.js index 8cb3834d..fba07cce 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -310,8 +310,8 @@ const buildESLintConfig = options => config => { } // ESLint does not expose a `fixDryRun` option in the programmatic API. - // Instead of using `fixDryRun` itself, the option is internally translated to `fix` option in ESLint's CLI module. - // https://github.com/eslint/eslint/blob/901ce0f1e32ea1e9e10ce4d8b37c0d750007a3c5/lib/cli.js#L96 + // 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; }