@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
@@ -1,7 +1,8 @@
1
1
  import { getStringIfConstant } from "@eslint-community/eslint-utils";
2
2
  import isHtml from "is-html";
3
3
 
4
- import { jsxPropName } from "../utils.mjs";
4
+ import { getSourceCode } from "../compat.mjs";
5
+ import { jsxPropName, messageCode } from "../utils.mjs";
5
6
  export default {
6
7
  meta: {
7
8
  type: "problem",
@@ -28,20 +29,42 @@ export default {
28
29
  ],
29
30
  messages: {
30
31
  dangerous:
31
- "The innerHTML attribute is dangerous; passing unsanitized input can lead to security vulnerabilities.",
32
+ "`innerHTML={{{value}}}` parses the value as HTML, so input that reaches it can inject markup and run script (XSS). Fix: for text, render it as a child, `<{{element}}>{{{value}}}</{{element}}>`; for HTML, sanitize it before it reaches `innerHTML`.",
32
33
  conflict:
33
- "The innerHTML attribute should not be used on an element with child elements; they will be overwritten.",
34
+ "`<{{element}}>` has both `innerHTML` and children, and one replaces the other, so part of this markup never renders. Fix: keep one: drop the children, or drop `innerHTML` and write its markup as JSX.",
34
35
  notHtml:
35
- "The string passed to innerHTML does not appear to be valid HTML.",
36
- useInnerText:
37
- "For text content, using innerText is clearer and safer.",
36
+ "`innerHTML` gets `{{value}}`, which holds no HTML, so it is parsed as HTML for nothing and a later `<` or `&` in it turns into markup. Fix: `{{fix}}`.",
37
+ useInnerText: "Replace `innerHTML` with `innerText`.",
38
38
  dangerouslySetInnerHTML:
39
- "The dangerouslySetInnerHTML prop is not supported; use innerHTML instead."
39
+ "`dangerouslySetInnerHTML` is React's prop; Solid sets it as an unknown attribute and the HTML never renders. Fix: `{{fix}}`."
40
40
  }
41
41
  },
42
42
  defaultOptions: [{ allowStatic: true }],
43
43
  create(context) {
44
44
  const allowStatic = Boolean(context.options[0]?.allowStatic ?? true);
45
+ const sourceCode = getSourceCode(context);
46
+ const elementName = (attribute) =>
47
+ sourceCode.getText(attribute.parent.name);
48
+ // `<div innerHTML={html} />`: the value as an expression.
49
+ const valueText = (attribute) =>
50
+ messageCode(
51
+ sourceCode.getText(
52
+ attribute.value?.type === "JSXExpressionContainer"
53
+ ? attribute.value.expression
54
+ : attribute.value
55
+ ),
56
+ "html"
57
+ );
58
+ const reportDangerous = (node) => {
59
+ context.report({
60
+ node,
61
+ messageId: "dangerous",
62
+ data: {
63
+ element: elementName(node),
64
+ value: node.value ? valueText(node) : "true"
65
+ }
66
+ });
67
+ };
45
68
  return {
46
69
  JSXAttribute(node) {
47
70
  if (jsxPropName(node) === "dangerouslySetInnerHTML") {
@@ -59,6 +82,9 @@ export default {
59
82
  context.report({
60
83
  node,
61
84
  messageId: "dangerouslySetInnerHTML",
85
+ data: {
86
+ fix: `innerHTML={${messageCode(sourceCode.getText(htmlProp.value), "html")}}`
87
+ },
62
88
  fix: (fixer) => {
63
89
  const propRange = node.range;
64
90
  const valueRange = htmlProp.value.range;
@@ -77,13 +103,15 @@ export default {
77
103
  } else {
78
104
  context.report({
79
105
  node,
80
- messageId: "dangerouslySetInnerHTML"
106
+ messageId: "dangerouslySetInnerHTML",
107
+ data: { fix: "innerHTML={html}" }
81
108
  });
82
109
  }
83
110
  } else {
84
111
  context.report({
85
112
  node,
86
- messageId: "dangerouslySetInnerHTML"
113
+ messageId: "dangerouslySetInnerHTML",
114
+ data: { fix: "innerHTML={html}" }
87
115
  });
88
116
  }
89
117
  return;
@@ -106,13 +134,24 @@ export default {
106
134
  ) {
107
135
  context.report({
108
136
  node: node.parent.parent, // report error on JSXElement instead of JSXAttribute
109
- messageId: "conflict"
137
+ messageId: "conflict",
138
+ data: { element: elementName(node) }
110
139
  });
111
140
  }
112
141
  } else {
113
142
  context.report({
114
143
  node,
115
144
  messageId: "notHtml",
145
+ data: {
146
+ value: messageCode(
147
+ sourceCode.getText(node.value),
148
+ '"…"'
149
+ ),
150
+ fix: messageCode(
151
+ `innerText=${sourceCode.getText(node.value)}`,
152
+ "innerText={text}"
153
+ )
154
+ },
116
155
  suggest: [
117
156
  {
118
157
  fix: (fixer) =>
@@ -126,16 +165,10 @@ export default {
126
165
  });
127
166
  }
128
167
  } else {
129
- context.report({
130
- node,
131
- messageId: "dangerous"
132
- });
168
+ reportDangerous(node);
133
169
  }
134
170
  } else {
135
- context.report({
136
- node,
137
- messageId: "dangerous"
138
- });
171
+ reportDangerous(node);
139
172
  }
140
173
  }
141
174
  };
@@ -0,0 +1,73 @@
1
+ // Ported from eslint-plugin-solid 0.20.1 (no-module-scope-reactive-primitive).
2
+ // Reactive state created at module scope is shared across every request when
3
+ // server rendering, so one user's state leaks into another's response. Any
4
+ // enclosing function is the escape hatch, `createRoot` included.
5
+ import { getSourceCode } from "../compat.mjs";
6
+ import { findParent, isFunctionNode, trackImports } from "../utils.mjs";
7
+
8
+ // Upstream's list plus createTrackedEffect and createReaction, which Solid
9
+ // 2.0 exports and which own state the same way.
10
+ const REACTIVE_PRIMITIVES = [
11
+ "createSignal",
12
+ "createMemo",
13
+ "createDeferred",
14
+ "createStore",
15
+ "createEffect",
16
+ "createRenderEffect",
17
+ "createTrackedEffect",
18
+ "createReaction",
19
+ "createOptimistic",
20
+ "createOptimisticStore",
21
+ "createProjection"
22
+ ];
23
+
24
+ export default {
25
+ meta: {
26
+ type: "problem",
27
+ docs: {
28
+ description:
29
+ "Disallow reactive primitives at module scope, where state is shared across SSR requests.",
30
+ url: "https://github.com/solidjs/eslint-plugin-solid/blob/main/packages/eslint-plugin-solid/docs/no-module-scope-reactive-primitive.md"
31
+ },
32
+ schema: [],
33
+ messages: {
34
+ moduleScopePrimitive:
35
+ "`{{statement}}` runs once per process at module scope, so with server rendering every request shares this state and one user's data reaches another's response. Fix: move `{{statement}}` into the component that uses it, or into a provider component that hands it down with `<Context value={…}>`. Only for client-only state that is meant to be global: wrap it in `createRoot(() => …)`."
36
+ }
37
+ },
38
+ defaultOptions: [],
39
+ create(context) {
40
+ const { matchImport, handleImportDeclaration } = trackImports();
41
+ return {
42
+ ImportDeclaration: handleImportDeclaration,
43
+ CallExpression(node) {
44
+ if (node.callee.type !== "Identifier") {
45
+ return;
46
+ }
47
+ if (!matchImport(REACTIVE_PRIMITIVES, node.callee.name)) {
48
+ return;
49
+ }
50
+ if (findParent(node, isFunctionNode) === null) {
51
+ // The whole declaration when there is one, so the fix can
52
+ // move it as written.
53
+ const declaration = findParent(
54
+ node,
55
+ (candidate) => candidate.type === "VariableDeclaration"
56
+ );
57
+ const text = getSourceCode(context).getText(
58
+ declaration ?? node
59
+ );
60
+ context.report({
61
+ data: {
62
+ statement: text.includes("\n")
63
+ ? `${node.callee.name}(…)`
64
+ : text
65
+ },
66
+ messageId: "moduleScopePrimitive",
67
+ node
68
+ });
69
+ }
70
+ }
71
+ };
72
+ }
73
+ };
@@ -16,6 +16,20 @@ const OWNED_PRIMITIVES = [
16
16
  "onSettled"
17
17
  ];
18
18
 
19
+ /**
20
+ * What Solid 2.0 does in dev when a ref callback creates the primitive. A ref
21
+ * callback owns nothing, like an effect callback.
22
+ */
23
+ function diagnosticFor(primitive) {
24
+ if (primitive === "onCleanup") {
25
+ return " and Solid throws CLEANUP_IN_FORBIDDEN_SCOPE in dev";
26
+ }
27
+
28
+ return ["onSettled", "createReaction"].includes(primitive)
29
+ ? ""
30
+ : " and Solid throws PRIMITIVE_IN_EFFECT_CALLBACK in dev";
31
+ }
32
+
19
33
  /** The callbacks a `ref` prop applies, including the ones in a ref array. */
20
34
  function getRefCallbacks(expression) {
21
35
  if (isFunctionNode(expression)) {
@@ -78,7 +92,7 @@ export default {
78
92
  schema: [],
79
93
  messages: {
80
94
  ownedPrimitiveInRef:
81
- "`{{name}}` is called inside a ref callback, which runs without an owner — nothing will ever dispose it. Create it in the directive factory instead, and let the returned callback only store the element."
95
+ "`{{name}}` is called inside a ref callback, which runs without an owner, so nothing disposes it{{diagnostic}}. Fix: call `{{name}}` in the component body, or in a directive factory that returns the ref callback, `const directive = () => { {{name}}(…); return (el) => { /* store or update el */ }; }` used as `ref={directive()}`; the ref callback itself only touches the element."
82
96
  }
83
97
  },
84
98
  defaultOptions: [],
@@ -109,10 +123,17 @@ export default {
109
123
  return;
110
124
  }
111
125
 
126
+ const primitive = matchImport(
127
+ OWNED_PRIMITIVES,
128
+ candidate.callee.name
129
+ );
112
130
  context.report({
113
131
  node: candidate,
114
132
  messageId: "ownedPrimitiveInRef",
115
- data: { name: candidate.callee.name }
133
+ data: {
134
+ diagnostic: diagnosticFor(primitive),
135
+ name: candidate.callee.name
136
+ }
116
137
  });
117
138
  });
118
139
  }
@@ -1,7 +1,9 @@
1
+ import { getSourceCode } from "../compat.mjs";
1
2
  import {
2
3
  isFunctionNode,
3
4
  trackImports,
4
5
  isPropsByName,
6
+ messageCode,
5
7
  trace
6
8
  } from "../utils.mjs";
7
9
  // Store APIs that hand back a Proxy. Solid 2.0 exports these from `solid-js`.
@@ -21,20 +23,23 @@ export default {
21
23
  schema: [],
22
24
  messages: {
23
25
  noStore:
24
- "Solid Store APIs use Proxies, which are incompatible with your target environment.",
26
+ "`{{name}}` returns a store, which is a Proxy, and this rule is on because the target environment has no Proxy. Fix: keep the state in signals, `createSignal(initial)`, and replace the value on update; if every target has Proxy, turn `solid/no-proxy-apis` off in the lint config.",
25
27
  spreadCall:
26
- "Using a function call in JSX spread makes Solid use Proxies, which are incompatible with your target environment.",
28
+ "`{...{{expression}}}` spreads a call, so Solid wraps it in a Proxy to keep it reactive, and the target environment has no Proxy. Fix: pass the props one by one, `name={{{expression}}.name}`, or spread a plain object.",
27
29
  spreadMember:
28
- "Using a property access in JSX spread makes Solid use Proxies, which are incompatible with your target environment.",
30
+ "`{...{{expression}}}` spreads a property access, so Solid wraps it in a Proxy to keep it reactive, and the target environment has no Proxy. Fix: pass the props one by one, `name={{{expression}}.name}`, or spread a plain object.",
29
31
  proxyLiteral:
30
- "Proxies are incompatible with your target environment.",
32
+ "`{{expression}}` creates a Proxy, and the target environment has no Proxy, which cannot be polyfilled. Fix: use a plain object, with getters (`get value() { … }`) where a read must compute.",
31
33
  mergeProps:
32
- "If you pass a function to `mergeProps`, it will create a Proxy, which are incompatible with your target environment."
34
+ "`{{name}}` gets `{{argument}}`, which may be a function or a store, and reading such a source live needs a Proxy, which the target environment does not have. Fix: pass plain objects, with a getter for a value that must stay live, `{{name}}({ get size() { return size(); } }, props)`."
33
35
  }
34
36
  },
35
37
  defaultOptions: [],
36
38
  create(context) {
37
39
  const { matchImport, handleImportDeclaration } = trackImports();
40
+ const sourceCode = getSourceCode(context);
41
+ const code = (node, fallback) =>
42
+ messageCode(sourceCode.getText(node), fallback);
38
43
  return {
39
44
  ImportDeclaration(node) {
40
45
  handleImportDeclaration(node); // track import aliases
@@ -44,7 +49,11 @@ export default {
44
49
  // Solid 2.0 moved them into `solid-js`, which every file
45
50
  // imports, so match the specifier names instead.
46
51
  if (source === "solid-js/store") {
47
- context.report({ node, messageId: "noStore" });
52
+ context.report({
53
+ node,
54
+ messageId: "noStore",
55
+ data: { name: "solid-js/store" }
56
+ });
48
57
  return;
49
58
  }
50
59
  if (source !== "solid-js") {
@@ -57,16 +66,25 @@ export default {
57
66
  ) {
58
67
  context.report({
59
68
  node: specifier,
60
- messageId: "noStore"
69
+ messageId: "noStore",
70
+ data: { name: specifier.imported.name }
61
71
  });
62
72
  }
63
73
  }
64
74
  },
65
75
  "JSXSpreadAttribute MemberExpression"(node) {
66
- context.report({ node, messageId: "spreadMember" });
76
+ context.report({
77
+ node,
78
+ messageId: "spreadMember",
79
+ data: { expression: code(node, "props.rest") }
80
+ });
67
81
  },
68
82
  "JSXSpreadAttribute CallExpression"(node) {
69
- context.report({ node, messageId: "spreadCall" });
83
+ context.report({
84
+ node,
85
+ messageId: "spreadCall",
86
+ data: { expression: code(node, "getProps()") }
87
+ });
70
88
  },
71
89
  CallExpression(node) {
72
90
  if (node.callee.type === "Identifier") {
@@ -86,7 +104,11 @@ export default {
86
104
  .forEach((badArg) => {
87
105
  context.report({
88
106
  node: badArg,
89
- messageId: "mergeProps"
107
+ messageId: "mergeProps",
108
+ data: {
109
+ argument: code(badArg, "…"),
110
+ name: node.callee.name
111
+ }
90
112
  });
91
113
  });
92
114
  }
@@ -99,7 +121,10 @@ export default {
99
121
  ) {
100
122
  context.report({
101
123
  node,
102
- messageId: "proxyLiteral"
124
+ messageId: "proxyLiteral",
125
+ data: {
126
+ expression: code(node, "Proxy.revocable(…)")
127
+ }
103
128
  });
104
129
  }
105
130
  }
@@ -109,7 +134,11 @@ export default {
109
134
  node.callee.type === "Identifier" &&
110
135
  node.callee.name === "Proxy"
111
136
  ) {
112
- context.report({ node, messageId: "proxyLiteral" });
137
+ context.report({
138
+ node,
139
+ messageId: "proxyLiteral",
140
+ data: { expression: code(node, "new Proxy(…)") }
141
+ });
113
142
  }
114
143
  }
115
144
  };