Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ If you want to pin a minor version, use a tilde in your `package.json`.

<!-- doc-gen TILDE -->
```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"
```
<!-- end-doc-gen -->
4 changes: 2 additions & 2 deletions packages/eslint-plugin-solid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ If you want to pin a minor version, use a tilde in your `package.json`.

<!-- doc-gen TILDE -->
```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"
```
<!-- end-doc-gen -->
7 changes: 7 additions & 0 deletions packages/eslint-plugin-solid/docs/jsx-no-undef.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ let el = <div use:X={{}} />;
/* eslint solid/jsx-no-undef: ["error", { "allowGlobals": true }] */
let el = <div use:X />;

import * as Solid from "solid-js";
let el = <For each={items}>{(item) => item.name}</For>;
// after eslint --fix:
import * as Solid from "solid-js";
import { For } from "solid-js";
let el = <For each={items}>{(item) => item.name}</For>;

let el = <For each={items}>{(item) => item.name}</For>;
// after eslint --fix:
import { For } from "solid-js";
Expand Down
38 changes: 37 additions & 1 deletion packages/eslint-plugin-solid/docs/no-destructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }) => <div>{name}</div>;

export const A = ({ items: [first] }) => <div>{first}</div>;

export const Outer = (props) => {
const Inner = ({ label }) => (
<div>
{label}: {props.title}
</div>
);
return <Inner label="hi" />;
};

export const C = ({ a }) => {
const props = { b: 1 };
return (
<div>
{a}
{props.b}
</div>
);
};

export const C = ({ a }) => {
const f = (props) => props.x + a;
return <div>{f({ x: 1 })}</div>;
};

export const D = ({ a, b = a }) => <div>{b}</div>;

export const D = ({ a: x, b = x }) => <div>{b}</div>;

export const P = ({ props }) => <div>{props}</div>;
// after eslint --fix:
export const P = (props) => <div>{props.props}</div>;

let Component = ({}) => <div />;
// after eslint --fix:
let Component = (props) => <div />;
Expand Down
75 changes: 70 additions & 5 deletions packages/eslint-plugin-solid/src/rules/no-destructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
Expand All @@ -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<string>();
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,
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions packages/eslint-plugin-solid/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} }`);
Expand Down
9 changes: 9 additions & 0 deletions packages/eslint-plugin-solid/test/rules/jsx-no-undef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <For each={items}>{item => item.name}</For>`,
errors: [{ messageId: "autoImport", data: { imports: "'For'", source: "solid-js" } }],
output: `import * as Solid from "solid-js";
import { For } from "solid-js";
let el = <For each={items}>{item => item.name}</For>`,
},
{
code: `let el = <For each={items}>{item => item.name}</For>`,
errors: [{ messageId: "autoImport", data: { imports: "'For'", source: "solid-js" } }],
Expand Down
50 changes: 50 additions & 0 deletions packages/eslint-plugin-solid/test/rules/no-destructure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }) => <div>{name}</div>;`,
errors: [{ messageId: "noDestructure" }],
},
{
code: `export const A = ({ items: [first] }) => <div>{first}</div>;`,
errors: [{ messageId: "noDestructure" }],
},
{
// an enclosing component's props would be shadowed
code: `export const Outer = (props) => {
const Inner = ({ label }) => <div>{label}: {props.title}</div>;
return <Inner label="hi" />;
};`,
errors: [{ messageId: "noDestructure" }],
},
{
// a local props would be declared twice
code: `export const C = ({ a }) => {
const props = { b: 1 };
return <div>{a}{props.b}</div>;
};`,
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 <div>{f({ x: 1 })}</div>;
};`,
errors: [{ messageId: "noDestructure" }],
},
{
// a default that reads another destructured prop used to crash ESLint with
// overlapping fixes
code: `export const D = ({ a, b = a }) => <div>{b}</div>;`,
errors: [{ messageId: "noDestructure" }],
},
{
code: `export const D = ({ a: x, b = x }) => <div>{b}</div>;`,
errors: [{ messageId: "noDestructure" }],
},
{
// a destructured prop named props is fine: the binding goes away with the fix
code: `export const P = ({ props }) => <div>{props}</div>;`,
errors: [{ messageId: "noDestructure" }],
output: `export const P = (props) => <div>{props.props}</div>;`,
},
{
code: `let Component = ({}) => <div />`,
errors: [{ messageId: "noDestructure" }],
Expand Down
Loading