Skip to content

Commit 0ae4289

Browse files
authored
fix(consistent-test-it): Remove duplicate imports inside the import statement (#638)
* refactor: execute lint:js * fix: remove duplicate imports inside the import statement
1 parent 0ee8457 commit 0ae4289

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Linter } from '@typescript-eslint/utils/ts-eslint'
2-
import type { ESLint } from "eslint"
2+
import type { ESLint } from 'eslint'
33
import { version } from '../package.json'
44
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
55
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
@@ -71,8 +71,8 @@ const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
7171
[`vitest/${ruleName}`]: rules[ruleName]
7272
}
7373
}, {})) as {
74-
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
75-
}
74+
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
75+
}
7676

7777
const createConfigLegacy = (rules: Record<string, string>) => ({
7878
plugins: ['@vitest'],
@@ -160,7 +160,6 @@ const recommended = {
160160
[noImportNodeTestName]: 'error'
161161
} as const
162162

163-
164163
const plugin = {
165164
meta: {
166165
name: 'vitest',

src/rules/consistent-test-it.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,21 @@ export default createEslintRule<
110110
node: specifier,
111111
data: { testFnKeyWork, oppositeTestKeyword },
112112
messageId: 'consistentMethod',
113-
fix: fixer => fixer.replaceText(
114-
specifier.local,
115-
testFnDisabled
116-
)
113+
fix: (fixer) => {
114+
const remainingSpecifiers = node.specifiers.filter(spec => spec.local.name !== oppositeTestKeyword)
115+
if (remainingSpecifiers.length > 0) {
116+
const importText = remainingSpecifiers.map(spec => spec.local.name).join(', ')
117+
const lastSpecifierRange = node.specifiers.at(-1)?.range
118+
if (!lastSpecifierRange) return null
119+
120+
return fixer.replaceTextRange(
121+
[node.specifiers[0].range[0], lastSpecifierRange[1]],
122+
importText
123+
)
124+
}
125+
126+
return fixer.replaceText(specifier.local, testFnDisabled)
127+
}
117128
})
118129
}
119130
}

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
KnownMemberExpression,
1111
ParsedExpectVitestFnCall
1212
} from './parse-vitest-fn-call'
13-
import { Rule } from "eslint"
13+
import { Rule } from 'eslint'
1414

1515
export interface PluginDocs {
1616
recommended?: boolean

tests/consistent-test-it.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ ruleTester.run(RULE_NAME, rule, {
8484
options: [{ fn: TestCaseName.it }],
8585
output: 'import { it } from "vitest"\nit("shows error", () => {});',
8686
errors: [{ messageId: 'consistentMethod' }, { messageId: 'consistentMethod' }]
87+
},
88+
{
89+
code: 'import { expect, test, it } from "vitest"\ntest("shows error", () => {});',
90+
options: [{ fn: TestCaseName.it }],
91+
output: 'import { expect, it } from "vitest"\nit("shows error", () => {});',
92+
errors: [
93+
{
94+
messageId: 'consistentMethod',
95+
data: {
96+
testFnKeyWork: TestCaseName.it,
97+
oppositeTestKeyword: TestCaseName.test
98+
},
99+
line: 1,
100+
column: 18,
101+
endColumn: 22
102+
},
103+
{
104+
messageId: 'consistentMethod',
105+
data: {
106+
testFnKeyWork: TestCaseName.it,
107+
oppositeTestKeyword: TestCaseName.test
108+
},
109+
line: 2,
110+
column: 1,
111+
endColumn: 5
112+
}
113+
]
87114
}
88115
]
89116
})
@@ -303,6 +330,35 @@ ruleTester.run(RULE_NAME, rule, {
303330
}
304331
]
305332
},
333+
{
334+
code: 'import { expect, it, test } from "vitest"\nit("foo")',
335+
output: 'import { expect, test } from "vitest"\ntest("foo")',
336+
options: [
337+
{ withinDescribe: TestCaseName.test }
338+
],
339+
errors: [
340+
{
341+
messageId: 'consistentMethod',
342+
data: {
343+
testFnKeyWork: TestCaseName.test,
344+
oppositeTestKeyword: TestCaseName.it
345+
},
346+
line: 1,
347+
column: 18,
348+
endColumn: 20
349+
},
350+
{
351+
messageId: 'consistentMethod',
352+
data: {
353+
testFnKeyWork: TestCaseName.test,
354+
oppositeTestKeyword: TestCaseName.it
355+
},
356+
line: 2,
357+
column: 1,
358+
endColumn: 3
359+
}
360+
]
361+
},
306362
{
307363
code: 'describe("suite", () => { it("foo") })',
308364
output: 'describe("suite", () => { test("foo") })',

0 commit comments

Comments
 (0)