Skip to content

Commit 2ed07d6

Browse files
committed
fix(review): address PR feedback on isExpectCall and chain-order docs
- Replace the isUnresolved-based fallback in isExpectCall with a narrower isGlobalExpect check (no variable, or a variable with no declarations) to avoid misclassifying a shadowed local `expect` (e.g. a function parameter) as the framework global. - Fix collectCallChain's JSDoc: segments are collected furthest from the root call first, not "innermost first".
1 parent 5c423f2 commit 2ed07d6

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

packages/analysis/src/jsts/rules/S8967/rule.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import type { Rule } from 'eslint';
2020
import type estree from 'estree';
21-
import { isIdentifier, isMethodCall, isUnresolved } from '../helpers/ast.js';
21+
import { getVariableFromName, isIdentifier, isMethodCall } from '../helpers/ast.js';
2222
import { collectCallChain, getRootCall } from '../helpers/expect-call-chain.js';
2323
import { generateMeta } from '../helpers/generate-meta.js';
2424
import { getFullyQualifiedName, importsOrDependsOnModule } from '../helpers/module.js';
@@ -128,6 +128,11 @@ function isExpectCall(
128128
return (
129129
(frameworks.jest || frameworks.vitest) &&
130130
isIdentifier(call.callee, 'expect') &&
131-
isUnresolved(call.callee, context)
131+
isGlobalExpect(context, call.callee)
132132
);
133133
}
134+
135+
function isGlobalExpect(context: Rule.RuleContext, node: estree.Identifier): boolean {
136+
const variable = getVariableFromName(context, node.name, node);
137+
return !variable || variable.defs.length === 0;
138+
}

packages/analysis/src/jsts/rules/helpers/expect-call-chain.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export function unwrapChainExpression<T extends estree.Node | estree.Super>(
3030
}
3131

3232
/**
33-
* Collects the chain of member names hung off `call`, innermost first, e.g. for
34-
* `expect(x).not.resolves.toBe(y)` called with the `toBe(y)` node, returns
33+
* Collects the chain of member names hung off `call`, furthest from the root
34+
* call first, e.g. for `expect(x).not.resolves.toBe(y)` called with the `toBe(y)`
35+
* node, returns
3536
* `{ segments: [{name: 'toBe', ...}, {name: 'resolves', ...}, {name: 'not', ...}], complete: true }`.
3637
* `complete` is `false` when a computed member or non-identifier property was hit
3738
* before reaching the root call, in which case `segments` only holds the

0 commit comments

Comments
 (0)