Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --fix-dry-run option #665

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli.js
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's unclear to the user how this would even work. The user will ask themselves: "If it does not save to the file system, what's the point of it?"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--fix-dry-run     Automatically fix problems without saving the changes to the file system.
                  Because the default formatter does not output the fixed code, you'll have to use another one (e.g. json)
                  This flag can be useful for integrations which need to autofix text from command line without saving it to the filesystem.
                  Note that `fix-dry-run` is mutually exclusive with the `fix` option.

How about the above one?
I'm worried about only this option's description looks too verbose because the other options' descriptions are all single lines.
These descriptions are copy-pasted from https://eslint.org/docs/user-guide/command-line-interface#--fix-dry-run without the last sentense.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps "Preview fixed file contents" or "Print fixed file contents"?

Suggested change
--fix-dry-run Automatically fix problems without saving the changes to the file system
--fix-dry-run Preview fixed file contents

--reporter Reporter to use
--env Environment preset [Can be set multiple times]
--global Global variable [Can be set multiple times]
Expand Down Expand Up @@ -53,6 +54,9 @@ const cli = meow(`
fix: {
type: 'boolean',
},
fixDryRun: {
type: 'boolean',
},
reporter: {
type: 'string',
},
Expand Down
4 changes: 4 additions & 0 deletions lib/options-manager.js
Expand Up @@ -309,6 +309,10 @@ const buildESLintConfig = options => config => {
};
}

if (options.fixDryRun) {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
config.fix = true;
}

return {
...config,
...pick(options, ['cwd', 'filePath', 'fix']),
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -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]
Expand Down
15 changes: 15 additions & 0 deletions test/cli.js
Expand Up @@ -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',
Expand Down