Skip to content

Commit

Permalink
fix(consistent-test-it): Remove duplicate imports inside the import s…
Browse files Browse the repository at this point in the history
…tatement (#638)

* refactor:  execute lint:js

* fix: remove duplicate imports inside the import statement
  • Loading branch information
y-hsgw authored Jan 12, 2025
1 parent 0ee8457 commit 0ae4289
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint'
import type { ESLint } from "eslint"
import type { ESLint } from 'eslint'
import { version } from '../package.json'
import lowerCaseTitle, { RULE_NAME as lowerCaseTitleName } from './rules/prefer-lowercase-title'
import maxNestedDescribe, { RULE_NAME as maxNestedDescribeName } from './rules/max-nested-describe'
Expand Down Expand Up @@ -71,8 +71,8 @@ const createConfig = <R extends Linter.RulesRecord>(rules: R) => (
[`vitest/${ruleName}`]: rules[ruleName]
}
}, {})) as {
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}
[K in keyof R as `vitest/${Extract<K, string>}`]: R[K]
}

const createConfigLegacy = (rules: Record<string, string>) => ({
plugins: ['@vitest'],
Expand Down Expand Up @@ -160,7 +160,6 @@ const recommended = {
[noImportNodeTestName]: 'error'
} as const


const plugin = {
meta: {
name: 'vitest',
Expand Down
19 changes: 15 additions & 4 deletions src/rules/consistent-test-it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,21 @@ export default createEslintRule<
node: specifier,
data: { testFnKeyWork, oppositeTestKeyword },
messageId: 'consistentMethod',
fix: fixer => fixer.replaceText(
specifier.local,
testFnDisabled
)
fix: (fixer) => {
const remainingSpecifiers = node.specifiers.filter(spec => spec.local.name !== oppositeTestKeyword)
if (remainingSpecifiers.length > 0) {
const importText = remainingSpecifiers.map(spec => spec.local.name).join(', ')
const lastSpecifierRange = node.specifiers.at(-1)?.range
if (!lastSpecifierRange) return null

return fixer.replaceTextRange(
[node.specifiers[0].range[0], lastSpecifierRange[1]],
importText
)
}

return fixer.replaceText(specifier.local, testFnDisabled)
}
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
KnownMemberExpression,
ParsedExpectVitestFnCall
} from './parse-vitest-fn-call'
import { Rule } from "eslint"
import { Rule } from 'eslint'

export interface PluginDocs {
recommended?: boolean
Expand Down
56 changes: 56 additions & 0 deletions tests/consistent-test-it.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ ruleTester.run(RULE_NAME, rule, {
options: [{ fn: TestCaseName.it }],
output: 'import { it } from "vitest"\nit("shows error", () => {});',
errors: [{ messageId: 'consistentMethod' }, { messageId: 'consistentMethod' }]
},
{
code: 'import { expect, test, it } from "vitest"\ntest("shows error", () => {});',
options: [{ fn: TestCaseName.it }],
output: 'import { expect, it } from "vitest"\nit("shows error", () => {});',
errors: [
{
messageId: 'consistentMethod',
data: {
testFnKeyWork: TestCaseName.it,
oppositeTestKeyword: TestCaseName.test
},
line: 1,
column: 18,
endColumn: 22
},
{
messageId: 'consistentMethod',
data: {
testFnKeyWork: TestCaseName.it,
oppositeTestKeyword: TestCaseName.test
},
line: 2,
column: 1,
endColumn: 5
}
]
}
]
})
Expand Down Expand Up @@ -303,6 +330,35 @@ ruleTester.run(RULE_NAME, rule, {
}
]
},
{
code: 'import { expect, it, test } from "vitest"\nit("foo")',
output: 'import { expect, test } from "vitest"\ntest("foo")',
options: [
{ withinDescribe: TestCaseName.test }
],
errors: [
{
messageId: 'consistentMethod',
data: {
testFnKeyWork: TestCaseName.test,
oppositeTestKeyword: TestCaseName.it
},
line: 1,
column: 18,
endColumn: 20
},
{
messageId: 'consistentMethod',
data: {
testFnKeyWork: TestCaseName.test,
oppositeTestKeyword: TestCaseName.it
},
line: 2,
column: 1,
endColumn: 3
}
]
},
{
code: 'describe("suite", () => { it("foo") })',
output: 'describe("suite", () => { test("foo") })',
Expand Down

0 comments on commit 0ae4289

Please sign in to comment.