@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.
Files changed (44) hide show
  1. package/README.md +161 -16
  2. package/THIRD_PARTY_NOTICES.md +7 -4
  3. package/package.json +5 -2
  4. package/src/compat.mjs +3 -0
  5. package/src/index.mjs +23 -0
  6. package/src/rules/components_return_once.mjs +131 -84
  7. package/src/rules/event_handlers.mjs +33 -13
  8. package/src/rules/filename_matches_export.mjs +292 -0
  9. package/src/rules/function_size.mjs +189 -0
  10. package/src/rules/imports.mjs +5 -2
  11. package/src/rules/init_declarations.mjs +139 -0
  12. package/src/rules/jsx_no_duplicate_props.mjs +36 -8
  13. package/src/rules/jsx_no_script_url.mjs +3 -2
  14. package/src/rules/jsx_no_undef.mjs +8 -11
  15. package/src/rules/no_array_handlers.mjs +35 -6
  16. package/src/rules/no_cleanup_in_effect_apply.mjs +154 -0
  17. package/src/rules/no_destructure.mjs +76 -4
  18. package/src/rules/no_innerhtml.mjs +51 -18
  19. package/src/rules/no_module_scope_reactive_primitive.mjs +73 -0
  20. package/src/rules/no_owned_primitives_in_ref.mjs +23 -2
  21. package/src/rules/no_proxy_apis.mjs +41 -12
  22. package/src/rules/no_react_deps.mjs +394 -38
  23. package/src/rules/no_react_specific_props.mjs +33 -4
  24. package/src/rules/no_server_import_in_client.mjs +1 -1
  25. package/src/rules/no_setter_in_effect.mjs +145 -22
  26. package/src/rules/no_single_arg_create_effect.mjs +55 -0
  27. package/src/rules/no_unassigned_vars.mjs +99 -0
  28. package/src/rules/no_unknown_namespaces.mjs +96 -17
  29. package/src/rules/no_untracked_effect_read.mjs +65 -28
  30. package/src/rules/no_write_in_pure_computation.mjs +273 -0
  31. package/src/rules/prefer_arrow_components.mjs +30 -4
  32. package/src/rules/prefer_class_object.mjs +37 -5
  33. package/src/rules/prefer_destructuring.mjs +164 -0
  34. package/src/rules/prefer_for.mjs +34 -4
  35. package/src/rules/prefer_ref_shorthand.mjs +142 -0
  36. package/src/rules/prefer_show.mjs +48 -17
  37. package/src/rules/reactive_async.mjs +55 -8
  38. package/src/rules/reactivity.mjs +1160 -291
  39. package/src/rules/self_closing_comp.mjs +7 -2
  40. package/src/rules/strict_void_return.mjs +43 -13
  41. package/src/rules/style_prop.mjs +38 -9
  42. package/src/rules/typed_merge_defaults.mjs +88 -0
  43. package/src/rules/validate_jsx_nesting.mjs +2 -2
  44. package/src/utils.mjs +51 -20
@@ -60,8 +60,10 @@ export default {
60
60
  }
61
61
  ],
62
62
  messages: {
63
- selfClose: "Empty components are self-closing.",
64
- dontSelfClose: "This element should not be self-closing."
63
+ selfClose:
64
+ "`<{{name}}></{{name}}>` has no children, so the closing tag only adds noise. Fix: `<{{name}} />`.",
65
+ dontSelfClose:
66
+ "`<{{name}} />` is written self-closing, and this rule's `component`/`html` options ask for an explicit closing tag on `<{{name}}>`. Fix: `<{{name}}></{{name}}>`."
65
67
  }
66
68
  },
67
69
  defaultOptions: [],
@@ -93,10 +95,12 @@ export default {
93
95
  if (canSelfClose) {
94
96
  const shouldSelfClose =
95
97
  shouldBeSelfClosedWhenPossible(node);
98
+ const name = getSourceCode(context).getText(node.name);
96
99
  if (shouldSelfClose && !node.selfClosing) {
97
100
  context.report({
98
101
  node,
99
102
  messageId: "selfClose",
103
+ data: { name },
100
104
  fix(fixer) {
101
105
  // Represents the last character of the JSXOpeningElement, the '>' character
102
106
  const openingElementEnding = node.range[1] - 1;
@@ -115,6 +119,7 @@ export default {
115
119
  context.report({
116
120
  node,
117
121
  messageId: "dontSelfClose",
122
+ data: { name },
118
123
  fix(fixer) {
119
124
  const sourceCode = getSourceCode(context);
120
125
  const tagName = sourceCode.getText(node.name);
@@ -1,4 +1,10 @@
1
- import { isFunctionNode, jsxPropName, trackImports } from "../utils.mjs";
1
+ import { getSourceCode } from "../compat.mjs";
2
+ import {
3
+ isFunctionNode,
4
+ jsxPropName,
5
+ messageCode,
6
+ trackImports
7
+ } from "../utils.mjs";
2
8
 
3
9
  const EVENT_HANDLER_NAME = /^on(?::|[A-Z])/;
4
10
  const DOM_EVENT_NAME = /^on[a-z]/;
@@ -25,6 +31,16 @@ function isVoidExpression(expression) {
25
31
  );
26
32
  }
27
33
 
34
+ // `(el) => (input = el)` as `(el) => { input = el; }`: the arrow up to its
35
+ // `=>`, then the body as a statement. Parentheses around the body are not in
36
+ // its range, so they are dropped with everything between `=>` and the body.
37
+ function blockBody(callback, sourceCode) {
38
+ const text = sourceCode.getText(callback);
39
+ const head = text.slice(0, callback.body.range[0] - callback.range[0]);
40
+ const arrow = head.slice(0, head.lastIndexOf("=>") + 2);
41
+ return `${arrow} { ${sourceCode.getText(callback.body)}; }`;
42
+ }
43
+
28
44
  function describe(expression) {
29
45
  return expression.type === "CallExpression"
30
46
  ? "the call's result"
@@ -84,16 +100,17 @@ export const strictVoidReturnRule = {
84
100
  schema: [],
85
101
  messages: {
86
102
  handlerReturnsValue:
87
- "This `{{attribute}}` callback is an expression-bodied arrow, so it returns {{what}}, which Solid ignores. Write a block body (`() => { … }`) or discard it explicitly with `void …`.",
103
+ "The `{{attribute}}` callback is an expression-bodied arrow, so it returns {{what}}, which Solid ignores: a returned cleanup, promise or `false` does nothing there. Fix: a block body, `{{attribute}}={{{fix}}}`.",
88
104
  cleanupReturnsValue:
89
- "This `{{primitive}}` callback returns a value, which is ignored. Remove the `return`, or use a block body that does not return.",
105
+ "The `{{primitive}}` callback returns `{{value}}`, which Solid ignores. Fix: {{fix}}",
90
106
  settledReturnsValue:
91
- "This `onSettled` callback returns a value that is not a function. Only a returned cleanup function is used; remove the `return` or return `() => { … }`."
107
+ "The `onSettled` callback returns `{{value}}`, which is not a function. Solid only uses a returned cleanup function and ignores anything else. Fix: return nothing, or return the cleanup, `return () => { … };`."
92
108
  }
93
109
  },
94
110
  defaultOptions: [],
95
111
  create(context) {
96
112
  const { matchImport, handleImportDeclaration } = trackImports();
113
+ const sourceCode = getSourceCode(context);
97
114
 
98
115
  function checkHandler(callback, attribute) {
99
116
  if (
@@ -109,7 +126,11 @@ export const strictVoidReturnRule = {
109
126
  messageId: "handlerReturnsValue",
110
127
  data: {
111
128
  attribute,
112
- what: describe(callback.body)
129
+ what: describe(callback.body),
130
+ fix: messageCode(
131
+ blockBody(callback, sourceCode),
132
+ "(event) => { … }"
133
+ )
113
134
  }
114
135
  });
115
136
  }
@@ -124,20 +145,29 @@ export const strictVoidReturnRule = {
124
145
 
125
146
  // A call may well return nothing (`() => clearInterval(id)`); without
126
147
  // types only values that are certainly not void are reported.
127
- if (
128
- values.some(
129
- (value) =>
130
- PLAIN_VALUE_TYPES.has(value.type) &&
131
- !isVoidExpression(value)
132
- )
133
- ) {
148
+ const returned = values.find(
149
+ (value) =>
150
+ PLAIN_VALUE_TYPES.has(value.type) &&
151
+ !isVoidExpression(value)
152
+ );
153
+ if (returned) {
154
+ const value = messageCode(sourceCode.getText(returned), "…");
134
155
  context.report({
135
156
  node: callback,
136
157
  messageId:
137
158
  primitive === "onSettled"
138
159
  ? "settledReturnsValue"
139
160
  : "cleanupReturnsValue",
140
- data: { primitive }
161
+ data: {
162
+ primitive,
163
+ value,
164
+ // An expression body becomes a statement; a block
165
+ // keeps its code and drops the returned value.
166
+ fix:
167
+ callback.body.type === "BlockStatement"
168
+ ? `drop the value, \`return;\` in place of \`return ${value};\`.`
169
+ : `a block body, \`${primitive}(${messageCode(blockBody(callback, sourceCode), "() => { … }")})\`.`
170
+ }
141
171
  });
142
172
  }
143
173
  }
@@ -6,8 +6,8 @@ import kebabCase from "kebab-case";
6
6
  import { all as allCssProperties } from "known-css-properties";
7
7
  import parse from "style-to-object";
8
8
 
9
- import { getScope } from "../compat.mjs";
10
- import { jsxPropName } from "../utils.mjs";
9
+ import { getScope, getSourceCode } from "../compat.mjs";
10
+ import { jsxPropName, messageCode } from "../utils.mjs";
11
11
  const lengthPercentageRegex =
12
12
  /\b(?:width|height|margin|padding|border-width|font-size)\b/i;
13
13
  export default {
@@ -46,11 +46,14 @@ export default {
46
46
  }
47
47
  ],
48
48
  messages: {
49
- kebabStyleProp: "Use {{ kebabName }} instead of {{ name }}.",
50
- invalidStyleProp: "{{ name }} is not a valid CSS property.",
49
+ kebabStyleProp:
50
+ "`{{ name }}` is React's camelCase. Solid passes each key to `style.setProperty`, which only takes CSS names, so this style is dropped. Fix: `{{ fix }}`.",
51
+ invalidStyleProp:
52
+ "`{{ name }}` is not a CSS property, so `style.setProperty` drops it. Fix: correct the spelling, or start a custom property with `--` (`--{{ name }}`).",
51
53
  numericStyleValue:
52
- 'This CSS property value should be a string with a unit; Solid does not automatically append a "px" unit.',
53
- stringStyle: "Use an object for the style prop instead of a string."
54
+ "`{{ name }}: {{ value }}` is a number without a unit. Solid passes it to `style.setProperty` as is, without adding `px`, and the browser drops a unitless length. Fix: `{{ fix }}`.",
55
+ stringStyle:
56
+ "`style` gets a string, which Solid sets as the whole `cssText`, so a change rewrites every property; an object lets Solid update the single property that changed. Fix: `{{ fix }}`."
54
57
  }
55
58
  },
56
59
  defaultOptions: [],
@@ -58,6 +61,7 @@ export default {
58
61
  const allCssPropertiesSet = new Set(allCssProperties);
59
62
  const allowString = Boolean(context.options[0]?.allowString);
60
63
  const styleProps = context.options[0]?.styleProps || ["style"];
64
+ const sourceCode = getSourceCode(context);
61
65
  return {
62
66
  JSXAttribute(node) {
63
67
  if (styleProps.indexOf(jsxPropName(node)) === -1) {
@@ -84,6 +88,14 @@ export default {
84
88
  context.report({
85
89
  node: style,
86
90
  messageId: "stringStyle",
91
+ data: {
92
+ fix: messageCode(
93
+ objectStyles
94
+ ? `style={${JSON.stringify(objectStyles)}}`
95
+ : "",
96
+ 'style={{ "font-size": "10px" }}'
97
+ )
98
+ },
87
99
  // replace full prop value, wrap in JSXExpressionContainer, more fixes may be applied below
88
100
  fix:
89
101
  objectStyles &&
@@ -96,7 +108,10 @@ export default {
96
108
  } else if (style.type === "TemplateLiteral" && !allowString) {
97
109
  context.report({
98
110
  node: style,
99
- messageId: "stringStyle"
111
+ messageId: "stringStyle",
112
+ data: {
113
+ fix: 'style={{ "font-size": `${size()}px` }}'
114
+ }
100
115
  });
101
116
  } else if (style.type === "ObjectExpression") {
102
117
  const properties = style.properties.filter(
@@ -118,7 +133,13 @@ export default {
118
133
  context.report({
119
134
  node: prop.key,
120
135
  messageId: "kebabStyleProp",
121
- data: { name, kebabName },
136
+ data: {
137
+ name,
138
+ fix: messageCode(
139
+ `"${kebabName}": ${sourceCode.getText(prop.value)}`,
140
+ `"${kebabName}": …`
141
+ )
142
+ },
122
143
  fix: (fixer) =>
123
144
  fixer.replaceText(
124
145
  prop.key,
@@ -141,9 +162,17 @@ export default {
141
162
  // and suggests quoting or appending 'px'
142
163
  const value = getStaticValue(prop.value)?.value;
143
164
  if (typeof value === "number" && value !== 0) {
165
+ const key = name
166
+ ? `"${name}"`
167
+ : `[${sourceCode.getText(prop.key)}]`;
144
168
  context.report({
145
169
  node: prop.value,
146
- messageId: "numericStyleValue"
170
+ messageId: "numericStyleValue",
171
+ data: {
172
+ name: key,
173
+ value: sourceCode.getText(prop.value),
174
+ fix: `${key}: "${value}px"`
175
+ }
147
176
  });
148
177
  }
149
178
  }
@@ -0,0 +1,88 @@
1
+ // An object literal passed to `merge` loses its types: TypeScript widens
2
+ // `{ variant: "primary" }` to `string`, and annotating the defaults as
3
+ // `Partial<Props>` makes every defaulted prop optional again. Writing the
4
+ // literal with `satisfies Partial<Props>` keeps the literal types and checks
5
+ // the keys against the props; `as const` keeps the literal types too.
6
+ import { getSourceCode } from "../compat.mjs";
7
+ import { trackImports } from "../utils.mjs";
8
+
9
+ function isAsConst(node) {
10
+ return (
11
+ node.type === "TSAsExpression" &&
12
+ node.typeAnnotation.type === "TSTypeReference" &&
13
+ node.typeAnnotation.typeName.type === "Identifier" &&
14
+ node.typeAnnotation.typeName.name === "const"
15
+ );
16
+ }
17
+
18
+ export default {
19
+ meta: {
20
+ type: "suggestion",
21
+ docs: {
22
+ description:
23
+ "Require `satisfies Partial<Props>` (or `as const`) on object literals passed to `merge`, so the defaults keep their types."
24
+ },
25
+ schema: [],
26
+ messages: {
27
+ untypedDefaults:
28
+ 'The defaults `{{object}}` passed to `{{name}}` have no checked type, so their literals widen (`"primary"` becomes `string`) and their keys are not checked against the props. Fix: `{{object}} satisfies {{type}}`{{where}}. Do not: write `as Partial<Props>` or use a `Partial<Props>` variable; both make the defaulted props optional again.'
29
+ }
30
+ },
31
+ defaultOptions: [],
32
+ create(context) {
33
+ const { matchImport, handleImportDeclaration } = trackImports();
34
+ const sourceCode = getSourceCode(context);
35
+ // A one-line literal is quoted as written; a longer one is elided.
36
+ const quote = (node) => {
37
+ const text = sourceCode.getText(node);
38
+ return text.includes("\n") ? "{ … }" : text;
39
+ };
40
+ return {
41
+ ImportDeclaration: handleImportDeclaration,
42
+ CallExpression(node) {
43
+ if (node.callee.type !== "Identifier") {
44
+ return;
45
+ }
46
+ // `@solidjs/web` re-exports `merge` as `mergeProps`.
47
+ const name = matchImport(
48
+ ["merge", "mergeProps"],
49
+ node.callee.name
50
+ );
51
+ if (!name) {
52
+ return;
53
+ }
54
+ for (const argument of node.arguments) {
55
+ const untyped =
56
+ argument.type === "ObjectExpression" ||
57
+ (argument.type === "TSAsExpression" &&
58
+ !isAsConst(argument) &&
59
+ argument.expression.type === "ObjectExpression");
60
+ if (untyped) {
61
+ const literal =
62
+ argument.type === "TSAsExpression"
63
+ ? argument.expression
64
+ : argument;
65
+ const asType =
66
+ argument.type === "TSAsExpression"
67
+ ? sourceCode.getText(argument.typeAnnotation)
68
+ : null;
69
+ context.report({
70
+ data: {
71
+ name: node.callee.name,
72
+ object: quote(literal),
73
+ // `{ … } as Partial<ButtonProps>` names the type.
74
+ type: asType ?? "Partial<Props>",
75
+ where:
76
+ asType === null
77
+ ? ", with `Props` the props type of the component"
78
+ : ""
79
+ },
80
+ messageId: "untypedDefaults",
81
+ node: argument
82
+ });
83
+ }
84
+ }
85
+ }
86
+ };
87
+ }
88
+ };
@@ -431,9 +431,9 @@ export default {
431
431
  },
432
432
  messages: {
433
433
  invalidNesting:
434
- "Invalid HTML nesting: `<{{child}}>` cannot be a child of `<{{parent}}>`. The HTML parser restructures this, so the rendered DOM won't match this markup.",
434
+ "Invalid HTML nesting: `<{{child}}>` cannot be a child of `<{{parent}}>`. The HTML parser restructures this, so the rendered DOM won't match this markup. Fix: move the `<{{child}}>` out of the `<{{parent}}>`, or change one of the two tags so HTML allows the nesting.",
435
435
  invalidNestingInserted:
436
- "Invalid HTML nesting: `<{{child}}>` cannot be a child of `<{{parent}}>`. The client inserts it as written, but the HTML parser restructures it in server-rendered HTML, so hydration won't match this markup."
436
+ "Invalid HTML nesting: `<{{child}}>` cannot be a child of `<{{parent}}>`. The client inserts it as written, but the HTML parser restructures it in server-rendered HTML, so hydration won't match this markup. Fix: move the `<{{child}}>` out of the `<{{parent}}>`, or change one of the two tags so HTML allows the nesting."
437
437
  },
438
438
  schema: []
439
439
  },
package/src/utils.mjs CHANGED
@@ -13,30 +13,33 @@ export function isDOMElementName(name) {
13
13
  return DOM_ELEMENT_REGEX.test(name);
14
14
  }
15
15
 
16
- export function isPropsByName(name) {
17
- return PROPS_REGEX.test(name);
18
- }
19
-
20
- export function formatList(strings) {
21
- if (strings.length === 0) {
22
- return "";
16
+ /** `const [value, setValue] = createSignal(...)` -> "value". */
17
+ export function getGetterName(declarator) {
18
+ if (declarator.id.type !== "ArrayPattern") {
19
+ return null;
23
20
  }
24
21
 
25
- if (strings.length === 1) {
26
- return `'${strings[0]}'`;
27
- }
22
+ const [element] = declarator.id.elements;
28
23
 
29
- if (strings.length === 2) {
30
- return `'${strings[0]}' and '${strings[1]}'`;
31
- }
24
+ return element?.type === "Identifier" ? element.name : null;
25
+ }
32
26
 
33
- const last = strings.length - 1;
34
- return `${strings
35
- .slice(0, last)
36
- .map((stringValue) => {
37
- return `'${stringValue}'`;
38
- })
39
- .join(", ")}, and '${strings[last]}'`;
27
+ /** `count` -> `count`, `props.value` -> `value`. */
28
+ export function localName(name) {
29
+ return name.slice(name.lastIndexOf(".") + 1);
30
+ }
31
+
32
+ export function isPropsByName(name) {
33
+ return PROPS_REGEX.test(name);
34
+ }
35
+
36
+ // Code quoted in a lint message: the text itself when it fits on one short
37
+ // line, else the fallback pattern, so a message never carries a whole block.
38
+ const MESSAGE_CODE_MAX_LENGTH = 100;
39
+ export function messageCode(text, fallback) {
40
+ return text.length <= MESSAGE_CODE_MAX_LENGTH && !text.includes("\n")
41
+ ? text
42
+ : fallback;
40
43
  }
41
44
 
42
45
  export function find(node, predicate) {
@@ -158,6 +161,34 @@ export function getCommentAfter(node, sourceCode) {
158
161
  });
159
162
  }
160
163
 
164
+ /**
165
+ * Matches names against exact strings, `*` wildcards (`use*Store`) and regexes
166
+ * written as "/pattern/" strings.
167
+ */
168
+ export function createNameMatcher(patterns) {
169
+ const matchers = patterns.map((entry) => {
170
+ if (entry.length > 2 && entry.startsWith("/") && entry.endsWith("/")) {
171
+ try {
172
+ return new RegExp(entry.slice(1, -1));
173
+ } catch {
174
+ return entry;
175
+ }
176
+ }
177
+ if (entry.includes("*")) {
178
+ const escaped = entry
179
+ .split("*")
180
+ .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
181
+ .join("[a-zA-Z0-9_$]*");
182
+ return new RegExp(`^${escaped}$`);
183
+ }
184
+ return entry;
185
+ });
186
+ return (name) =>
187
+ matchers.some((matcher) =>
188
+ typeof matcher === "string" ? matcher === name : matcher.test(name)
189
+ );
190
+ }
191
+
161
192
  // Solid 2.0 split the runtime across packages: `solid-js` keeps the core and
162
193
  // the store APIs, while `@solidjs/web`, `@solidjs/h`, `@solidjs/html` and
163
194
  // `@solidjs/universal` own the renderers. Match both layouts.