Skip to content

Commit 8110bfa

Browse files
authored
feat(require-test-timeout): support ancestor traversal (#890)
1 parent 123a9c4 commit 8110bfa

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/rules/require-test-timeout.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Scope } from '@typescript-eslint/scope-manager'
12
import { createEslintRule, getAccessorValue } from '../utils'
23
import { parseVitestFnCall } from '../utils/parse-vitest-fn-call'
34
import { getScope } from '../utils/scope'
@@ -74,8 +75,19 @@ export default createEslintRule<Options, MESSAGE_ID>({
7475
}
7576

7677
if (node.type === AST_NODE_TYPES.Identifier) {
77-
const scope = getScope(context, node)
78-
const variable = scope.set.get(node.name)
78+
let currentScope: Scope | null = getScope(context, node)
79+
let variable
80+
81+
// Walk parent scopes until we find a definition for this identifier
82+
while (currentScope !== null) {
83+
const ref = currentScope.set.get(node.name)
84+
if (ref && ref.defs && ref.defs.length > 0) {
85+
variable = ref
86+
break
87+
}
88+
currentScope = currentScope.upper
89+
}
90+
7991
if (!variable || !variable.defs || variable.defs.length === 0)
8092
return undefined
8193

tests/require-test-timeout.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ ruleTester.run(RULE_NAME, rule, {
3333
// both object and numeric timeout present
3434
'test("a", { timeout: 500 }, 1000, () => {})',
3535
'test("a", () => {}, 1000, { extra: true })',
36+
// in-source unit tests
37+
'if (import.meta.vitest) { const opts = { timeout: 500 }; describe("outer", () => { it("repro: same-file opts object", opts, () => {}); }); }',
38+
'if (import.meta.vitest) { const T = 500; describe("outer", () => { describe("inner", () => { it("repro: same-file const timeout", () => {}, T); }); }); }',
3639
],
3740
invalid: [
3841
{

0 commit comments

Comments
 (0)