Skip to content

Commit 68d07e1

Browse files
committed
refactor: improve ESLint settings retrieval
1 parent 350d0ef commit 68d07e1

File tree

11 files changed

+85
-94
lines changed

11 files changed

+85
-94
lines changed

eslint.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ export default tseslint.config(
138138
files: [...GLOB_JS, ...GLOB_TS],
139139
rules: {
140140
eqeqeq: ["error", "always"],
141-
"max-depth": ["warn", 4],
142141
"no-console": "error",
143142
"no-else-return": "error",
144143
"no-fallthrough": ["error", { commentPattern: ".*intentional fallthrough.*" }],

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
},
5353
"devDependencies": {
5454
"@changesets/cli": "^2.27.11",
55-
"@effect/platform": "^0.72.0",
56-
"@effect/platform-bun": "^0.52.0",
57-
"@effect/platform-node": "^0.68.0",
55+
"@effect/platform": "^0.72.1",
56+
"@effect/platform-bun": "^0.52.1",
57+
"@effect/platform-node": "^0.68.1",
5858
"@effect/schema": "^0.75.5",
5959
"@eslint/config-inspector": "^0.7.0",
6060
"@eslint/js": "^9.17.0",
@@ -75,7 +75,7 @@
7575
"cspell": "^8.17.1",
7676
"dedent": "^1.5.3",
7777
"dprint": "^0.48.0",
78-
"effect": "^3.12.0",
78+
"effect": "^3.12.1",
7979
"esbuild": "^0.24.2",
8080
"eslint": "^9.17.0",
8181
"eslint-config-flat-gitignore": "^0.3.0",

packages/core/src/hook/is.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as AST from "@eslint-react/ast";
22
import { F, O } from "@eslint-react/eff";
3-
import { unsafeReadSettings } from "@eslint-react/shared";
3+
import { getSettingsFromContext } from "@eslint-react/shared";
44
import type { RuleContext } from "@eslint-react/types";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES } from "@typescript-eslint/types";
@@ -31,7 +31,7 @@ export function isReactHookCall(node: TSESTree.Node) {
3131
}
3232

3333
export function isReactHookCallWithName(node: TSESTree.CallExpression, context: RuleContext) {
34-
const settings = unsafeReadSettings(context.settings);
34+
const settings = getSettingsFromContext(context);
3535
return (name: string) => {
3636
const initialScope = context.sourceCode.getScope(node);
3737
switch (true) {
@@ -63,7 +63,7 @@ export function isReactHookCallWithNameLoose(node: TSESTree.CallExpression) {
6363
}
6464

6565
export function isReactHookCallWithNameAlias(name: string, context: RuleContext, alias: string[]) {
66-
const settings = unsafeReadSettings(context.settings);
66+
const settings = getSettingsFromContext(context);
6767
return (node: TSESTree.CallExpression) => {
6868
const initialScope = context.sourceCode.getScope(node);
6969
switch (true) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as AST from "@eslint-react/ast";
22
import { F } from "@eslint-react/eff";
3-
import { unsafeReadSettings } from "@eslint-react/shared";
3+
import { getSettingsFromContext } from "@eslint-react/shared";
44
import type { RuleContext } from "@eslint-react/types";
55
import type { TSESTree } from "@typescript-eslint/types";
66
import { AST_NODE_TYPES } from "@typescript-eslint/types";
@@ -17,7 +17,7 @@ export function isFromReact(name: string) {
1717
node: TSESTree.Identifier | TSESTree.MemberExpression,
1818
context: RuleContext,
1919
) => {
20-
const settings = unsafeReadSettings(context.settings);
20+
const settings = getSettingsFromContext(context);
2121
const initialScope = context.sourceCode.getScope(node);
2222
if (node.type === AST_NODE_TYPES.MemberExpression) {
2323
return node.object.type === AST_NODE_TYPES.Identifier
@@ -44,7 +44,7 @@ export function isFromReactMember(
4444
node: TSESTree.MemberExpression,
4545
context: RuleContext,
4646
) => {
47-
const settings = unsafeReadSettings(context.settings);
47+
const settings = getSettingsFromContext(context);
4848
const initialScope = context.sourceCode.getScope(node);
4949
if (node.property.type !== AST_NODE_TYPES.Identifier || node.property.name !== name) return false;
5050
if (node.object.type === AST_NODE_TYPES.Identifier && node.object.name === memberName) {

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
@@ -1,7 +1,7 @@
11
import * as AST from "@eslint-react/ast";
22
import { isCloneElementCall, isCreateElementCall, isInitializedFromReact } from "@eslint-react/core";
33
import { isNullable, O, or } from "@eslint-react/eff";
4-
import { unsafeReadSettings } from "@eslint-react/shared";
4+
import { getSettingsFromContext } from "@eslint-react/shared";
55
import type { RuleContext, RuleFeature } from "@eslint-react/types";
66
import { AST_NODE_TYPES } from "@typescript-eslint/types";
77
import type { TSESTree } from "@typescript-eslint/utils";
@@ -62,7 +62,7 @@ function isReactChildrenMethod(name: string): name is typeof reactChildrenMethod
6262
}
6363

6464
function isUsingReactChildren(node: TSESTree.CallExpression, context: RuleContext) {
65-
const settings = unsafeReadSettings(context.settings);
65+
const settings = getSettingsFromContext(context);
6666
const { callee } = node;
6767
if (!("property" in callee) || !("object" in callee) || !("name" in callee.property)) {
6868
return false;

packages/shared/docs/functions/getSettingsFromContext.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88

99
> **getSettingsFromContext**(`context`): `ESLintReactSettingsNormalized`
1010
11+
Get the normalized ESLint settings for "react-x" from the given context.
12+
1113
## Parameters
1214

1315
### context
1416

17+
The context.
18+
1519
#### settings
1620

1721
`unknown`
1822

23+
The ESLint settings.
24+
1925
## Returns
2026

2127
`ESLintReactSettingsNormalized`
28+
29+
The normalized ESLint settings.

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@typescript-eslint/utils": "^8.19.0",
4545
"picomatch": "^4.0.2",
4646
"ts-pattern": "^5.6.0",
47-
"valibot": "^1.0.0-beta.9"
47+
"valibot": "^1.0.0-beta.10"
4848
},
4949
"devDependencies": {
5050
"@types/picomatch": "^3.0.1",

packages/shared/src/settings.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { E, F } from "@eslint-react/eff";
22
import pm from "picomatch";
33
import { match, P } from "ts-pattern";
44
import type { PartialDeep } from "type-fest";
5-
import { parse } from "valibot";
5+
import { assert } from "valibot";
66

77
import { normalizedSettingsCache } from "./cache";
88
import { getReactVersion } from "./get-react-version";
@@ -22,30 +22,18 @@ export const DEFAULT_ESLINT_REACT_SETTINGS = {
2222
} as const satisfies ESLintReactSettings;
2323

2424
/**
25-
* Unsafely casts settings from a data object from `context.settings`.
26-
* @internal
27-
* @param data The data object.
28-
* @returns settings The settings.
25+
* Get the normalized ESLint settings for "react-x" from the given context.
26+
* @param context The context.
27+
* @param context.settings The ESLint settings.
28+
* @returns The normalized ESLint settings.
2929
*/
30-
export function unsafeReadSettings(data: unknown): PartialDeep<ESLintReactSettings> {
31-
// @ts-expect-error - skip type checking for unsafe cast
32-
// eslint-disable-next-line @susisu/safe-typescript/no-type-assertion
33-
return (data?.["react-x"] ?? {}) as PartialDeep<ESLintReactSettings>;
34-
}
35-
36-
/**
37-
* Normalizes the settings by converting all shorthand properties to their full form.
38-
* @param data The raw settings.
39-
* @returns The normalized settings.
40-
* @internal
41-
*/
42-
export function normalizeSettings(data: unknown): ESLintReactSettingsNormalized {
43-
const memoized = normalizedSettingsCache.get(data);
30+
export function getSettingsFromContext(context: { settings: unknown }): ESLintReactSettingsNormalized {
31+
const memoized = normalizedSettingsCache.get(context.settings);
4432
if (memoized) return memoized;
45-
33+
assert(ESLintSettingsSchema, context.settings);
4634
const settings = {
4735
...DEFAULT_ESLINT_REACT_SETTINGS,
48-
...parse(ESLintSettingsSchema, data)["react-x"] ?? {},
36+
...context.settings?.["react-x"],
4937
};
5038
const additionalComponents = settings.additionalComponents ?? [];
5139
const normalized = {
@@ -68,14 +56,10 @@ export function normalizeSettings(data: unknown): ESLintReactSettingsNormalized
6856
.with(P.union(P.nullish, "", "detect"), () => E.getOrElse(getReactVersion(), F.constant("19.0.0")))
6957
.otherwise(F.identity),
7058
};
71-
normalizedSettingsCache.set(data, normalized);
59+
normalizedSettingsCache.set(settings, normalized);
7260
return normalized;
7361
}
7462

75-
export function getSettingsFromContext(context: { settings: unknown }): ESLintReactSettingsNormalized {
76-
return normalizeSettings(context.settings);
77-
}
78-
7963
/**
8064
* A helper function to define settings for "react-x" with type checking in JavaScript files.
8165
* @param settings The settings.

packages/utilities/eff/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"@workspace/configs": "workspace:*",
44-
"effect": "^3.12.0",
44+
"effect": "^3.12.1",
4545
"tsup": "^8.3.5"
4646
},
4747
"engines": {

0 commit comments

Comments
 (0)