@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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getStaticValue } from "@eslint-community/eslint-utils";
|
|
2
2
|
|
|
3
3
|
import { getScope, getSourceCode } from "../compat.mjs";
|
|
4
|
-
import { isDOMElementName } from "../utils.mjs";
|
|
4
|
+
import { isDOMElementName, messageCode } from "../utils.mjs";
|
|
5
5
|
const COMMON_EVENTS = [
|
|
6
6
|
"onAnimationEnd",
|
|
7
7
|
"onAnimationIteration",
|
|
@@ -69,6 +69,7 @@ const COMMON_EVENTS_MAP = new Map(
|
|
|
69
69
|
}
|
|
70
70
|
})()
|
|
71
71
|
);
|
|
72
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
72
73
|
const NONSTANDARD_EVENTS_MAP = {
|
|
73
74
|
ondoubleclick: "onDblClick"
|
|
74
75
|
};
|
|
@@ -112,20 +113,25 @@ export default {
|
|
|
112
113
|
],
|
|
113
114
|
messages: {
|
|
114
115
|
"detected-attr":
|
|
115
|
-
|
|
116
|
-
naming: "
|
|
116
|
+
"`{{name}}` is named like an event handler but has the constant value `{{staticValue}}`, so Solid writes it into the template as an attribute and attaches no listener. Fix: to handle the event, pass a function, `{{name}}={{{handler}}}`; to keep an attribute, use a name that does not start with `on`.",
|
|
117
|
+
naming: "`{{name}}` is not an event handler in Solid 2.0: only `on` followed by an uppercase letter is, so `{{name}}` is set as an attribute and a function value is never called. Fix: `{{handlerName}}` if it handles the `{{event}}` event; otherwise a name that does not start with `on`.",
|
|
117
118
|
capitalization:
|
|
118
|
-
"
|
|
119
|
+
"`{{name}}` is not an event handler in Solid 2.0: only `on` followed by an uppercase letter is, so `{{name}}` is set as an attribute and a function value is never called. Fix: `{{fixedName}}`.",
|
|
119
120
|
nonstandard:
|
|
120
|
-
"
|
|
121
|
-
"make-handler": "
|
|
121
|
+
"`{{name}}` listens to a `{{event}}` event, which the DOM never fires. Fix: `{{fixedName}}`.",
|
|
122
|
+
"make-handler": "Rename `{{name}}` to `{{handlerName}}`.",
|
|
122
123
|
"spread-handler":
|
|
123
|
-
"
|
|
124
|
+
"`{{name}}` is spread in from an object literal. Solid before 1.6 attaches no listener for a spread handler, which is what the `warnOnSpread` option checks. Fix: pass it as an attribute, `{{name}}={{{value}}}`."
|
|
124
125
|
}
|
|
125
126
|
},
|
|
126
127
|
defaultOptions: [],
|
|
127
128
|
create(context) {
|
|
128
129
|
const sourceCode = getSourceCode(context);
|
|
130
|
+
// `onClick="save"` most likely means the function `save`.
|
|
131
|
+
const handlerFor = (value) =>
|
|
132
|
+
typeof value === "string" && IDENTIFIER.test(value)
|
|
133
|
+
? value
|
|
134
|
+
: "(event) => { … }";
|
|
129
135
|
return {
|
|
130
136
|
JSXAttribute(node) {
|
|
131
137
|
const openingElement = node.parent;
|
|
@@ -170,7 +176,8 @@ export default {
|
|
|
170
176
|
messageId: "detected-attr",
|
|
171
177
|
data: {
|
|
172
178
|
name,
|
|
173
|
-
staticValue: staticValue.value
|
|
179
|
+
staticValue: staticValue.value,
|
|
180
|
+
handler: handlerFor(staticValue.value)
|
|
174
181
|
}
|
|
175
182
|
});
|
|
176
183
|
} else if (
|
|
@@ -178,13 +185,14 @@ export default {
|
|
|
178
185
|
node.value?.type === "Literal"
|
|
179
186
|
) {
|
|
180
187
|
// Check for same as above for literal values
|
|
188
|
+
const value = node.value !== null ? node.value.value : true;
|
|
181
189
|
context.report({
|
|
182
190
|
node,
|
|
183
191
|
messageId: "detected-attr",
|
|
184
192
|
data: {
|
|
185
193
|
name,
|
|
186
|
-
staticValue:
|
|
187
|
-
|
|
194
|
+
staticValue: value,
|
|
195
|
+
handler: handlerFor(value)
|
|
188
196
|
}
|
|
189
197
|
});
|
|
190
198
|
} else if (!context.options[0]?.ignoreCase) {
|
|
@@ -195,7 +203,11 @@ export default {
|
|
|
195
203
|
context.report({
|
|
196
204
|
node: node.name,
|
|
197
205
|
messageId: "nonstandard",
|
|
198
|
-
data: {
|
|
206
|
+
data: {
|
|
207
|
+
name,
|
|
208
|
+
fixedName,
|
|
209
|
+
event: name.slice(2).toLowerCase()
|
|
210
|
+
},
|
|
199
211
|
fix: (fixer) =>
|
|
200
212
|
fixer.replaceText(node.name, fixedName)
|
|
201
213
|
});
|
|
@@ -223,7 +235,11 @@ export default {
|
|
|
223
235
|
context.report({
|
|
224
236
|
node: node.name,
|
|
225
237
|
messageId: "naming",
|
|
226
|
-
data: {
|
|
238
|
+
data: {
|
|
239
|
+
name,
|
|
240
|
+
handlerName,
|
|
241
|
+
event: name.slice(2).toLowerCase()
|
|
242
|
+
},
|
|
227
243
|
suggest: [
|
|
228
244
|
{
|
|
229
245
|
messageId: "make-handler",
|
|
@@ -262,7 +278,11 @@ export default {
|
|
|
262
278
|
node,
|
|
263
279
|
messageId: "spread-handler",
|
|
264
280
|
data: {
|
|
265
|
-
name: node.key.name
|
|
281
|
+
name: node.key.name,
|
|
282
|
+
value: messageCode(
|
|
283
|
+
sourceCode.getText(node.value),
|
|
284
|
+
"handler"
|
|
285
|
+
)
|
|
266
286
|
},
|
|
267
287
|
*fix(fixer) {
|
|
268
288
|
const commaAfter =
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// A `.tsx`/`.jsx` file that exports a component is named after it
|
|
2
|
+
// (`Navigate.tsx` exports `Navigate`); one that only uses JSX stays snake_case
|
|
3
|
+
// (`solid_ssr_renderer.tsx`). unicorn/filename-case allows both cases for
|
|
4
|
+
// these files, this rule decides which one applies. Route folders follow the
|
|
5
|
+
// router's own naming and are skipped through `ignore`.
|
|
6
|
+
import { basename, relative } from "node:path";
|
|
7
|
+
|
|
8
|
+
const JSX_FILE = /\.[jt]sx$/u;
|
|
9
|
+
const PASCAL_NAME = /^[A-Z]/u;
|
|
10
|
+
// Router files follow the router's naming; fixtures and tests export many
|
|
11
|
+
// throwaway components and are named after what they test.
|
|
12
|
+
const DEFAULT_IGNORE = [
|
|
13
|
+
"**/pages/**",
|
|
14
|
+
"**/routes/**",
|
|
15
|
+
"**/*.fixture.*",
|
|
16
|
+
"**/*.test.*",
|
|
17
|
+
"**/*.spec.*"
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
function isFunction(node) {
|
|
21
|
+
let inner = node;
|
|
22
|
+
// `const Counter = (() => …) satisfies Component`
|
|
23
|
+
while (
|
|
24
|
+
inner?.type === "TSAsExpression" ||
|
|
25
|
+
inner?.type === "TSSatisfiesExpression" ||
|
|
26
|
+
inner?.type === "ParenthesizedExpression"
|
|
27
|
+
) {
|
|
28
|
+
inner = inner.expression;
|
|
29
|
+
}
|
|
30
|
+
return (
|
|
31
|
+
inner?.type === "ArrowFunctionExpression" ||
|
|
32
|
+
inner?.type === "FunctionExpression" ||
|
|
33
|
+
inner?.type === "FunctionDeclaration"
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ponytail: `**`, `*` and `?` only; no braces or character classes.
|
|
38
|
+
function globToRegExp(glob) {
|
|
39
|
+
const source = glob
|
|
40
|
+
.split(/(\*\*\/|\*\*|\*|\?)/u)
|
|
41
|
+
.map((part) => {
|
|
42
|
+
if (part === "**/") {
|
|
43
|
+
return "(?:.*/)?";
|
|
44
|
+
}
|
|
45
|
+
if (part === "**") {
|
|
46
|
+
return ".*";
|
|
47
|
+
}
|
|
48
|
+
if (part === "*") {
|
|
49
|
+
return "[^/]*";
|
|
50
|
+
}
|
|
51
|
+
if (part === "?") {
|
|
52
|
+
return "[^/]";
|
|
53
|
+
}
|
|
54
|
+
return part.replaceAll(/[.+^${}()|[\]\\]/gu, String.raw`\$&`);
|
|
55
|
+
})
|
|
56
|
+
.join("");
|
|
57
|
+
return new RegExp(`^${source}$`, "u");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function toSnakeCase(name) {
|
|
61
|
+
const leading = /^_*/u.exec(name)[0];
|
|
62
|
+
return (
|
|
63
|
+
leading +
|
|
64
|
+
name
|
|
65
|
+
.slice(leading.length)
|
|
66
|
+
.replaceAll(/([a-z0-9])([A-Z])/gu, "$1_$2")
|
|
67
|
+
.replaceAll(/([A-Z]+)([A-Z][a-z])/gu, "$1_$2")
|
|
68
|
+
.replaceAll(/[\s-]+/gu, "_")
|
|
69
|
+
.replaceAll(/_+/gu, "_")
|
|
70
|
+
.toLowerCase()
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Top-level names bound to a function, exported or not, so `export { C }`
|
|
75
|
+
// and `export default C` can be resolved.
|
|
76
|
+
function collectFunctionBindings(program) {
|
|
77
|
+
const functions = new Set();
|
|
78
|
+
for (const statement of program.body) {
|
|
79
|
+
const declaration =
|
|
80
|
+
statement.type === "ExportNamedDeclaration"
|
|
81
|
+
? statement.declaration
|
|
82
|
+
: statement;
|
|
83
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id) {
|
|
84
|
+
functions.add(declaration.id.name);
|
|
85
|
+
}
|
|
86
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
87
|
+
for (const declarator of declaration.declarations) {
|
|
88
|
+
if (
|
|
89
|
+
declarator.id.type === "Identifier" &&
|
|
90
|
+
isFunction(declarator.init)
|
|
91
|
+
) {
|
|
92
|
+
functions.add(declarator.id.name);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return functions;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The exported components: `{ name, node }` for every PascalCase export bound
|
|
101
|
+
// to a function, and the default export separately. Type-only exports are
|
|
102
|
+
// left out. A PascalCase re-export of a local module (`export { default as
|
|
103
|
+
// Counter } from "./CounterComponent"`) counts as the component it names: the
|
|
104
|
+
// module it comes from is not visible here, and the name is the convention.
|
|
105
|
+
function collectComponents(program) {
|
|
106
|
+
const functions = collectFunctionBindings(program);
|
|
107
|
+
const named = [];
|
|
108
|
+
let defaultComponent = null;
|
|
109
|
+
for (const statement of program.body) {
|
|
110
|
+
if (
|
|
111
|
+
statement.type === "ExportNamedDeclaration" &&
|
|
112
|
+
statement.exportKind !== "type" &&
|
|
113
|
+
statement.source === null
|
|
114
|
+
) {
|
|
115
|
+
const { declaration } = statement;
|
|
116
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id) {
|
|
117
|
+
named.push({ name: declaration.id.name, node: declaration.id });
|
|
118
|
+
}
|
|
119
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
120
|
+
for (const declarator of declaration.declarations) {
|
|
121
|
+
if (
|
|
122
|
+
declarator.id.type === "Identifier" &&
|
|
123
|
+
isFunction(declarator.init)
|
|
124
|
+
) {
|
|
125
|
+
named.push({
|
|
126
|
+
name: declarator.id.name,
|
|
127
|
+
node: declarator.id
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const specifier of statement.specifiers) {
|
|
133
|
+
if (
|
|
134
|
+
specifier.exportKind !== "type" &&
|
|
135
|
+
functions.has(specifier.local.name) &&
|
|
136
|
+
specifier.exported.type === "Identifier" &&
|
|
137
|
+
specifier.exported.name !== "default"
|
|
138
|
+
) {
|
|
139
|
+
named.push({
|
|
140
|
+
name: specifier.exported.name,
|
|
141
|
+
node: specifier.exported
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (
|
|
147
|
+
statement.type === "ExportNamedDeclaration" &&
|
|
148
|
+
statement.exportKind !== "type" &&
|
|
149
|
+
// A local module re-exported under its component's name; a
|
|
150
|
+
// re-export from a package (`export { Show } from "solid-js"`)
|
|
151
|
+
// is not this file's component.
|
|
152
|
+
typeof statement.source?.value === "string" &&
|
|
153
|
+
statement.source.value.startsWith(".")
|
|
154
|
+
) {
|
|
155
|
+
for (const specifier of statement.specifiers) {
|
|
156
|
+
if (
|
|
157
|
+
specifier.exportKind !== "type" &&
|
|
158
|
+
specifier.exported.type === "Identifier" &&
|
|
159
|
+
/^[A-Z]/u.test(specifier.exported.name)
|
|
160
|
+
) {
|
|
161
|
+
named.push({
|
|
162
|
+
name: specifier.exported.name,
|
|
163
|
+
node: specifier.exported
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
169
|
+
const { declaration } = statement;
|
|
170
|
+
if (isFunction(declaration) && declaration.id) {
|
|
171
|
+
defaultComponent = {
|
|
172
|
+
name: declaration.id.name,
|
|
173
|
+
node: declaration.id
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (
|
|
177
|
+
declaration.type === "Identifier" &&
|
|
178
|
+
functions.has(declaration.name)
|
|
179
|
+
) {
|
|
180
|
+
defaultComponent = {
|
|
181
|
+
name: declaration.name,
|
|
182
|
+
node: declaration
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const isComponent = (entry) => PASCAL_NAME.test(entry.name);
|
|
188
|
+
return {
|
|
189
|
+
defaultComponent:
|
|
190
|
+
defaultComponent !== null && isComponent(defaultComponent)
|
|
191
|
+
? defaultComponent
|
|
192
|
+
: null,
|
|
193
|
+
named: named.filter(isComponent)
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export default {
|
|
198
|
+
meta: {
|
|
199
|
+
type: "suggestion",
|
|
200
|
+
docs: {
|
|
201
|
+
description:
|
|
202
|
+
"Name a `.tsx`/`.jsx` file after the component it exports, and keep files without a component snake_case.",
|
|
203
|
+
url: "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/oxlint-plugin-solid#solidfilename-matches-export"
|
|
204
|
+
},
|
|
205
|
+
schema: [
|
|
206
|
+
{
|
|
207
|
+
type: "object",
|
|
208
|
+
properties: {
|
|
209
|
+
ignore: {
|
|
210
|
+
type: "array",
|
|
211
|
+
items: { type: "string" },
|
|
212
|
+
description:
|
|
213
|
+
"Globs of files the rule skips; route folders follow the router's naming."
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
additionalProperties: false
|
|
217
|
+
}
|
|
218
|
+
],
|
|
219
|
+
messages: {
|
|
220
|
+
componentName:
|
|
221
|
+
"The file `{{file}}` exports the component `{{component}}`, so imports and the file tree disagree on its name and searching for the component does not find the file. Fix: rename the file to `{{expected}}`. Do not: rename the export to match the file; the component's name is the source of truth.",
|
|
222
|
+
snakeCase:
|
|
223
|
+
"The file `{{file}}` exports no component, but a PascalCase name marks a component file, so readers and agents look for a component that is not there. Fix: rename the file to `{{expected}}`. Do not: export a component only to justify the name."
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
defaultOptions: [],
|
|
227
|
+
create(context) {
|
|
228
|
+
const filename = context.filename ?? context.getFilename();
|
|
229
|
+
const file = basename(filename);
|
|
230
|
+
if (!JSX_FILE.test(file)) {
|
|
231
|
+
return {};
|
|
232
|
+
}
|
|
233
|
+
const ignore = (context.options[0]?.ignore ?? DEFAULT_IGNORE).map(
|
|
234
|
+
globToRegExp
|
|
235
|
+
);
|
|
236
|
+
const paths = [
|
|
237
|
+
filename.replaceAll("\\", "/"),
|
|
238
|
+
relative(context.cwd ?? process.cwd(), filename).replaceAll(
|
|
239
|
+
"\\",
|
|
240
|
+
"/"
|
|
241
|
+
)
|
|
242
|
+
];
|
|
243
|
+
if (ignore.some((glob) => paths.some((path) => glob.test(path)))) {
|
|
244
|
+
return {};
|
|
245
|
+
}
|
|
246
|
+
// `Counter.island.tsx`, `island_hydration.test.tsx`: the name is the
|
|
247
|
+
// part before the first dot, the rest stays as it is.
|
|
248
|
+
const dot = file.indexOf(".");
|
|
249
|
+
const base = file.slice(0, dot);
|
|
250
|
+
const rest = file.slice(dot);
|
|
251
|
+
if (base === "index") {
|
|
252
|
+
return {};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
Program(program) {
|
|
257
|
+
const { defaultComponent, named } = collectComponents(program);
|
|
258
|
+
if (defaultComponent !== null || named.length > 0) {
|
|
259
|
+
const target = defaultComponent ?? named[0];
|
|
260
|
+
if (
|
|
261
|
+
defaultComponent === null
|
|
262
|
+
? named.some((entry) => entry.name === base)
|
|
263
|
+
: defaultComponent.name === base
|
|
264
|
+
) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
context.report({
|
|
268
|
+
data: {
|
|
269
|
+
component: target.name,
|
|
270
|
+
expected: `${target.name}${rest}`,
|
|
271
|
+
file
|
|
272
|
+
},
|
|
273
|
+
messageId: "componentName",
|
|
274
|
+
node: target.node
|
|
275
|
+
});
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const expected = toSnakeCase(base);
|
|
279
|
+
if (expected !== base) {
|
|
280
|
+
context.report({
|
|
281
|
+
data: { expected: `${expected}${rest}`, file },
|
|
282
|
+
loc: {
|
|
283
|
+
end: { column: 0, line: 1 },
|
|
284
|
+
start: { column: 0, line: 1 }
|
|
285
|
+
},
|
|
286
|
+
messageId: "snakeCase"
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// eslint's max-lines-per-function (skipBlankLines, skipComments, IIFEs) with a
|
|
2
|
+
// message per kind of function. oxlint can neither change a built-in rule's
|
|
3
|
+
// message nor wrap it from a JS plugin, so the counting is redone here. A Solid
|
|
4
|
+
// component runs once, so extracting from it costs nothing; the messages name
|
|
5
|
+
// what to extract and where, and the two ways extraction breaks reactivity
|
|
6
|
+
// (destructured props, a primitive that gets a value instead of an accessor).
|
|
7
|
+
import { getFunctionHeadLocation } from "@eslint-community/eslint-utils";
|
|
8
|
+
|
|
9
|
+
import { getSourceCode } from "../compat.mjs";
|
|
10
|
+
import { getFunctionName } from "../utils.mjs";
|
|
11
|
+
|
|
12
|
+
const DEFAULT_MAX = 60;
|
|
13
|
+
const DEFAULT_MAX_COMPONENT = 80;
|
|
14
|
+
const COMPONENT_NAME = /^[A-Z]/u;
|
|
15
|
+
const PRIMITIVE_NAME = /^(?:create|use)[A-Z0-9]/u;
|
|
16
|
+
const JSX_FILE = /\.[jt]sx$/u;
|
|
17
|
+
const DO_NOT_SPLIT =
|
|
18
|
+
"If splitting would scatter one cohesive unit, leave it and add a one-line comment above `{{name}}` saying why.";
|
|
19
|
+
|
|
20
|
+
// Code lines of the file as a prefix sum: `counts[line]` is the number of
|
|
21
|
+
// lines up to and including `line` that keep text once comments are blanked.
|
|
22
|
+
function countCodeLines(sourceCode) {
|
|
23
|
+
const text = sourceCode.text.split("");
|
|
24
|
+
const comments = sourceCode.getAllComments?.() ?? sourceCode.ast.comments;
|
|
25
|
+
for (const comment of comments) {
|
|
26
|
+
const [start, end] = comment.range ?? [comment.start, comment.end];
|
|
27
|
+
for (let index = start; index < end; index += 1) {
|
|
28
|
+
if (text[index] !== "\n") {
|
|
29
|
+
text[index] = " ";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const counts = [0];
|
|
34
|
+
for (const line of text.join("").split("\n")) {
|
|
35
|
+
counts.push(counts.at(-1) + (line.trim() === "" ? 0 : 1));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return counts;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function nameOf(node, sourceCode) {
|
|
42
|
+
const name = getFunctionName(node);
|
|
43
|
+
if (name !== null) {
|
|
44
|
+
return name;
|
|
45
|
+
}
|
|
46
|
+
const { parent } = node;
|
|
47
|
+
if (
|
|
48
|
+
(parent.type === "Property" ||
|
|
49
|
+
parent.type === "MethodDefinition" ||
|
|
50
|
+
parent.type === "PropertyDefinition") &&
|
|
51
|
+
!parent.computed
|
|
52
|
+
) {
|
|
53
|
+
return parent.key.name ?? String(parent.key.value);
|
|
54
|
+
}
|
|
55
|
+
if (parent.type === "AssignmentExpression") {
|
|
56
|
+
return sourceCode.getText(parent.left);
|
|
57
|
+
}
|
|
58
|
+
if (parent.type === "CallExpression" && parent.callee !== node) {
|
|
59
|
+
return `${sourceCode.getText(parent.callee)} callback`;
|
|
60
|
+
}
|
|
61
|
+
if (parent.type === "ExportDefaultDeclaration") {
|
|
62
|
+
return "default export";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return "anonymous function";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default {
|
|
69
|
+
meta: {
|
|
70
|
+
type: "suggestion",
|
|
71
|
+
docs: {
|
|
72
|
+
description:
|
|
73
|
+
"Limit the code lines of a function, with a stricter message for components and `createX` primitives.",
|
|
74
|
+
url: "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/oxlint-plugin-solid#solidfunction-size"
|
|
75
|
+
},
|
|
76
|
+
schema: [
|
|
77
|
+
{
|
|
78
|
+
type: "object",
|
|
79
|
+
properties: {
|
|
80
|
+
max: {
|
|
81
|
+
type: "integer",
|
|
82
|
+
minimum: 1,
|
|
83
|
+
description:
|
|
84
|
+
"Code lines for a function or primitive (blank and comment lines don't count)."
|
|
85
|
+
},
|
|
86
|
+
maxComponent: {
|
|
87
|
+
type: "integer",
|
|
88
|
+
minimum: 1,
|
|
89
|
+
description:
|
|
90
|
+
"Code lines for a component: a PascalCase function with JSX in a `.tsx`/`.jsx` file."
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
additionalProperties: false
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
messages: {
|
|
97
|
+
function: `\`{{name}}\` has {{lines}} code lines (max {{max}}; blank and comment lines don't count), so several jobs share one body and none can be tested or reused alone. Fix: move each self-contained step (lines {{start}}–{{end}} first) into a named pure helper that takes its inputs as parameters and returns a value; put a helper used by more than one file in its own snake_case \`.ts\`. ${DO_NOT_SPLIT} Do not: delete comments, join lines, split into \`{{name}}Part1\`/\`Part2\` that just run in sequence, or add \`// oxlint-disable\`.`,
|
|
98
|
+
component: `Component \`{{name}}\` has {{lines}} code lines (max {{max}}), so several pieces of UI and their state can't be reused or tested separately. Fix: move repeated or self-contained JSX into a sub-component in its own PascalCase \`.tsx\` that receives values (\`<Slot label={label()} />\`, read as \`props.label\`); move signals, memos and effects that belong together into a \`createX\` primitive in a snake_case \`.ts\` that takes accessors (\`createX(() => props.id)\`) and returns accessors. ${DO_NOT_SPLIT} Do not: destructure props, pass \`props.x\` or \`count()\` into a helper or primitive (it is read once), delete comments, join lines, or add \`// oxlint-disable\`.`,
|
|
99
|
+
primitive: `Primitive \`{{name}}\` has {{lines}} code lines (max {{max}}; blank and comment lines don't count), so several concerns share one owner and none can be reused or tested alone. Fix: split it by concern into smaller \`createX\` primitives (lines {{start}}–{{end}} first) in snake_case \`.ts\` files that take accessors (\`createX(() => props.id)\`) and return accessors, and let \`{{name}}\` compose them. ${DO_NOT_SPLIT} Do not: pass \`props.x\` or \`count()\` into a primitive (it is read once), return plain values instead of accessors, delete comments, join lines, or add \`// oxlint-disable\`.`
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
defaultOptions: [],
|
|
103
|
+
create(context) {
|
|
104
|
+
const sourceCode = getSourceCode(context);
|
|
105
|
+
const max = context.options[0]?.max ?? DEFAULT_MAX;
|
|
106
|
+
const maxComponent =
|
|
107
|
+
context.options[0]?.maxComponent ?? DEFAULT_MAX_COMPONENT;
|
|
108
|
+
const jsxFile = JSX_FILE.test(
|
|
109
|
+
context.filename ?? context.getFilename()
|
|
110
|
+
);
|
|
111
|
+
const smallest = Math.min(max, jsxFile ? maxComponent : max);
|
|
112
|
+
// One frame per open function; JSX marks every enclosing frame.
|
|
113
|
+
const stack = [];
|
|
114
|
+
let counts = null;
|
|
115
|
+
const codeLines = (node) => {
|
|
116
|
+
counts ??= countCodeLines(sourceCode);
|
|
117
|
+
|
|
118
|
+
return counts[node.loc.end.line] - counts[node.loc.start.line - 1];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// The longest top-level statement is where an agent starts extracting.
|
|
122
|
+
const longestStatement = (node) => {
|
|
123
|
+
const statements =
|
|
124
|
+
node.body.type === "BlockStatement" && node.body.body.length > 0
|
|
125
|
+
? node.body.body
|
|
126
|
+
: [node.body];
|
|
127
|
+
|
|
128
|
+
return statements.reduce((longest, statement) => {
|
|
129
|
+
return codeLines(statement) > codeLines(longest)
|
|
130
|
+
? statement
|
|
131
|
+
: longest;
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const exit = (node) => {
|
|
136
|
+
const { hasJsx } = stack.pop();
|
|
137
|
+
if (
|
|
138
|
+
node.body === null ||
|
|
139
|
+
node.loc.end.line - node.loc.start.line + 1 <= smallest
|
|
140
|
+
) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const name = nameOf(node, sourceCode);
|
|
144
|
+
const component = jsxFile && hasJsx && COMPONENT_NAME.test(name);
|
|
145
|
+
const limit = component ? maxComponent : max;
|
|
146
|
+
const lines = codeLines(node);
|
|
147
|
+
if (lines <= limit) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const start = longestStatement(node);
|
|
151
|
+
let messageId = "function";
|
|
152
|
+
if (component) {
|
|
153
|
+
messageId = "component";
|
|
154
|
+
} else if (PRIMITIVE_NAME.test(name)) {
|
|
155
|
+
messageId = "primitive";
|
|
156
|
+
}
|
|
157
|
+
context.report({
|
|
158
|
+
data: {
|
|
159
|
+
end: start.loc.end.line,
|
|
160
|
+
lines,
|
|
161
|
+
max: limit,
|
|
162
|
+
name,
|
|
163
|
+
start: start.loc.start.line
|
|
164
|
+
},
|
|
165
|
+
loc: getFunctionHeadLocation(node, sourceCode),
|
|
166
|
+
messageId
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
const enter = () => {
|
|
170
|
+
stack.push({ hasJsx: false });
|
|
171
|
+
};
|
|
172
|
+
const markJsx = () => {
|
|
173
|
+
for (const frame of stack) {
|
|
174
|
+
frame.hasJsx = true;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
ArrowFunctionExpression: enter,
|
|
180
|
+
"ArrowFunctionExpression:exit": exit,
|
|
181
|
+
FunctionDeclaration: enter,
|
|
182
|
+
"FunctionDeclaration:exit": exit,
|
|
183
|
+
FunctionExpression: enter,
|
|
184
|
+
"FunctionExpression:exit": exit,
|
|
185
|
+
JSXElement: markJsx,
|
|
186
|
+
JSXFragment: markJsx
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
};
|
package/src/rules/imports.mjs
CHANGED
|
@@ -141,7 +141,8 @@ export default {
|
|
|
141
141
|
fixable: "code",
|
|
142
142
|
schema: [],
|
|
143
143
|
messages: {
|
|
144
|
-
"prefer-source":
|
|
144
|
+
"prefer-source":
|
|
145
|
+
"`{{name}}` is imported from `{{from}}`, but Solid 2.0 defines it in `{{source}}`; where `{{from}}` does not re-export it, the import is a build error or `undefined`. Fix: `{{fix}}`."
|
|
145
146
|
}
|
|
146
147
|
},
|
|
147
148
|
defaultOptions: [],
|
|
@@ -163,7 +164,9 @@ export default {
|
|
|
163
164
|
messageId: "prefer-source",
|
|
164
165
|
data: {
|
|
165
166
|
name: specifier.imported.name,
|
|
166
|
-
|
|
167
|
+
from: source,
|
|
168
|
+
source: correctSource,
|
|
169
|
+
fix: `import ${isType ? "type " : ""}{ ${specifierText(specifier, false)} } from "${correctSource}";`
|
|
167
170
|
},
|
|
168
171
|
fix(fixer) {
|
|
169
172
|
const sourceCode = getSourceCode(context);
|