Skip to content
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
31 changes: 28 additions & 3 deletions src/rules/prefer-describe-function-title.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AST_NODE_TYPES, ESLintUtils, TSESLint } from '@typescript-eslint/utils'
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
import { DefinitionType } from '@typescript-eslint/scope-manager'
import { createEslintRule } from '../utils'
import { parsePluginSettings } from '../utils/parse-plugin-settings'
import { parseVitestFnCall } from '../utils/parse-vitest-fn-call'
Expand Down Expand Up @@ -32,7 +33,32 @@ export default createEslintRule<Options, MESSAGE_IDS>({
return
}

const scope = getModuleScope(context, node)
const [argument] = node.arguments
if (
argument.type === AST_NODE_TYPES.MemberExpression &&
argument.object.type === AST_NODE_TYPES.Identifier &&
argument.property.type === AST_NODE_TYPES.Identifier
) {
const identifierName = argument.object.name
const scopedFunction = scope?.set.get(identifierName)?.defs[0]
if (
scopedFunction?.type !== DefinitionType.ImportBinding ||
argument.property.name !== 'name'
) {
return
}

context.report({
node: argument,
messageId: 'preferFunction',
fix(fixer) {
return fixer.replaceText(argument, identifierName)
},
})
return
}

if (
argument.type !== AST_NODE_TYPES.Literal ||
typeof argument.value !== 'string'
Expand All @@ -50,9 +76,8 @@ export default createEslintRule<Options, MESSAGE_IDS>({
return
}

const scope = getModuleScope(context, node)
const scopedFunction = scope?.set.get(describedTitle)?.defs[0]
if (scopedFunction?.type !== 'ImportBinding') {
if (scopedFunction?.type !== DefinitionType.ImportBinding) {
return
}

Expand Down
26 changes: 26 additions & 0 deletions tests/prefer-describe-function-title.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ ruleTester.run(RULE_NAME, rule, {
`,
filename: 'myFunction.test.ts',
},
{
code: `
import { myFunction } from "./myFunction.js"
describe(otherFunction.name, () => {})
`,
filename: 'myFunction.test.ts',
},
{
code: `
declare const myFunction: () => unknown
Expand Down Expand Up @@ -114,6 +121,25 @@ ruleTester.run(RULE_NAME, rule, {
describe(myFunction, () => {})
`,
},
{
code: `
import { myFunction } from "./myFunction"
describe(myFunction.name, () => {})
`,
errors: [
{
column: 18,
endColumn: 33,
line: 3,
messageId: 'preferFunction',
},
],
filename: 'myFunction.test.ts',
output: `
import { myFunction } from "./myFunction"
describe(myFunction, () => {})
`,
},
{
code: `
import { myFunction } from "./myFunction"
Expand Down