Skip to content
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

fix(consistent-test-it): Remove duplicate imports inside the import statement #638

Merged
merged 2 commits into from
Jan 12, 2025
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
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