Skip to content

Commit 5f1fe04

Browse files
authored
feat(prefer-describe-function-title): Check for unnecessary .name (#744)
* feat: report unnecessary `.name` usage in prefer-describe-function-title * test: add tests * refactor: use DefinitionType for type checks
1 parent d48741a commit 5f1fe04

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

src/rules/prefer-describe-function-title.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AST_NODE_TYPES, ESLintUtils, TSESLint } from '@typescript-eslint/utils'
1+
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
2+
import { DefinitionType } from '@typescript-eslint/scope-manager'
23
import { createEslintRule } from '../utils'
34
import { parsePluginSettings } from '../utils/parse-plugin-settings'
45
import { parseVitestFnCall } from '../utils/parse-vitest-fn-call'
@@ -32,7 +33,32 @@ export default createEslintRule<Options, MESSAGE_IDS>({
3233
return
3334
}
3435

36+
const scope = getModuleScope(context, node)
3537
const [argument] = node.arguments
38+
if (
39+
argument.type === AST_NODE_TYPES.MemberExpression &&
40+
argument.object.type === AST_NODE_TYPES.Identifier &&
41+
argument.property.type === AST_NODE_TYPES.Identifier
42+
) {
43+
const identifierName = argument.object.name
44+
const scopedFunction = scope?.set.get(identifierName)?.defs[0]
45+
if (
46+
scopedFunction?.type !== DefinitionType.ImportBinding ||
47+
argument.property.name !== 'name'
48+
) {
49+
return
50+
}
51+
52+
context.report({
53+
node: argument,
54+
messageId: 'preferFunction',
55+
fix(fixer) {
56+
return fixer.replaceText(argument, identifierName)
57+
},
58+
})
59+
return
60+
}
61+
3662
if (
3763
argument.type !== AST_NODE_TYPES.Literal ||
3864
typeof argument.value !== 'string'
@@ -50,9 +76,8 @@ export default createEslintRule<Options, MESSAGE_IDS>({
5076
return
5177
}
5278

53-
const scope = getModuleScope(context, node)
5479
const scopedFunction = scope?.set.get(describedTitle)?.defs[0]
55-
if (scopedFunction?.type !== 'ImportBinding') {
80+
if (scopedFunction?.type !== DefinitionType.ImportBinding) {
5681
return
5782
}
5883

tests/prefer-describe-function-title.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ ruleTester.run(RULE_NAME, rule, {
7575
`,
7676
filename: 'myFunction.test.ts',
7777
},
78+
{
79+
code: `
80+
import { myFunction } from "./myFunction.js"
81+
describe(otherFunction.name, () => {})
82+
`,
83+
filename: 'myFunction.test.ts',
84+
},
7885
{
7986
code: `
8087
declare const myFunction: () => unknown
@@ -114,6 +121,25 @@ ruleTester.run(RULE_NAME, rule, {
114121
describe(myFunction, () => {})
115122
`,
116123
},
124+
{
125+
code: `
126+
import { myFunction } from "./myFunction"
127+
describe(myFunction.name, () => {})
128+
`,
129+
errors: [
130+
{
131+
column: 18,
132+
endColumn: 33,
133+
line: 3,
134+
messageId: 'preferFunction',
135+
},
136+
],
137+
filename: 'myFunction.test.ts',
138+
output: `
139+
import { myFunction } from "./myFunction"
140+
describe(myFunction, () => {})
141+
`,
142+
},
117143
{
118144
code: `
119145
import { myFunction } from "./myFunction"

0 commit comments

Comments
 (0)