Skip to content

Commit 942e825

Browse files
committed
Minor cleanup, text changes to #75.
1 parent c9c6462 commit 942e825

File tree

4 files changed

+21
-42
lines changed

4 files changed

+21
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ const [editedValue, setEditedValue] = createSignal(props.value);
128128
|| | [solid/jsx-no-script-url](docs/jsx-no-script-url.md) | Disallow javascript: URLs. |
129129
|| 🔧 | [solid/jsx-no-undef](docs/jsx-no-undef.md) | Disallow references to undefined variables in JSX. Handles custom directives. |
130130
|| | [solid/jsx-uses-vars](docs/jsx-uses-vars.md) | Prevent variables used in JSX from being marked as unused. |
131-
| | | [solid/no-array-handlers](docs/no-array-handlers.md) | Disallow usage of unsafe event handlers. |
131+
| | | [solid/no-array-handlers](docs/no-array-handlers.md) | Disallow usage of type-unsafe event handlers. |
132132
|| 🔧 | [solid/no-destructure](docs/no-destructure.md) | Disallow destructuring props. In Solid, props must be used with property accesses (`props.foo`) to preserve reactivity. This rule only tracks destructuring in the parameter list. |
133133
|| 🔧 | [solid/no-innerhtml](docs/no-innerhtml.md) | Disallow usage of the innerHTML attribute, which can often lead to security vulnerabilities. |
134134
| | | [solid/no-proxy-apis](docs/no-proxy-apis.md) | Disallow usage of APIs that use ES6 Proxies, only to target environments that don't support them. |

docs/no-array-handlers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- AUTO-GENERATED-CONTENT:START (HEADER) -->
22
# solid/no-array-handlers
3-
Disallow usage of unsafe event handlers.
3+
Disallow usage of type-unsafe event handlers.
44
This rule is **off** by default.
55

66
[View source](../src/rules/no-array-handlers.ts) · [View tests](../test/rules/no-array-handlers.test.ts)

src/rules/event-handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const rule: TSESLint.RuleModule<
164164
// string name of the name node
165165
const { name } = node.name;
166166

167-
if (!/^on[a-zA-Z].*$/.test(name)) {
167+
if (!/^on[a-zA-Z]/.test(name)) {
168168
return; // bail if Solid doesn't consider the prop name an event handler
169169
}
170170

src/rules/no-array-handlers.ts

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const rule: TSESLint.RuleModule<"noArrayHandlers", []> = {
66
type: "problem",
77
docs: {
88
recommended: false,
9-
description: "Disallow usage of unsafe event handlers.",
9+
description: "Disallow usage of type-unsafe event handlers.",
1010
url: "https://github.com/solidjs-community/eslint-plugin-solid/blob/main/docs/no-array-handlers.md",
1111
},
1212
schema: [],
13-
messages: { noArrayHandlers: "Passing an array as an event handler is potentially unsafe." },
13+
messages: {
14+
noArrayHandlers: "Passing an array as an event handler is potentially type-unsafe.",
15+
},
1416
},
1517
create(context) {
1618
return {
@@ -23,44 +25,21 @@ const rule: TSESLint.RuleModule<"noArrayHandlers", []> = {
2325
return; // bail if this is not a DOM/SVG element or web component
2426
}
2527

26-
if (node.name.type === "JSXNamespacedName") {
27-
if (
28-
node.name.namespace.name === "on" &&
29-
node.value?.type === "JSXExpressionContainer" &&
30-
node.value.expression.type === "ArrayExpression"
31-
) {
32-
// Handling events that start with "on:"
33-
context.report({
34-
node,
35-
messageId: "noArrayHandlers",
36-
});
37-
}
38-
return; // bali if it's not an event namespace
39-
}
40-
41-
// string name of the name node
42-
const { name } = node.name;
28+
const isNamespacedHandler =
29+
node.name.type === "JSXNamespacedName" && node.name.namespace.name === "on";
30+
const isNormalEventHandler =
31+
node.name.type === "JSXIdentifier" && /^on[a-zA-Z]/.test(node.name.name);
4332

44-
if (!/^on[a-zA-Z].*$/.test(name)) {
45-
return; // bail if Solid doesn't consider the prop name an event handler
46-
}
47-
48-
if (node.value?.type === "JSXExpressionContainer") {
49-
if (node.value.expression.type === "ArrayExpression") {
50-
// If passed an array
51-
context.report({
52-
node,
53-
messageId: "noArrayHandlers",
54-
});
55-
} else if (node.value.expression.type === "Identifier") {
56-
const traced = trace(node.value.expression, context.getScope());
57-
if (traced.type === "ArrayExpression") {
58-
context.report({
59-
node,
60-
messageId: "noArrayHandlers",
61-
});
62-
}
63-
}
33+
if (
34+
(isNamespacedHandler || isNormalEventHandler) &&
35+
node.value?.type === "JSXExpressionContainer" &&
36+
trace(node.value.expression, context.getScope()).type === "ArrayExpression"
37+
) {
38+
// Warn if passed an array
39+
context.report({
40+
node,
41+
messageId: "noArrayHandlers",
42+
});
6443
}
6544
},
6645
};

0 commit comments

Comments
 (0)