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 all 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
9 changes: 8 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,13 @@ module.exports = {
context.report({
node: test,
message,
fix (fixer) {
const start = sourceCode.getTokenBefore(test);
const end = sourceCode.getTokenAfter(test);
return fixer.removeRange(
// should remove test's trailing comma
[start.range[1], end.value === ',' ? end.range[1] : test.range[1]]);
},
});
} else {
cache[testCode] = true;
Expand Down
48 changes: 47 additions & 1 deletion tests/lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,42 @@ ruleTester.run('no-identical-tests', rule, {
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
{ code: 'bar' }
{ code: 'bar' },
],
invalid: []
});
`,
`
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' }
],
invalid: []
});
`,
],

invalid: [
{
code: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
{ code: 'foo' },
],
invalid: []
});
`,
errors: [ERROR],
output: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
],
invalid: []
});
`,
},
{
code: `
new RuleTester().run('foo', bar, {
Expand All @@ -44,6 +72,14 @@ ruleTester.run('no-identical-tests', rule, {
});
`,
errors: [ERROR],
output: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
],
invalid: []
});
`,
},
{
code: `
Expand All @@ -59,6 +95,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' }
]

},
],
});