Skip to content

Commit dc56d09

Browse files
authored
refactor(var): restructure the module (#1076)
1 parent c648894 commit dc56d09

File tree

16 files changed

+71
-87
lines changed

16 files changed

+71
-87
lines changed

.vscode/settings.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
}
3232
],
3333
"eslint.rules.customizations": [
34-
{ "rule": "@eslint-react/debug/*", "severity": "info" }
34+
{
35+
"rule": "@eslint-react/debug/*",
36+
"severity": "info"
37+
}
3538
],
3639
"files.exclude": {
3740
"**/.zed": true,
@@ -40,5 +43,5 @@
4043
"mdx.server.enable": true,
4144
"npm.packageManager": "pnpm",
4245
"typescript.preferences.importModuleSpecifier": "non-relative",
43-
"typescript.tsdk": "node_modules/typescript/lib"
44-
}
46+
"typescript.tsdk": "node_modules/typescript/lib",
47+
}

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-context-value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
6262
if (value?.type !== T.JSXExpressionContainer) return;
6363
const valueExpression = value.expression;
6464
const initialScope = context.sourceCode.getScope(valueExpression);
65-
const construction = VAR.getConstructionDetectionResult(valueExpression, initialScope);
65+
const construction = VAR.getConstruction(valueExpression, initialScope);
6666
if (construction == null) return;
6767
if (ER.isReactHookCall(construction.node)) {
6868
return;

packages/plugins/eslint-plugin-react-x/src/rules/no-unstable-default-props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
6767
const { value } = prop;
6868
const { right } = value;
6969
const initialScope = context.sourceCode.getScope(value);
70-
const construction = VAR.getConstructionDetectionResult(
70+
const construction = VAR.getConstruction(
7171
value,
7272
initialScope,
7373
VAR.ConstructionDetectionHint.StrictCallExpression,

packages/utilities/var/src/construction/construction-detection-hint.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/utilities/var/src/construction/construction.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/utilities/var/src/construction/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/utilities/var/src/find-variable.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/utilities/var/src/get-variables.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
export * from "./construction";
2-
export * from "./find-property-in-properties";
3-
export * from "./find-variable";
4-
export * from "./get-child-scopes";
5-
export * from "./get-variable-declarator-id";
6-
export * from "./get-variable-init-node";
7-
export * from "./get-variables";
8-
export * from "./is-node-value-equal";
9-
export * from "./lazy-value";
1+
export * from "./var-collect";
2+
export * from "./var-construction";
3+
export * from "./var-declarator-id";
4+
export * from "./var-init-node";
5+
export * from "./var-scope";
6+
export * from "./var-value";
7+
export * from "./var-value-equal";

packages/utilities/var/src/find-property-in-properties.ts renamed to packages/utilities/var/src/var-collect.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1-
import type { _ } from "@eslint-react/eff";
2-
import type { Scope } from "@typescript-eslint/scope-manager";
1+
import type { Scope, Variable } from "@typescript-eslint/scope-manager";
32
import type { TSESTree } from "@typescript-eslint/types";
3+
import { _, dual } from "@eslint-react/eff";
4+
import { ScopeType } from "@typescript-eslint/scope-manager";
45
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
6+
import * as ASTUtils from "@typescript-eslint/utils/ast-utils";
7+
import { getVariableInitNode } from "./var-init-node";
58

6-
import { findVariable } from "./find-variable";
7-
import { getVariableInitNode } from "./get-variable-init-node";
9+
/**
10+
* Get all variables from the given scope up to the global scope
11+
* @param initialScope The scope to start from
12+
* @returns All variables from the given scope up to the global scope
13+
*/
14+
export function getVariables(initialScope: Scope): Variable[] {
15+
let scope = initialScope;
16+
const variables = [...scope.variables];
17+
while (scope.type !== ScopeType.global) {
18+
scope = scope.upper;
19+
variables.push(...scope.variables);
20+
}
21+
return variables.reverse();
22+
}
23+
24+
export const findVariable: {
25+
(initialScope: Scope): (nameOrNode: string | TSESTree.Identifier | _) => Variable | _;
26+
(nameOrNode: string | TSESTree.Identifier | _, initialScope: Scope): Variable | _;
27+
} = dual(2, (nameOrNode: string | TSESTree.Identifier | _, initialScope: Scope) => {
28+
if (nameOrNode == null) return _;
29+
return ASTUtils.findVariable(initialScope, nameOrNode) ?? _;
30+
});
831

932
export function findPropertyInProperties(
1033
name: string,

0 commit comments

Comments
 (0)