@murky-web/oxlint-plugin-solid 1.5.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -16
- package/THIRD_PARTY_NOTICES.md +7 -4
- package/package.json +5 -2
- package/src/compat.mjs +3 -0
- package/src/index.mjs +23 -0
- package/src/rules/components_return_once.mjs +131 -84
- package/src/rules/event_handlers.mjs +33 -13
- package/src/rules/filename_matches_export.mjs +292 -0
- package/src/rules/function_size.mjs +189 -0
- package/src/rules/imports.mjs +5 -2
- package/src/rules/init_declarations.mjs +139 -0
- package/src/rules/jsx_no_duplicate_props.mjs +36 -8
- package/src/rules/jsx_no_script_url.mjs +3 -2
- package/src/rules/jsx_no_undef.mjs +8 -11
- package/src/rules/no_array_handlers.mjs +35 -6
- package/src/rules/no_cleanup_in_effect_apply.mjs +154 -0
- package/src/rules/no_destructure.mjs +76 -4
- package/src/rules/no_innerhtml.mjs +51 -18
- package/src/rules/no_module_scope_reactive_primitive.mjs +73 -0
- package/src/rules/no_owned_primitives_in_ref.mjs +23 -2
- package/src/rules/no_proxy_apis.mjs +41 -12
- package/src/rules/no_react_deps.mjs +394 -38
- package/src/rules/no_react_specific_props.mjs +33 -4
- package/src/rules/no_server_import_in_client.mjs +1 -1
- package/src/rules/no_setter_in_effect.mjs +145 -22
- package/src/rules/no_single_arg_create_effect.mjs +55 -0
- package/src/rules/no_unassigned_vars.mjs +99 -0
- package/src/rules/no_unknown_namespaces.mjs +96 -17
- package/src/rules/no_untracked_effect_read.mjs +65 -28
- package/src/rules/no_write_in_pure_computation.mjs +273 -0
- package/src/rules/prefer_arrow_components.mjs +30 -4
- package/src/rules/prefer_class_object.mjs +37 -5
- package/src/rules/prefer_destructuring.mjs +164 -0
- package/src/rules/prefer_for.mjs +34 -4
- package/src/rules/prefer_ref_shorthand.mjs +142 -0
- package/src/rules/prefer_show.mjs +48 -17
- package/src/rules/reactive_async.mjs +55 -8
- package/src/rules/reactivity.mjs +1160 -291
- package/src/rules/self_closing_comp.mjs +7 -2
- package/src/rules/strict_void_return.mjs +43 -13
- package/src/rules/style_prop.mjs +38 -9
- package/src/rules/typed_merge_defaults.mjs +88 -0
- package/src/rules/validate_jsx_nesting.mjs +2 -2
- package/src/utils.mjs +51 -20
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Fork of eslint's prefer-destructuring for Solid code. Destructuring is
|
|
2
|
+
// preferred everywhere except on reactive sources: `const { x } = props` or
|
|
3
|
+
// `const { items } = store` copies the value once at setup and drops
|
|
4
|
+
// reactivity, so there `props.x` read where it is used is the right form, and
|
|
5
|
+
// eslint's rule pushed toward the bug. Defaults as in eslint (declarations and
|
|
6
|
+
// assignments, arrays and objects, no renamed properties); like
|
|
7
|
+
// typescript-eslint, a declaration with a type annotation is left alone.
|
|
8
|
+
import { findVariable, getSourceCode } from "../compat.mjs";
|
|
9
|
+
import {
|
|
10
|
+
getFunctionName,
|
|
11
|
+
isFunctionNode,
|
|
12
|
+
isPropsByName,
|
|
13
|
+
trackImports
|
|
14
|
+
} from "../utils.mjs";
|
|
15
|
+
|
|
16
|
+
/** Calls whose result is a reactive proxy read by property access. */
|
|
17
|
+
const REACTIVE_OBJECT_SOURCES = [
|
|
18
|
+
"createStore",
|
|
19
|
+
"createProjection",
|
|
20
|
+
"createOptimisticStore",
|
|
21
|
+
"merge",
|
|
22
|
+
"omit"
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
function isIndexAccess(member) {
|
|
26
|
+
return (
|
|
27
|
+
member.computed &&
|
|
28
|
+
member.property.type === "Literal" &&
|
|
29
|
+
Number.isInteger(member.property.value)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isComponentProps(definition) {
|
|
34
|
+
if (definition.type !== "Parameter") {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const fn = definition.node;
|
|
38
|
+
return (
|
|
39
|
+
isFunctionNode(fn) &&
|
|
40
|
+
fn.params[0] === definition.name &&
|
|
41
|
+
/^[A-Z]/u.test(getFunctionName(fn) ?? "")
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default {
|
|
46
|
+
meta: {
|
|
47
|
+
type: "suggestion",
|
|
48
|
+
docs: {
|
|
49
|
+
description:
|
|
50
|
+
"Prefer destructuring, except from props and stores, where it drops reactivity."
|
|
51
|
+
},
|
|
52
|
+
schema: [],
|
|
53
|
+
messages: {
|
|
54
|
+
preferDestructuring:
|
|
55
|
+
"`{{statement}}` reads `{{member}}` into a variable by hand; {{type}} destructuring states the source once. Fix: `{{fix}}`."
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
defaultOptions: [],
|
|
59
|
+
create(context) {
|
|
60
|
+
const { matchImport, handleImportDeclaration } = trackImports();
|
|
61
|
+
|
|
62
|
+
function isReactiveSource(object) {
|
|
63
|
+
if (object.type !== "Identifier") {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (isPropsByName(object.name)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
const definition = findVariable(context, object)?.defs[0];
|
|
70
|
+
if (!definition) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (isComponentProps(definition)) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
const init =
|
|
77
|
+
definition.type === "Variable" ? definition.node.init : null;
|
|
78
|
+
if (
|
|
79
|
+
init?.type !== "CallExpression" ||
|
|
80
|
+
init.callee.type !== "Identifier" ||
|
|
81
|
+
!matchImport(REACTIVE_OBJECT_SOURCES, init.callee.name)
|
|
82
|
+
) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
// `const [store] = createStore(...)` or `const local = merge(...)`.
|
|
86
|
+
const { id } = definition.node;
|
|
87
|
+
return id.type === "ArrayPattern"
|
|
88
|
+
? id.elements[0] === definition.name
|
|
89
|
+
: id === definition.name;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const sourceCode = getSourceCode(context);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The destructuring form of `reportNode`: `const { x } = obj` or `const
|
|
96
|
+
* [, x] = list`, and `({ x } = obj)` for an assignment.
|
|
97
|
+
*/
|
|
98
|
+
function buildData(value, reportNode, pattern) {
|
|
99
|
+
const object = sourceCode.getText(value.object);
|
|
100
|
+
const statement =
|
|
101
|
+
reportNode.type === "VariableDeclarator"
|
|
102
|
+
? `${reportNode.parent.kind} ${sourceCode.getText(reportNode)}`
|
|
103
|
+
: sourceCode.getText(reportNode);
|
|
104
|
+
const fix =
|
|
105
|
+
reportNode.type === "VariableDeclarator"
|
|
106
|
+
? `${reportNode.parent.kind} ${pattern} = ${object}`
|
|
107
|
+
: `(${pattern} = ${object})`;
|
|
108
|
+
return {
|
|
109
|
+
fix,
|
|
110
|
+
member: sourceCode.getText(value),
|
|
111
|
+
statement,
|
|
112
|
+
type: pattern.startsWith("[") ? "array" : "object"
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function check(target, value, reportNode) {
|
|
117
|
+
if (
|
|
118
|
+
target.type !== "Identifier" ||
|
|
119
|
+
value?.type !== "MemberExpression"
|
|
120
|
+
) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (isIndexAccess(value)) {
|
|
124
|
+
context.report({
|
|
125
|
+
data: buildData(
|
|
126
|
+
value,
|
|
127
|
+
reportNode,
|
|
128
|
+
`[${", ".repeat(value.property.value)}${target.name}]`
|
|
129
|
+
),
|
|
130
|
+
messageId: "preferDestructuring",
|
|
131
|
+
node: reportNode
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (
|
|
136
|
+
!value.computed &&
|
|
137
|
+
value.property.type === "Identifier" &&
|
|
138
|
+
value.property.name === target.name &&
|
|
139
|
+
!isReactiveSource(value.object)
|
|
140
|
+
) {
|
|
141
|
+
context.report({
|
|
142
|
+
data: buildData(value, reportNode, `{ ${target.name} }`),
|
|
143
|
+
messageId: "preferDestructuring",
|
|
144
|
+
node: reportNode
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
ImportDeclaration: handleImportDeclaration,
|
|
151
|
+
VariableDeclarator(node) {
|
|
152
|
+
if (node.id.typeAnnotation) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
check(node.id, node.init, node);
|
|
156
|
+
},
|
|
157
|
+
AssignmentExpression(node) {
|
|
158
|
+
if (node.operator === "=") {
|
|
159
|
+
check(node.left, node.right, node);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
};
|
package/src/rules/prefer_for.mjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { getPropertyName } from "@eslint-community/eslint-utils";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { getSourceCode } from "../compat.mjs";
|
|
4
|
+
import {
|
|
5
|
+
isFunctionNode,
|
|
6
|
+
isJSXElementOrFragment,
|
|
7
|
+
messageCode
|
|
8
|
+
} from "../utils.mjs";
|
|
4
9
|
export default {
|
|
5
10
|
meta: {
|
|
6
11
|
type: "problem",
|
|
@@ -13,13 +18,16 @@ export default {
|
|
|
13
18
|
schema: [],
|
|
14
19
|
messages: {
|
|
15
20
|
preferFor:
|
|
16
|
-
"
|
|
21
|
+
"`{{array}}.map(…)` in JSX re-runs the whole map when `{{array}}` changes, so the DOM of every row is created again. Fix: `<For each={{{array}}}>{{{callback}}}</For>`.",
|
|
17
22
|
preferForOrIndex:
|
|
18
|
-
"
|
|
23
|
+
"`{{array}}.map(…)` in JSX re-runs the whole map when `{{array}}` changes, so the DOM of every row is created again. Fix: `<For each={{{array}}}>{({{item}}, {{index}}) => …}</For>`, where `{{index}}` is an accessor (read `{{index}}()`); with `keyed={false}`, `{{item}}` is the accessor (`{{item}}()`) and `{{index}}` a plain number."
|
|
19
24
|
}
|
|
20
25
|
},
|
|
21
26
|
defaultOptions: [],
|
|
22
27
|
create(context) {
|
|
28
|
+
const sourceCode = getSourceCode(context);
|
|
29
|
+
const arrayText = (node) =>
|
|
30
|
+
messageCode(sourceCode.getText(node.callee.object), "items()");
|
|
23
31
|
const reportPreferFor = (node) => {
|
|
24
32
|
const jsxExpressionContainerNode = node.parent;
|
|
25
33
|
const arrayNode = node.callee.object;
|
|
@@ -27,6 +35,13 @@ export default {
|
|
|
27
35
|
context.report({
|
|
28
36
|
node,
|
|
29
37
|
messageId: "preferFor",
|
|
38
|
+
data: {
|
|
39
|
+
array: arrayText(node),
|
|
40
|
+
callback: messageCode(
|
|
41
|
+
sourceCode.getText(mapFnNode),
|
|
42
|
+
"(item) => …"
|
|
43
|
+
)
|
|
44
|
+
},
|
|
30
45
|
fix: (fixer) => {
|
|
31
46
|
const beforeArray = [
|
|
32
47
|
jsxExpressionContainerNode.range[0],
|
|
@@ -77,9 +92,24 @@ export default {
|
|
|
77
92
|
reportPreferFor(node);
|
|
78
93
|
} else {
|
|
79
94
|
// Too many possible solutions to make a suggestion or fix
|
|
95
|
+
const [item, index] = mapFnNode.params.map(
|
|
96
|
+
(param) => {
|
|
97
|
+
if (param.type === "RestElement") {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return param.type === "Identifier"
|
|
101
|
+
? param.name
|
|
102
|
+
: sourceCode.getText(param);
|
|
103
|
+
}
|
|
104
|
+
);
|
|
80
105
|
context.report({
|
|
81
106
|
node,
|
|
82
|
-
messageId: "preferForOrIndex"
|
|
107
|
+
messageId: "preferForOrIndex",
|
|
108
|
+
data: {
|
|
109
|
+
array: arrayText(node),
|
|
110
|
+
item: item ?? "item",
|
|
111
|
+
index: index ?? "index"
|
|
112
|
+
}
|
|
83
113
|
});
|
|
84
114
|
}
|
|
85
115
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// A single `ref={el}` is rewritten by Solid's compiler to `el = node`, so a
|
|
2
|
+
// callback that only does that assignment is the long form of the shorthand.
|
|
3
|
+
// The rewrite is single-ref only: inside `ref={[…]}` every entry is a callback
|
|
4
|
+
// and a bare `el` is never assigned.
|
|
5
|
+
import { findVariable, getSourceCode } from "../compat.mjs";
|
|
6
|
+
import { jsxPropName } from "../utils.mjs";
|
|
7
|
+
|
|
8
|
+
function unwrapNonNull(node) {
|
|
9
|
+
return node?.type === "TSNonNullExpression" ? node.expression : node;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** `el` in `(node) => { el = node; }` or `(node) => (el = node)`. */
|
|
13
|
+
function getStoredIdentifier(callback) {
|
|
14
|
+
if (
|
|
15
|
+
(callback.type !== "ArrowFunctionExpression" &&
|
|
16
|
+
callback.type !== "FunctionExpression") ||
|
|
17
|
+
callback.async ||
|
|
18
|
+
callback.generator ||
|
|
19
|
+
callback.params.length !== 1 ||
|
|
20
|
+
callback.params[0].type !== "Identifier"
|
|
21
|
+
) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
let expression = callback.body;
|
|
25
|
+
if (expression.type === "BlockStatement") {
|
|
26
|
+
const [statement] = expression.body;
|
|
27
|
+
if (
|
|
28
|
+
expression.body.length !== 1 ||
|
|
29
|
+
statement.type !== "ExpressionStatement"
|
|
30
|
+
) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
expression = statement.expression;
|
|
34
|
+
}
|
|
35
|
+
const param = callback.params[0].name;
|
|
36
|
+
if (
|
|
37
|
+
expression.type === "AssignmentExpression" &&
|
|
38
|
+
expression.operator === "=" &&
|
|
39
|
+
expression.left.type === "Identifier" &&
|
|
40
|
+
expression.left.name !== param &&
|
|
41
|
+
expression.right.type === "Identifier" &&
|
|
42
|
+
expression.right.name === param
|
|
43
|
+
) {
|
|
44
|
+
return expression.left;
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default {
|
|
50
|
+
meta: {
|
|
51
|
+
type: "suggestion",
|
|
52
|
+
docs: {
|
|
53
|
+
description:
|
|
54
|
+
"Prefer `ref={el}` over a callback that only stores the element, and report a bare variable in a ref array, which is never assigned."
|
|
55
|
+
},
|
|
56
|
+
fixable: "code",
|
|
57
|
+
hasSuggestions: true,
|
|
58
|
+
schema: [],
|
|
59
|
+
messages: {
|
|
60
|
+
callbackOnlyStores:
|
|
61
|
+
"The `ref` callback `{{callback}}` only stores the element in `{{name}}`, which `{{shorthand}}` does on its own: Solid's compiler rewrites a single `ref` variable to that assignment. Fix: `{{shorthand}}`.",
|
|
62
|
+
bareEntryInArray:
|
|
63
|
+
"`{{name}}` in the `ref` array is never assigned and stays `undefined`: Solid rewrites only a single `{{shorthand}}` to an assignment, and an array entry must be a callback. Fix: `{{callback}}` in its place. Do not: add `!` or a default to `{{name}}`; it stays unassigned either way.",
|
|
64
|
+
useCallback: "Replace `{{name}}` with `{{callback}}`."
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
defaultOptions: [],
|
|
68
|
+
create(context) {
|
|
69
|
+
const sourceCode = getSourceCode(context);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A `let el;` waiting for its element. A directive in the array
|
|
73
|
+
* (`ref={[autofocus]}`) is a function or a variable with a value.
|
|
74
|
+
*/
|
|
75
|
+
function isElementVariable(identifier) {
|
|
76
|
+
const definition = findVariable(context, identifier)?.defs[0];
|
|
77
|
+
return (
|
|
78
|
+
definition?.type === "Variable" &&
|
|
79
|
+
definition.parent.kind !== "const" &&
|
|
80
|
+
!definition.parent.declare &&
|
|
81
|
+
definition.node.init === null
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function checkArrayEntry(entry) {
|
|
86
|
+
const identifier = unwrapNonNull(entry);
|
|
87
|
+
if (
|
|
88
|
+
identifier?.type !== "Identifier" ||
|
|
89
|
+
!isElementVariable(identifier)
|
|
90
|
+
) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const { name } = identifier;
|
|
94
|
+
const param = name === "node" ? "element" : "node";
|
|
95
|
+
const callback = `(${param}) => { ${name} = ${param}; }`;
|
|
96
|
+
context.report({
|
|
97
|
+
data: { callback, name, shorthand: `ref={${name}}` },
|
|
98
|
+
messageId: "bareEntryInArray",
|
|
99
|
+
node: entry,
|
|
100
|
+
suggest: [
|
|
101
|
+
{
|
|
102
|
+
data: { callback, name },
|
|
103
|
+
fix: (fixer) => fixer.replaceText(entry, callback),
|
|
104
|
+
messageId: "useCallback"
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
JSXAttribute(node) {
|
|
112
|
+
if (
|
|
113
|
+
jsxPropName(node) !== "ref" ||
|
|
114
|
+
node.value?.type !== "JSXExpressionContainer"
|
|
115
|
+
) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const { expression } = node.value;
|
|
119
|
+
if (expression.type === "ArrayExpression") {
|
|
120
|
+
for (const entry of expression.elements) {
|
|
121
|
+
checkArrayEntry(entry);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const stored = getStoredIdentifier(expression);
|
|
126
|
+
if (stored === null) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
context.report({
|
|
130
|
+
data: {
|
|
131
|
+
callback: sourceCode.getText(expression),
|
|
132
|
+
name: stored.name,
|
|
133
|
+
shorthand: `ref={${stored.name}}`
|
|
134
|
+
},
|
|
135
|
+
fix: (fixer) => fixer.replaceText(expression, stored.name),
|
|
136
|
+
messageId: "callbackOnlyStores",
|
|
137
|
+
node: expression
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { getSourceCode } from "../compat.mjs";
|
|
2
|
-
import { isJSXElementOrFragment } from "../utils.mjs";
|
|
3
|
-
|
|
2
|
+
import { isJSXElementOrFragment, messageCode } from "../utils.mjs";
|
|
3
|
+
// Only a branch that is visibly JSX is reported. A text ternary is the
|
|
4
|
+
// cheapest form, and an Identifier may hold text, where a `<Show>` is slower:
|
|
5
|
+
// the compiler does not lower it. Oxlint's tree drops parentheses, so
|
|
6
|
+
// `(<A/>)` is a JSXElement here too.
|
|
4
7
|
export default {
|
|
5
8
|
meta: {
|
|
6
9
|
type: "problem",
|
|
@@ -10,12 +13,15 @@ export default {
|
|
|
10
13
|
url: "https://github.com/solidjs-community/eslint-plugin-solid/blob/main/packages/eslint-plugin-solid/docs/prefer-show.md"
|
|
11
14
|
},
|
|
12
15
|
fixable: "code",
|
|
16
|
+
hasSuggestions: true,
|
|
13
17
|
schema: [],
|
|
14
18
|
messages: {
|
|
15
19
|
preferShowAnd:
|
|
16
|
-
"
|
|
20
|
+
'`{{test}} && …` renders an element, and when `{{test}}` is `0` or `""` it renders that value instead of nothing. Fix: `{{fix}}`; `<Show>` renders nothing for every falsy value.',
|
|
17
21
|
preferShowTernary:
|
|
18
|
-
"
|
|
22
|
+
"`{{test}} ? … : …` switches between elements, which is what `<Show>` is for; a ternary is the cheaper form only when both branches are text. Fix: `{{fix}}`.",
|
|
23
|
+
useShow:
|
|
24
|
+
"Replace with `<Show>`. A falsy value such as `0` is then no longer rendered."
|
|
19
25
|
}
|
|
20
26
|
},
|
|
21
27
|
defaultOptions: [],
|
|
@@ -25,33 +31,58 @@ export default {
|
|
|
25
31
|
const text = sourceCode.getText(node);
|
|
26
32
|
return isJSXElementOrFragment(node) ? text : `{${text}}`;
|
|
27
33
|
};
|
|
34
|
+
// `{count() && <A/>}` renders `0` when count() is 0, `<Show>` renders
|
|
35
|
+
// nothing, so the rewrite is a suggestion, not a fix.
|
|
36
|
+
const testText = (node) =>
|
|
37
|
+
messageCode(sourceCode.getText(node), "condition");
|
|
28
38
|
const logicalExpressionHandler = (node) => {
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
EXPENSIVE_TYPES.includes(node.right.type)
|
|
32
|
-
) {
|
|
39
|
+
if (node.operator === "&&" && isJSXElementOrFragment(node.right)) {
|
|
40
|
+
const test = sourceCode.getText(node.left);
|
|
33
41
|
context.report({
|
|
34
42
|
node,
|
|
35
43
|
messageId: "preferShowAnd",
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
: node,
|
|
42
|
-
`<Show when={${sourceCode.getText(node.left)}}>${putIntoJSX(node.right)}</Show>`
|
|
44
|
+
data: {
|
|
45
|
+
test: testText(node.left),
|
|
46
|
+
fix: messageCode(
|
|
47
|
+
`<Show when={${test}}>${putIntoJSX(node.right)}</Show>`,
|
|
48
|
+
`<Show when={${testText(node.left)}}>…</Show>`
|
|
43
49
|
)
|
|
50
|
+
},
|
|
51
|
+
suggest: [
|
|
52
|
+
{
|
|
53
|
+
messageId: "useShow",
|
|
54
|
+
fix: (fixer) =>
|
|
55
|
+
fixer.replaceText(
|
|
56
|
+
node.parent?.type ===
|
|
57
|
+
"JSXExpressionContainer" &&
|
|
58
|
+
isJSXElementOrFragment(
|
|
59
|
+
node.parent.parent
|
|
60
|
+
)
|
|
61
|
+
? node.parent
|
|
62
|
+
: node,
|
|
63
|
+
`<Show when={${sourceCode.getText(node.left)}}>${putIntoJSX(node.right)}</Show>`
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
]
|
|
44
67
|
});
|
|
45
68
|
}
|
|
46
69
|
};
|
|
47
70
|
const conditionalExpressionHandler = (node) => {
|
|
48
71
|
if (
|
|
49
|
-
|
|
50
|
-
|
|
72
|
+
isJSXElementOrFragment(node.consequent) ||
|
|
73
|
+
isJSXElementOrFragment(node.alternate)
|
|
51
74
|
) {
|
|
75
|
+
const test = sourceCode.getText(node.test);
|
|
52
76
|
context.report({
|
|
53
77
|
node,
|
|
54
78
|
messageId: "preferShowTernary",
|
|
79
|
+
data: {
|
|
80
|
+
test: testText(node.test),
|
|
81
|
+
fix: messageCode(
|
|
82
|
+
`<Show when={${test}} fallback={${sourceCode.getText(node.alternate)}}>${putIntoJSX(node.consequent)}</Show>`,
|
|
83
|
+
`<Show when={${testText(node.test)}} fallback={…}>…</Show>`
|
|
84
|
+
)
|
|
85
|
+
},
|
|
55
86
|
fix: (fixer) =>
|
|
56
87
|
fixer.replaceText(
|
|
57
88
|
node.parent?.type === "JSXExpressionContainer" &&
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { findVariable } from "../compat.mjs";
|
|
1
|
+
import { findVariable, getSourceCode } from "../compat.mjs";
|
|
2
2
|
import {
|
|
3
3
|
isFunctionNode,
|
|
4
4
|
isPropsByName,
|
|
@@ -14,6 +14,8 @@ import {
|
|
|
14
14
|
const COMPUTATION_PRIMITIVES = [
|
|
15
15
|
"createDeferred",
|
|
16
16
|
"createMemo",
|
|
17
|
+
"dynamic",
|
|
18
|
+
"dynamicComponent",
|
|
17
19
|
"createOptimistic",
|
|
18
20
|
"createOptimisticStore",
|
|
19
21
|
"createProjection",
|
|
@@ -90,6 +92,16 @@ function isOnlyReturnAwait(callback, awaits) {
|
|
|
90
92
|
);
|
|
91
93
|
}
|
|
92
94
|
|
|
95
|
+
/** `id` -> `currentId`, `props.lang` -> `lang`. */
|
|
96
|
+
function localName(name) {
|
|
97
|
+
const dot = name.lastIndexOf(".");
|
|
98
|
+
if (dot !== -1) {
|
|
99
|
+
return name.slice(dot + 1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return `current${name[0].toUpperCase()}${name.slice(1)}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
93
105
|
export const reactiveAsyncRule = {
|
|
94
106
|
meta: {
|
|
95
107
|
type: "problem",
|
|
@@ -101,9 +113,9 @@ export const reactiveAsyncRule = {
|
|
|
101
113
|
schema: [],
|
|
102
114
|
messages: {
|
|
103
115
|
untrackedReadAfterAwait:
|
|
104
|
-
"'{{name}}' is read after an `await` in this `{{primitive}}` callback
|
|
116
|
+
"'{{name}}' is first read after an `await` or `yield` in this `{{primitive}}` callback, so it is not a dependency and the result goes stale when it changes (Solid warns UNTRACKED_READ_AFTER_AWAIT in dev). Fix: read it before the first `await`, `const {{local}} = {{read}};` at the top of the callback, and use `{{local}}` where `{{read}}` is now; or read every input synchronously and move the async work into a helper, `{{primitive}}(() => load(…, {{read}}))`. Do not: wrap the read in `untrack()`; that keeps the stale result.",
|
|
105
117
|
redundantAsync:
|
|
106
|
-
"This `async` `{{primitive}}` callback only does `return await
|
|
118
|
+
"This `async` `{{primitive}}` callback only does `return await {{promise}}`, and the `await` adds nothing: Solid reads a returned promise like a value. Fix: return the promise, `{{primitive}}({{fix}})`. Do not: wrap it in `try` only to keep the `await`."
|
|
107
119
|
}
|
|
108
120
|
},
|
|
109
121
|
defaultOptions: [],
|
|
@@ -114,6 +126,12 @@ export const reactiveAsyncRule = {
|
|
|
114
126
|
// the same name is not mistaken for the signal.
|
|
115
127
|
const accessors = new Set();
|
|
116
128
|
const stores = new Set();
|
|
129
|
+
const sourceCode = getSourceCode(context);
|
|
130
|
+
/** Code as written, or `fallback` when it spans lines. */
|
|
131
|
+
const quote = (node, fallback) => {
|
|
132
|
+
const text = sourceCode.getText(node);
|
|
133
|
+
return text.includes("\n") ? fallback : text;
|
|
134
|
+
};
|
|
117
135
|
|
|
118
136
|
function bindingKind(identifier) {
|
|
119
137
|
const definition = findVariable(context, identifier)?.defs[0];
|
|
@@ -154,12 +172,20 @@ export const reactiveAsyncRule = {
|
|
|
154
172
|
|
|
155
173
|
function checkCallback(callback, primitive) {
|
|
156
174
|
const awaits = [];
|
|
175
|
+
// `await`, and `yield` in a generator computation: both suspend it.
|
|
176
|
+
const gaps = [];
|
|
157
177
|
const reads = [];
|
|
158
178
|
|
|
159
179
|
walkOwnBody(callback.body, (candidate) => {
|
|
160
180
|
if (candidate.type === "AwaitExpression") {
|
|
161
181
|
awaits.push(candidate);
|
|
162
182
|
}
|
|
183
|
+
if (
|
|
184
|
+
candidate.type === "AwaitExpression" ||
|
|
185
|
+
candidate.type === "YieldExpression"
|
|
186
|
+
) {
|
|
187
|
+
gaps.push(candidate);
|
|
188
|
+
}
|
|
163
189
|
|
|
164
190
|
const name = readName(candidate);
|
|
165
191
|
if (name !== null) {
|
|
@@ -167,7 +193,7 @@ export const reactiveAsyncRule = {
|
|
|
167
193
|
}
|
|
168
194
|
});
|
|
169
195
|
|
|
170
|
-
if (
|
|
196
|
+
if (gaps.length === 0) {
|
|
171
197
|
return;
|
|
172
198
|
}
|
|
173
199
|
|
|
@@ -178,19 +204,40 @@ export const reactiveAsyncRule = {
|
|
|
178
204
|
context.report({
|
|
179
205
|
node: callback,
|
|
180
206
|
messageId: "redundantAsync",
|
|
181
|
-
data: {
|
|
207
|
+
data: {
|
|
208
|
+
// .ts keeps `async`: typescript/promise-function-async
|
|
209
|
+
// asks for it on a promise-returning function there,
|
|
210
|
+
// and typescript/require-await accepts it without an
|
|
211
|
+
// await. .tsx/.jsx turn that rule off, so `async` goes.
|
|
212
|
+
fix: /\.[jt]sx$/u.test(context.filename ?? "")
|
|
213
|
+
? `() => ${quote(awaits[0].argument, "…")}`
|
|
214
|
+
: `async () => ${quote(awaits[0].argument, "…")}`,
|
|
215
|
+
primitive,
|
|
216
|
+
promise: quote(awaits[0].argument, "…")
|
|
217
|
+
}
|
|
182
218
|
});
|
|
183
219
|
}
|
|
184
220
|
|
|
185
221
|
// A read inside the awaited expression itself still runs before the
|
|
186
222
|
// await suspends, so the boundary is where the first await ends.
|
|
187
|
-
|
|
223
|
+
// Like the runtime, a source already read before it is a dependency
|
|
224
|
+
// and fine to read again; a late source is reported once.
|
|
225
|
+
const firstAwaitEnd = Math.min(...gaps.map(getEnd));
|
|
226
|
+
const tracked = new Set();
|
|
188
227
|
for (const read of reads) {
|
|
189
|
-
if (getStart(read.node)
|
|
228
|
+
if (getStart(read.node) < firstAwaitEnd) {
|
|
229
|
+
tracked.add(read.name);
|
|
230
|
+
} else if (!tracked.has(read.name)) {
|
|
231
|
+
tracked.add(read.name);
|
|
190
232
|
context.report({
|
|
191
233
|
node: read.node,
|
|
192
234
|
messageId: "untrackedReadAfterAwait",
|
|
193
|
-
data: {
|
|
235
|
+
data: {
|
|
236
|
+
local: localName(read.name),
|
|
237
|
+
name: read.name,
|
|
238
|
+
primitive,
|
|
239
|
+
read: quote(read.node, read.name)
|
|
240
|
+
}
|
|
194
241
|
});
|
|
195
242
|
}
|
|
196
243
|
}
|