Skip to content

Commit

Permalink
refactor: update the default behavior of import check, closes #858 (#860
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Rel1cx authored Nov 19, 2024
1 parent 1a597ba commit 7cfbadc
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/core/docs/functions/isInitializedFromReact.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The top-level identifier's name

Initial scope to search for the identifier

**settings**: `Partial`\<`Pick`\<`object`, `"importSource"` \| `"skipImportCheck"`\>\>
**settings**: `Partial`\<`Pick`\<`object`, `"importSource"` \| `"strictImportCheck"`\>\>

ESLint React settings

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/is-initialized-from-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { isMatching, match } from "ts-pattern";
export function isInitializedFromReact(
name: string,
initialScope: Scope,
settings: Partial<Pick<ESLintReactSettings, "importSource" | "skipImportCheck">>,
settings: Partial<Pick<ESLintReactSettings, "importSource" | "strictImportCheck">>,
): boolean {
if (settings.skipImportCheck) return true;
if (!settings.strictImportCheck) return true;
// Optimistic assertion when identifier is named react
if (name.toLowerCase() === "react") return true;
const { importSource = "react" } = settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -45,7 +45,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -66,7 +66,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -85,7 +85,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -107,7 +107,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -132,7 +132,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -154,7 +154,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -171,7 +171,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -189,7 +189,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -210,7 +210,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -229,7 +229,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -251,7 +251,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -276,7 +276,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "@pika/react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -296,7 +296,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -316,7 +316,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -330,7 +330,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -342,7 +342,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -354,7 +354,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -366,7 +366,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -380,7 +380,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand All @@ -396,7 +396,7 @@ ruleTester.run(RULE_NAME, rule, {
settings: {
"react-x": {
importSource: "react",
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
1 change: 0 additions & 1 deletion packages/plugins/eslint-plugin-react-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const DEFAULT_ESLINT_REACT_SETTINGS = {
],
},
],
skipImportCheck: false,
} as const satisfies ESLintReactSettings;

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@ import rule, { RULE_NAME } from "./use-state";

ruleTester.run(RULE_NAME, rule, {
invalid: [
{
code: /* tsx */ `
function Component() {
const [state, setValue] = useState(0);
return <div />;
}
`,
errors: [{
messageId: "useState",
data: {
setterName: "setState",
stateName: "state",
},
}],
},
{
code: /* tsx */ `
function Component() {
const [state, setValue] = useState(0);
return <div />;
}
`,
errors: [{
messageId: "useState",
data: {
setterName: "setState",
stateName: "state",
},
}],
settings: {
"react-x": {
strictImportCheck: false,
},
},
},
{
code: /* tsx */ `
import { useState } from "react";
Expand Down Expand Up @@ -112,5 +149,19 @@ ruleTester.run(RULE_NAME, rule, {
return <div />;
}
`,
{
code: /* tsx */ `
function Component() {
const [state, setValue] = useState(0);
return <div />;
}
`,
settings: {
"react-x": {
strictImportCheck: true,
},
},
},
],
});
4 changes: 1 addition & 3 deletions packages/plugins/eslint-plugin-react-web-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import noLeakedInterval from "./rules/no-leaked-interval";
import noLeakedResizeObserver from "./rules/no-leaked-resize-observer";
import noLeakedTimeout from "./rules/no-leaked-timeout";

const DEFAULT_ESLINT_REACT_SETTINGS = {
skipImportCheck: true,
} as const satisfies ESLintReactSettings;
const DEFAULT_ESLINT_REACT_SETTINGS = {} as const satisfies ESLintReactSettings;

export default {
meta: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin-react-x/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const DEFAULT_ESLINT_REACT_SETTINGS = {
useLayoutEffect: ["useIsomorphicLayoutEffect"],
},
polymorphicPropName: "as",
skipImportCheck: false,
strictImportCheck: true,
version: "detect",
} as const satisfies ESLintReactSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ ruleTester.run(RULE_NAME, rule, {
`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ ruleTester.run(RULE_NAME, rule, {
code: /* tsx */ `<NotReact.Fragment />`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ruleTester.run(RULE_NAME, rule, {
code: /* tsx */ `<NotReact.Fragment />`,
settings: {
"react-x": {
skipImportCheck: false,
strictImportCheck: true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@
> `const` **INITIAL\_ESLINT\_REACT\_SETTINGS**: `object`
The initial settings for "react-x".

## Type declaration

### skipImportCheck

> `readonly` **skipImportCheck**: `false` = `false`
2 changes: 1 addition & 1 deletion packages/shared/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const ESLintReactSettingsSchema = object({
/**
* @internal
*/
skipImportCheck: optional(boolean(), true),
strictImportCheck: optional(boolean()),
/**
* React version to use, "detect" means auto detect React version from the project’s dependencies.
* If `importSource` is specified, an equivalent version of React should be provided here.
Expand Down
4 changes: 1 addition & 3 deletions packages/shared/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { ESLintSettingsSchema } from "./schemas";
/**
* The initial settings for "react-x".
*/
export const INITIAL_ESLINT_REACT_SETTINGS = {
skipImportCheck: false,
} as const satisfies ESLintReactSettings;
export const INITIAL_ESLINT_REACT_SETTINGS = {} as const satisfies ESLintReactSettings;

// #endregion

Expand Down

0 comments on commit 7cfbadc

Please sign in to comment.