Skip to content

Commit 0e4f43a

Browse files
committed
refactor: reorder parameters in 'isInitializedFromReact' function and update related usages
1 parent a9445ee commit 0e4f43a

File tree

7 files changed

+34
-29
lines changed

7 files changed

+34
-29
lines changed

packages/core/docs/functions/isInitializedFromReact.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
# Function: isInitializedFromReact()
88

9-
> **isInitializedFromReact**(`name`, `initialScope`, `importSource`): `boolean`
9+
> **isInitializedFromReact**(`name`, `importSource`, `initialScope`): `boolean`
1010
1111
## Parameters
1212

1313
### name
1414

1515
`string`
1616

17-
### initialScope
17+
### importSource
1818

19-
`Scope`
19+
`string`
2020

21-
### importSource
21+
### initialScope
2222

23-
`string` = `"react"`
23+
`Scope`
2424

2525
## Returns
2626

packages/core/src/hook/is.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,22 @@ export function isReactHookCall(node: TSESTree.Node | _) {
3636
export function isReactHookCallWithName(node: TSESTree.CallExpression | _, context: RuleContext) {
3737
if (node == null) return returnFalse;
3838
const settings = unsafeDecodeSettings(context.settings);
39+
const importSource = settings.importSource ?? "react";
40+
const initialScope = context.sourceCode.getScope(node);
3941
return (name: string) => {
40-
const initialScope = context.sourceCode.getScope(node);
4142
switch (true) {
4243
case node.callee.type === T.Identifier
4344
&& node.callee.name === name:
4445
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
4546
return !settings.strictImportCheck
46-
|| isInitializedFromReact(name, initialScope, settings.importSource);
47+
|| isInitializedFromReact(name, importSource, initialScope);
4748
case node.callee.type === T.MemberExpression
4849
&& node.callee.property.type === T.Identifier
4950
&& node.callee.property.name === name
5051
&& "name" in node.callee.object:
5152
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
5253
return !settings.strictImportCheck
53-
|| isInitializedFromReact(node.callee.object.name, initialScope, settings.importSource);
54+
|| isInitializedFromReact(node.callee.object.name, importSource, initialScope);
5455
default:
5556
return false;
5657
}
@@ -73,21 +74,22 @@ export function isReactHookCallWithNameLoose(node: TSESTree.CallExpression | _)
7374

7475
export function isReactHookCallWithNameAlias(name: string, context: RuleContext, alias: string[]) {
7576
const settings = unsafeDecodeSettings(context);
77+
const importSource = settings.importSource ?? "react";
7678
return (node: TSESTree.CallExpression) => {
7779
const initialScope = context.sourceCode.getScope(node);
7880
switch (true) {
7981
case node.callee.type === T.Identifier
8082
&& node.callee.name === name:
8183
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
8284
return !settings.strictImportCheck
83-
|| isInitializedFromReact(name, initialScope, settings.importSource);
85+
|| isInitializedFromReact(name, importSource, initialScope);
8486
case node.callee.type === T.MemberExpression
8587
&& node.callee.property.type === T.Identifier
8688
&& node.callee.property.name === name
8789
&& "name" in node.callee.object:
8890
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
8991
return !settings.strictImportCheck
90-
|| isInitializedFromReact(node.callee.object.name, initialScope, settings.importSource);
92+
|| isInitializedFromReact(node.callee.object.name, importSource, initialScope);
9193
default:
9294
return alias.some(isReactHookCallWithNameLoose(node));
9395
}

packages/core/src/utils/is-from-react.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as AST from "@eslint-react/ast";
22
import { dual } from "@eslint-react/eff";
33
import type { RuleContext } from "@eslint-react/shared";
4-
import { unsafeDecodeSettings } from "@eslint-react/shared";
4+
import { DEFAULT_ESLINT_REACT_SETTINGS, unsafeDecodeSettings } from "@eslint-react/shared";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
77

@@ -27,15 +27,16 @@ export function isFromReact(name: string) {
2727
}
2828
return node.name === name;
2929
}
30+
const importSource = settings.importSource ?? DEFAULT_ESLINT_REACT_SETTINGS.importSource;
3031
const initialScope = context.sourceCode.getScope(node);
3132
if (node.type === T.MemberExpression) {
3233
return node.object.type === T.Identifier
3334
&& node.property.type === T.Identifier
3435
&& node.property.name === name
35-
&& isInitializedFromReact(node.object.name, initialScope, settings.importSource);
36+
&& isInitializedFromReact(node.object.name, importSource, initialScope);
3637
}
3738
if (node.name === name) {
38-
return isInitializedFromReact(name, initialScope, settings.importSource);
39+
return isInitializedFromReact(name, importSource, initialScope);
3940
}
4041
return false;
4142
};
@@ -73,17 +74,18 @@ export function isFromReactMember(
7374
}
7475
return false;
7576
}
77+
const importSource = settings.importSource ?? DEFAULT_ESLINT_REACT_SETTINGS.importSource;
7678
const initialScope = context.sourceCode.getScope(node);
7779
if (node.property.type !== T.Identifier || node.property.name !== name) {
7880
return false;
7981
}
8082
if (node.object.type === T.Identifier && node.object.name === memberName) {
81-
return isInitializedFromReact(node.object.name, initialScope, settings.importSource);
83+
return isInitializedFromReact(node.object.name, importSource, initialScope);
8284
}
8385
if (
8486
node.object.type === T.MemberExpression
8587
&& node.object.object.type === T.Identifier
86-
&& isInitializedFromReact(node.object.object.name, initialScope, settings.importSource)
88+
&& isInitializedFromReact(node.object.object.name, importSource, initialScope)
8789
&& node.object.property.type === T.Identifier
8890
) {
8991
return node.object.property.name === memberName;

packages/core/src/utils/is-initialized-from-react.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { Scope } from "@typescript-eslint/scope-manager";
33

44
export function isInitializedFromReact(
55
name: string,
6+
importSource: string,
67
initialScope: Scope,
7-
importSource = "react",
88
) {
99
return name.toLowerCase() === "react"
1010
|| VAR.isInitializedFromSource(

packages/plugins/eslint-plugin-react-debug/src/rules/is-from-react.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ export default createRule<[], MessageID>({
3131
},
3232
name: RULE_NAME,
3333
create(context) {
34-
const settings = {
35-
importSource: "react",
36-
...getSettingsFromContext(context),
37-
strictImportCheck: true,
38-
};
34+
const { importSource = "react" } = getSettingsFromContext(context);
35+
3936
function isFromReact(
4037
node: TSESTree.Identifier | TSESTree.JSXIdentifier,
4138
initialScope: Scope,
@@ -45,13 +42,13 @@ export default createRule<[], MessageID>({
4542
case node.parent.type === T.MemberExpression
4643
&& node.parent.property === node
4744
&& node.parent.object.type === T.Identifier:
48-
return isInitializedFromReact(node.parent.object.name, initialScope, settings.importSource);
45+
return isInitializedFromReact(node.parent.object.name, importSource, initialScope);
4946
case node.parent.type === T.JSXMemberExpression
5047
&& node.parent.property === node
5148
&& node.parent.object.type === T.JSXIdentifier:
52-
return isInitializedFromReact(node.parent.object.name, initialScope, settings.importSource);
49+
return isInitializedFromReact(node.parent.object.name, importSource, initialScope);
5350
default:
54-
return isInitializedFromReact(name, initialScope, settings.importSource);
51+
return isInitializedFromReact(name, importSource, initialScope);
5552
}
5653
}
5754
function visitorFunction(node: TSESTree.Identifier | TSESTree.JSXIdentifier) {
@@ -73,7 +70,7 @@ export default createRule<[], MessageID>({
7370
// eslint-disable-next-line eslint-plugin/no-unused-placeholders
7471
type: node.type,
7572
name,
76-
importSource: settings.importSource,
73+
importSource,
7774
},
7875
});
7976
}

packages/plugins/eslint-plugin-react-x/src/rules/no-array-index-key.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function isReactChildrenMethod(name: string): name is typeof reactChildrenMethod
5151
}
5252

5353
function isUsingReactChildren(node: TSESTree.CallExpression, context: RuleContext) {
54-
const settings = unsafeDecodeSettings(context.settings);
54+
const { importSource = "react" } = unsafeDecodeSettings(context.settings);
5555
const { callee } = node;
5656
if (!("property" in callee) || !("object" in callee) || !("name" in callee.property)) {
5757
return false;
@@ -64,7 +64,7 @@ function isUsingReactChildren(node: TSESTree.CallExpression, context: RuleContex
6464
return true;
6565
}
6666
if (callee.object.type === T.MemberExpression && "name" in callee.object.object) {
67-
return isInitializedFromReact(callee.object.object.name, initialScope, settings.importSource);
67+
return isInitializedFromReact(callee.object.object.name, importSource, initialScope);
6868
}
6969
return false;
7070
}

packages/shared/src/settings.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export interface CustomComponentPropNormalized {
3434
export interface ESLintReactSettingsNormalized {
3535
additionalComponents: CustomComponentNormalized[];
3636
additionalHooks: CustomHooks;
37-
importSource?: string;
38-
polymorphicPropName?: string | _;
37+
importSource: string;
38+
polymorphicPropName: string | _;
3939
// strict: boolean;
4040
strictImportCheck: boolean;
4141
version: string;
@@ -76,6 +76,8 @@ export const toNormalizedSettings = memoize(
7676
({
7777
additionalComponents = [],
7878
additionalHooks = {},
79+
importSource = "react",
80+
polymorphicPropName = "as",
7981
version,
8082
...rest
8183
}: ESLintReactSettings): ESLintReactSettingsNormalized => {
@@ -99,6 +101,8 @@ export const toNormalizedSettings = memoize(
99101
re: pm.makeRe(name, { fastpaths: true }),
100102
})),
101103
additionalHooks,
104+
importSource,
105+
polymorphicPropName,
102106
version: match(version)
103107
.with(P.union(P.nullish, "", "detect"), () => getReactVersion("19.0.0"))
104108
.otherwise(identity),

0 commit comments

Comments
 (0)