From 6e1fb7f72726fdf204e972b762beb3fdfaaf993b Mon Sep 17 00:00:00 2001 From: Ryan Carniato Date: Tue, 17 Nov 2026 17:22:08 +0000 Subject: [PATCH] fix: no-destructure and import autofixes that broke code (fixes #261) no-destructure now reports without an autofix when the fix can't be faithful: - a nested pattern (`{ user: { name } }`) or non-identifier rest, which has no `props.x` rewrite and used to leave the inner bindings undefined; - a default that reads another destructured prop (`{ a, b = a }`), which used to crash ESLint with overlapping fixes; - `props` or `_props` already declared in the function (or a nested scope) or read from an enclosing scope, which the new parameter would redeclare or shadow. appendImports (jsx-no-undef, imports) no longer merges named specifiers into a namespace import (`import * as Solid, { For }` is a syntax error); it adds a separate import declaration instead. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0117BoWPBR5hjwWf2VYS9c3p --- README.md | 4 +- packages/eslint-plugin-solid/README.md | 4 +- .../eslint-plugin-solid/docs/jsx-no-undef.md | 7 ++ .../docs/no-destructure.md | 38 +++++++++- .../src/rules/no-destructure.ts | 75 +++++++++++++++++-- packages/eslint-plugin-solid/src/utils.ts | 14 +++- .../test/rules/jsx-no-undef.test.ts | 9 +++ .../test/rules/no-destructure.test.ts | 50 +++++++++++++ 8 files changed, 189 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1131d37..dcad80f 100644 --- a/README.md +++ b/README.md @@ -286,7 +286,7 @@ If you want to pin a minor version, use a tilde in your `package.json`. ```diff -- "eslint-plugin-solid": "^0.18.1" -+ "eslint-plugin-solid": "~0.18.1" +- "eslint-plugin-solid": "^0.19.0" ++ "eslint-plugin-solid": "~0.19.0" ``` diff --git a/packages/eslint-plugin-solid/README.md b/packages/eslint-plugin-solid/README.md index 1131d37..dcad80f 100644 --- a/packages/eslint-plugin-solid/README.md +++ b/packages/eslint-plugin-solid/README.md @@ -286,7 +286,7 @@ If you want to pin a minor version, use a tilde in your `package.json`. ```diff -- "eslint-plugin-solid": "^0.18.1" -+ "eslint-plugin-solid": "~0.18.1" +- "eslint-plugin-solid": "^0.19.0" ++ "eslint-plugin-solid": "~0.19.0" ``` diff --git a/packages/eslint-plugin-solid/docs/jsx-no-undef.md b/packages/eslint-plugin-solid/docs/jsx-no-undef.md index 3a623c1..b3f90b0 100644 --- a/packages/eslint-plugin-solid/docs/jsx-no-undef.md +++ b/packages/eslint-plugin-solid/docs/jsx-no-undef.md @@ -45,6 +45,13 @@ let el =
; /* eslint solid/jsx-no-undef: ["error", { "allowGlobals": true }] */ let el =
; +import * as Solid from "solid-js"; +let el = {(item) => item.name}; +// after eslint --fix: +import * as Solid from "solid-js"; +import { For } from "solid-js"; +let el = {(item) => item.name}; + let el = {(item) => item.name}; // after eslint --fix: import { For } from "solid-js"; diff --git a/packages/eslint-plugin-solid/docs/no-destructure.md b/packages/eslint-plugin-solid/docs/no-destructure.md index 49162b7..8245225 100644 --- a/packages/eslint-plugin-solid/docs/no-destructure.md +++ b/packages/eslint-plugin-solid/docs/no-destructure.md @@ -22,9 +22,45 @@ components, even when they return JSX. Functions passed to PascalCase wrappers ### Invalid Examples -These snippets cause lint errors, and all of them can be auto-fixed. +These snippets cause lint errors, and some can be auto-fixed. ```js +export const A = ({ user: { name } }) =>
{name}
; + +export const A = ({ items: [first] }) =>
{first}
; + +export const Outer = (props) => { + const Inner = ({ label }) => ( +
+ {label}: {props.title} +
+ ); + return ; +}; + +export const C = ({ a }) => { + const props = { b: 1 }; + return ( +
+ {a} + {props.b} +
+ ); +}; + +export const C = ({ a }) => { + const f = (props) => props.x + a; + return
{f({ x: 1 })}
; +}; + +export const D = ({ a, b = a }) =>
{b}
; + +export const D = ({ a: x, b = x }) =>
{b}
; + +export const P = ({ props }) =>
{props}
; +// after eslint --fix: +export const P = (props) =>
{props.props}
; + let Component = ({}) =>
; // after eslint --fix: let Component = (props) =>
; diff --git a/packages/eslint-plugin-solid/src/rules/no-destructure.ts b/packages/eslint-plugin-solid/src/rules/no-destructure.ts index cedff19..bd4f9ce 100644 --- a/packages/eslint-plugin-solid/src/rules/no-destructure.ts +++ b/packages/eslint-plugin-solid/src/rules/no-destructure.ts @@ -91,7 +91,9 @@ export default createRule({ context.report({ node: props, messageId: "noDestructure", - fix: (fixer) => fixDestructure(node, props, fixer), + fix: canFixDestructure(node, props) + ? (fixer) => fixDestructure(node, props, fixer) + : undefined, }); } } @@ -100,6 +102,72 @@ export default createRule({ functionStack.pop(); }; + /** + * The fix rewrites every destructured binding to a `props.x` access and may + * declare `props`, `_props` and the rest name. Skip it (report only) when + * that can't be done faithfully (#261): + * - a property the fix can't represent, such as a nested pattern + * (`{ user: { name } }`), or a rest that isn't a plain identifier; + * - a default that reads another destructured prop (`{ a, b = a }`): the + * default moves into a `merge`/`mergeProps` call where `a` is unbound, + * and rewriting it inside the parameter overlaps the parameter fix; + * - a name the fix introduces (`props`, `_props`) that is already declared + * anywhere in the function, or read from an enclosing scope, since the + * new binding would collide with or shadow it. + */ + const canFixDestructure = (func: FunctionNode, props: T.ObjectPattern): boolean => { + const destructured = new Set(); + for (const property of props.properties) { + if (property.type === "RestElement") { + if (property.argument.type !== "Identifier") return false; + destructured.add(property.argument.name); + continue; + } + const info = getPropertyInfo(property); + if (info === null) return false; + destructured.add(info.var); + } + + const scope = getSourceCode(context).scopeManager?.acquire(func); + if (!scope) return false; + + for (const name of destructured) { + const variable = scope.set.get(name); + if ( + variable?.references.some( + (reference) => + reference.identifier.range[0] >= props.range[0] && + reference.identifier.range[1] <= props.range[1] && + !reference.init + ) + ) { + return false; + } + } + + const introduced = ["props", "_props"]; + const conflicts = (name: string): boolean => { + // Read from an enclosing scope (e.g. an outer component's `props`) + if (scope.through.some((reference) => reference.identifier.name === name)) return true; + // Declared in the function or any scope nested in it + const stack = [scope]; + while (stack.length) { + const current = stack.pop()!; + for (const variable of current.variables) { + if (variable.name !== name) continue; + // the destructured binding itself goes away with the fix + if (current === scope && destructured.has(name)) continue; + // `arguments` and similar implicit variables have no definitions + if (variable.defs.length === 0) continue; + return true; + } + stack.push(...current.childScopes); + } + return false; + }; + return !introduced.some(conflicts); + }; + /** Import the props helpers the fix uses from "solid-js", extending an existing import. */ function* importHelpers( fixer: TSESLint.RuleFixer, @@ -171,10 +239,7 @@ export default createRule({ const v2 = isSolidV2(context); const mergeName = v2 ? "merge" : "mergeProps"; const splitName = v2 ? "omit" : "splitProps"; - const helpers = [ - ...(hasDefaults ? [mergeName] : []), - ...(rest ? [splitName] : []), - ]; + const helpers = [...(hasDefaults ? [mergeName] : []), ...(rest ? [splitName] : [])]; // Replace destructured props with a `props` identifier (`_props` in case of rest // params/defaults). `omit` returns only the rest, so 2.0 keeps `props` for rest alone. diff --git a/packages/eslint-plugin-solid/src/utils.ts b/packages/eslint-plugin-solid/src/utils.ts index f8477e4..2941f62 100644 --- a/packages/eslint-plugin-solid/src/utils.ts +++ b/packages/eslint-plugin-solid/src/utils.ts @@ -336,9 +336,19 @@ export function appendImports( // import { B } from 'source' => import { B, C, D } from 'source' return fixer.insertTextAfter(lastSpecifier, `, ${identifiersString}`); } - const otherSpecifier = importNode.specifiers.find( - (s) => s.type === "ImportDefaultSpecifier" || s.type === "ImportNamespaceSpecifier" + const namespaceSpecifier = importNode.specifiers.find( + (s) => s.type === "ImportNamespaceSpecifier" ); + if (namespaceSpecifier) { + // A namespace import can't take named specifiers (#261): + // import * as A from 'source' => import * as A from 'source';\nimport { B, C, D } from 'source'; + const typePrefix = importNode.importKind === "type" ? "type " : ""; + return fixer.insertTextAfter( + importNode, + `\nimport ${typePrefix}{ ${identifiersString} } from ${importNode.source.raw};` + ); + } + const otherSpecifier = importNode.specifiers.find((s) => s.type === "ImportDefaultSpecifier"); if (otherSpecifier) { // import A from 'source' => import A, { B, C, D } from 'source' return fixer.insertTextAfter(otherSpecifier, `, { ${identifiersString} }`); diff --git a/packages/eslint-plugin-solid/test/rules/jsx-no-undef.test.ts b/packages/eslint-plugin-solid/test/rules/jsx-no-undef.test.ts index 4f8ba83..86c1d41 100644 --- a/packages/eslint-plugin-solid/test/rules/jsx-no-undef.test.ts +++ b/packages/eslint-plugin-solid/test/rules/jsx-no-undef.test.ts @@ -56,6 +56,15 @@ export const cases = run("jsx-no-undef", rule, { errors: [{ messageId: "customDirectiveUndefined", data: { identifier: "X" } }], }, // auto imports + { + // a namespace import can't take named specifiers; add a separate import (#261) + code: `import * as Solid from "solid-js"; +let el = {item => item.name}`, + errors: [{ messageId: "autoImport", data: { imports: "'For'", source: "solid-js" } }], + output: `import * as Solid from "solid-js"; +import { For } from "solid-js"; +let el = {item => item.name}`, + }, { code: `let el = {item => item.name}`, errors: [{ messageId: "autoImport", data: { imports: "'For'", source: "solid-js" } }], diff --git a/packages/eslint-plugin-solid/test/rules/no-destructure.test.ts b/packages/eslint-plugin-solid/test/rules/no-destructure.test.ts index d7cf8eb..8154599 100644 --- a/packages/eslint-plugin-solid/test/rules/no-destructure.test.ts +++ b/packages/eslint-plugin-solid/test/rules/no-destructure.test.ts @@ -49,6 +49,56 @@ export const cases = run("no-destructure", rule, { }, ], invalid: [ + // reported without an autofix when the fix can't be faithful (#261) + { + // nested pattern: the inner bindings have no props.x equivalent + code: `export const A = ({ user: { name } }) =>
{name}
;`, + errors: [{ messageId: "noDestructure" }], + }, + { + code: `export const A = ({ items: [first] }) =>
{first}
;`, + errors: [{ messageId: "noDestructure" }], + }, + { + // an enclosing component's props would be shadowed + code: `export const Outer = (props) => { + const Inner = ({ label }) =>
{label}: {props.title}
; + return ; +};`, + errors: [{ messageId: "noDestructure" }], + }, + { + // a local props would be declared twice + code: `export const C = ({ a }) => { + const props = { b: 1 }; + return
{a}{props.b}
; +};`, + errors: [{ messageId: "noDestructure" }], + }, + { + // a nested function's props would capture the rewritten access + code: `export const C = ({ a }) => { + const f = (props) => props.x + a; + return
{f({ x: 1 })}
; +};`, + errors: [{ messageId: "noDestructure" }], + }, + { + // a default that reads another destructured prop used to crash ESLint with + // overlapping fixes + code: `export const D = ({ a, b = a }) =>
{b}
;`, + errors: [{ messageId: "noDestructure" }], + }, + { + code: `export const D = ({ a: x, b = x }) =>
{b}
;`, + errors: [{ messageId: "noDestructure" }], + }, + { + // a destructured prop named props is fine: the binding goes away with the fix + code: `export const P = ({ props }) =>
{props}
;`, + errors: [{ messageId: "noDestructure" }], + output: `export const P = (props) =>
{props.props}
;`, + }, { code: `let Component = ({}) =>
`, errors: [{ messageId: "noDestructure" }],