Skip to content

Commit 54be66e

Browse files
authored
fix(consistent-test-it): Handle aliased Vitest imports when enforcing consistent test names (#828)
* test: add test * fix: improve import specifier handling in fixer
1 parent cc5fcdb commit 54be66e

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

src/rules/consistent-test-it.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export default createEslintRule<
8686
const testKeywordWithinDescribe = withinDescribe || fn || TestCaseName.it
8787
const testFnDisabled =
8888
testFnKeyWork === testKeywordWithinDescribe ? testFnKeyWork : undefined
89+
const { sourceCode } = context
8990

9091
let describeNestingLevel = 0
9192

@@ -99,33 +100,43 @@ export default createEslintRule<
99100
for (const specifier of node.specifiers) {
100101
if (specifier.type !== 'ImportSpecifier') continue
101102
if (specifier.imported.type !== 'Identifier') continue
102-
if (specifier.local.name !== specifier.imported.name) continue
103-
if (specifier.local.name === oppositeTestKeyword) {
104-
context.report({
105-
node: specifier,
106-
data: { testFnKeyWork, oppositeTestKeyword },
107-
messageId: 'consistentMethod',
108-
fix: (fixer) => {
109-
const remainingSpecifiers = node.specifiers.filter(
110-
(spec) => spec.local.name !== oppositeTestKeyword,
103+
if (specifier.imported.name !== oppositeTestKeyword) continue
104+
105+
context.report({
106+
node: specifier,
107+
data: { testFnKeyWork, oppositeTestKeyword },
108+
messageId: 'consistentMethod',
109+
fix: (fixer) => {
110+
const remainingSpecifiers = node.specifiers.filter(
111+
(spec) => spec !== specifier,
112+
)
113+
if (remainingSpecifiers.length > 0) {
114+
const hasPreferredSpecifier = remainingSpecifiers.some(
115+
(spec) =>
116+
spec.type === AST_NODE_TYPES.ImportSpecifier &&
117+
spec.imported.type === AST_NODE_TYPES.Identifier &&
118+
spec.imported.name === testFnDisabled,
119+
)
120+
const importNames = remainingSpecifiers.map((spec) =>
121+
sourceCode.getText(spec),
111122
)
112-
if (remainingSpecifiers.length > 0) {
113-
const importText = remainingSpecifiers
114-
.map((spec) => spec.local.name)
115-
.join(', ')
116-
const lastSpecifierRange = node.specifiers.at(-1)?.range
117-
if (!lastSpecifierRange) return null
118-
119-
return fixer.replaceTextRange(
120-
[node.specifiers[0].range[0], lastSpecifierRange[1]],
121-
importText,
122-
)
123+
if (!hasPreferredSpecifier) {
124+
importNames.push(testFnDisabled)
123125
}
124126

125-
return fixer.replaceText(specifier.local, testFnDisabled)
126-
},
127-
})
128-
}
127+
const importText = importNames.join(', ')
128+
const lastSpecifierRange = node.specifiers.at(-1)?.range
129+
if (!lastSpecifierRange) return null
130+
131+
return fixer.replaceTextRange(
132+
[node.specifiers[0].range[0], lastSpecifierRange[1]],
133+
importText,
134+
)
135+
}
136+
137+
return fixer.replaceText(specifier, testFnDisabled)
138+
},
139+
})
129140
}
130141
},
131142
CallExpression(node: TSESTree.CallExpression) {

tests/consistent-test-it.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,36 @@ ruleTester.run(RULE_NAME, rule, {
322322
testFnKeyWork: TestCaseName.test,
323323
oppositeTestKeyword: TestCaseName.it,
324324
},
325+
line: 1,
326+
column: 10,
327+
endColumn: 12,
325328
},
326329
{
327330
messageId: 'consistentMethod',
328331
data: {
329332
testFnKeyWork: TestCaseName.test,
330333
oppositeTestKeyword: TestCaseName.it,
331334
},
335+
line: 2,
336+
column: 1,
337+
endColumn: 3,
338+
},
339+
],
340+
},
341+
{
342+
code: 'import { it as baseIt, test } from "vitest"\nbaseIt("foo")',
343+
output: 'import { it as baseIt } from "vitest"\nbaseIt("foo")',
344+
options: [{ fn: TestCaseName.it }],
345+
errors: [
346+
{
347+
messageId: 'consistentMethod',
348+
data: {
349+
testFnKeyWork: TestCaseName.it,
350+
oppositeTestKeyword: TestCaseName.test,
351+
},
352+
column: 24,
353+
endColumn: 28,
354+
line: 1,
332355
},
333356
],
334357
},

0 commit comments

Comments
 (0)