Skip to content

add --fix to no-identical-tests. #13

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

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Name | ✔️ | 🛠 | Description
[prefer-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-placeholders.md) | | | Disallows template literals as report messages
[no-useless-token-range](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-useless-token-range.md) | ✔️ | 🛠 | Disallows unnecessary calls to sourceCode.getFirstToken and sourceCode.getLastToken
[consistent-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/consistent-output.md) | | | Enforces consistent use of output assertions in rule tests
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | | Disallows identical tests
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | 🛠 | Disallows identical tests

## Supported Presets

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-identical-tests.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow identical tests (no-identical-tests)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties.

## Rule Details
Expand Down
10 changes: 9 additions & 1 deletion lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
category: 'Tests',
recommended: false,
},
fixable: null, // or "code" or "whitespace"
fixable: 'code',
schema: [],
},

Expand All @@ -41,6 +41,14 @@ module.exports = {
context.report({
node: test,
message,
fix (fixer) {
const start = sourceCode.getTokenBefore(test);
const end = sourceCode.getTokenAfter(test);
return fixer.replaceTextRange(
Copy link
Contributor

@not-an-aardvark not-an-aardvark Jul 9, 2017

Choose a reason for hiding this comment

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

Nitpick: This can be fixer.removeRange instead of fixer.replaceTextRange.

Actually, maybe that would be a good rule to add to this plugin. 😄

// should remove test's trailing comma
[start.range[1], end.range[end.value === ',' ? 1 : 0]]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will handle whitespace differently depending on whether there is a comma. If there is no comma, then all the whitespace up to the next test will be removed. If there is a comma, the whitespace up to the next test will be preserved.

Maybe it would be better to be consistent about handling whitespace.

, '');
},
});
} else {
cache[testCode] = true;
Expand Down
22 changes: 20 additions & 2 deletions tests/lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ruleTester.run('no-identical-tests', rule, {
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
{ code: 'bar' }
{ code: 'bar' },
],
invalid: []
});
Expand All @@ -38,12 +38,20 @@ ruleTester.run('no-identical-tests', rule, {
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
{ code: 'foo' }
{ code: 'foo' },
],
invalid: []
});
`,
errors: [ERROR],
output: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
],
invalid: []
});
`,
},
{
code: `
Expand All @@ -59,6 +67,16 @@ ruleTester.run('no-identical-tests', rule, {
});
`,
errors: [ERROR, ERROR],
output: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
],
invalid: [
{ code: 'foo', errors: ['bar'] },
]
});
`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test where there is no trailing comma?

valid: [
  { code: 'foo' }
]

},
],
});