Skip to content

Commit ca607c4

Browse files
authored
Update: add --fix to no-identical-tests. (#13)
* update: tests. * Fix: should remove test's trailing comma * docs: no-identical-tests fixable * Fix: consistent about handling whitespace & add tests. * Update: use removeRange.
1 parent b41a2b8 commit ca607c4

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Name | ✔️ | 🛠 | Description
5656
[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
5757
[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
5858
[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
59-
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | | Disallows identical tests
59+
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | 🛠 | Disallows identical tests
6060

6161
## Supported Presets
6262

docs/rules/no-identical-tests.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow identical tests (no-identical-tests)
22

3+
(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.
4+
35
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.
46

57
## Rule Details

lib/rules/no-identical-tests.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
category: 'Tests',
1919
recommended: false,
2020
},
21-
fixable: null, // or "code" or "whitespace"
21+
fixable: 'code',
2222
schema: [],
2323
},
2424

@@ -41,6 +41,13 @@ module.exports = {
4141
context.report({
4242
node: test,
4343
message,
44+
fix (fixer) {
45+
const start = sourceCode.getTokenBefore(test);
46+
const end = sourceCode.getTokenAfter(test);
47+
return fixer.removeRange(
48+
// should remove test's trailing comma
49+
[start.range[1], end.value === ',' ? end.range[1] : test.range[1]]);
50+
},
4451
});
4552
} else {
4653
cache[testCode] = true;

tests/lib/rules/no-identical-tests.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,42 @@ ruleTester.run('no-identical-tests', rule, {
2525
new RuleTester().run('foo', bar, {
2626
valid: [
2727
{ code: 'foo' },
28-
{ code: 'bar' }
28+
{ code: 'bar' },
29+
],
30+
invalid: []
31+
});
32+
`,
33+
`
34+
new RuleTester().run('foo', bar, {
35+
valid: [
36+
{ code: 'foo' }
2937
],
3038
invalid: []
3139
});
3240
`,
3341
],
3442

3543
invalid: [
44+
{
45+
code: `
46+
new RuleTester().run('foo', bar, {
47+
valid: [
48+
{ code: 'foo' },
49+
{ code: 'foo' },
50+
],
51+
invalid: []
52+
});
53+
`,
54+
errors: [ERROR],
55+
output: `
56+
new RuleTester().run('foo', bar, {
57+
valid: [
58+
{ code: 'foo' },
59+
],
60+
invalid: []
61+
});
62+
`,
63+
},
3664
{
3765
code: `
3866
new RuleTester().run('foo', bar, {
@@ -44,6 +72,14 @@ ruleTester.run('no-identical-tests', rule, {
4472
});
4573
`,
4674
errors: [ERROR],
75+
output: `
76+
new RuleTester().run('foo', bar, {
77+
valid: [
78+
{ code: 'foo' },
79+
],
80+
invalid: []
81+
});
82+
`,
4783
},
4884
{
4985
code: `
@@ -59,6 +95,16 @@ ruleTester.run('no-identical-tests', rule, {
5995
});
6096
`,
6197
errors: [ERROR, ERROR],
98+
output: `
99+
new RuleTester().run('foo', bar, {
100+
valid: [
101+
{ code: 'foo' },
102+
],
103+
invalid: [
104+
{ code: 'foo', errors: ['bar'] },
105+
]
106+
});
107+
`,
62108
},
63109
],
64110
});

0 commit comments

Comments
 (0)