Skip to content

Commit 7df25f3

Browse files
committed
refactor: streamline init expression retrieval in 'no-leaked-conditional-rendering' rule
1 parent 9d370df commit 7df25f3

File tree

1 file changed

+4
-24
lines changed

1 file changed

+4
-24
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-leaked-conditional-rendering.ts

+4-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as AST from "@eslint-react/ast";
2-
import { _ } from "@eslint-react/eff";
2+
import { _, identity } from "@eslint-react/eff";
33
import type { RuleFeature } from "@eslint-react/shared";
44
import { getSettingsFromContext } from "@eslint-react/shared";
55
import * as VAR from "@eslint-react/var";
6-
import type { Variable } from "@typescript-eslint/scope-manager";
76
import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils";
87
import type { TSESTree } from "@typescript-eslint/types";
98
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
@@ -171,27 +170,6 @@ function inspectVariantTypes(types: ts.Type[]) {
171170
return variantTypes;
172171
}
173172

174-
function isInitExpression(
175-
node:
176-
| _
177-
| null
178-
| TSESTree.Expression
179-
| TSESTree.LetOrConstOrVarDeclaration,
180-
): node is TSESTree.Expression {
181-
if (node == null) return false;
182-
return node.type !== T.VariableDeclaration;
183-
}
184-
185-
function getVariableInitExpression(variable: Variable | _, at: number): TSESTree.Expression | _ {
186-
const def = variable?.defs[at];
187-
if (def?.node == null || !("init" in def.node)) {
188-
return _;
189-
}
190-
return isInitExpression(def.node.init)
191-
? def.node.init
192-
: _;
193-
}
194-
195173
// #endregion
196174

197175
// #region Rule Implementation
@@ -274,7 +252,9 @@ export default createRule<[], MessageID>({
274252
})
275253
.with({ type: T.Identifier }, (n) => {
276254
const variable = VAR.findVariable(n.name, context.sourceCode.getScope(n));
277-
const initExpression = getVariableInitExpression(variable, 0);
255+
const initExpression = match(variable?.defs.at(0)?.node)
256+
.with({ init: P.select({ type: P.not(T.VariableDeclaration) }) }, identity)
257+
.otherwise(() => _);
278258
return getReportDescriptor(initExpression);
279259
})
280260
.otherwise(() => _);

0 commit comments

Comments
 (0)