Skip to content

Commit f7a3556

Browse files
committed
feat: add Svelte 5 support to no-not-function-handler
1 parent a003664 commit f7a3556

17 files changed

+102
-4
lines changed

.changeset/brown-dots-change.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': minor
3+
---
4+
5+
feat: add Svelte 5 support to `no-not-function-handler`

docs/rules/no-not-function-handler.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ If you use a non-function value for the event handler, it event handler will not
2929
</script>
3030
3131
<!-- ✓ GOOD -->
32-
<button on:click={foo} />
32+
<button onclick={foo} />
3333
<button
34-
on:click={() => {
34+
onclick={() => {
3535
/* */
3636
}}
3737
/>
3838
3939
<!-- ✗ BAD -->
40-
<button on:click={{ foo }} />
41-
<button on:click={bar} />
40+
<button onclick={{ foo }} />
41+
<button onclick={bar} />
4242
```
4343

4444
## :wrench: Options

packages/eslint-plugin-svelte/src/rules/no-not-function-handler.ts

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ export default createRule('no-not-function-handler', {
9595
return;
9696
}
9797
verify(node.expression);
98+
},
99+
SvelteAttribute(node) {
100+
if (node.key.type === 'SvelteName' && node.key.name.startsWith('on')) {
101+
const { value } = node;
102+
if (Array.isArray(value)) {
103+
for (const v of value) {
104+
if (v.type === 'SvelteMustacheTag') {
105+
verify(v.expression);
106+
}
107+
}
108+
}
109+
}
98110
}
99111
};
100112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unexpected array in event handler.
2+
line: 5
3+
column: 18
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
const a = 'hello!';
3+
</script>
4+
5+
<button onclick={[a]} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unexpected class in event handler.
2+
line: 4
3+
column: 18
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
</script>
3+
4+
<button onclick={class B {}} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unexpected object in event handler.
2+
line: 5
3+
column: 18
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
function fn() {}
3+
</script>
4+
5+
<button onclick={{ fn }} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": "^5.0.0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- message: Unexpected string value in event handler.
2+
line: 6
3+
column: 18
4+
suggestions: null
5+
- message: Unexpected string value in event handler.
6+
line: 7
7+
column: 18
8+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
const a = 'hello!';
3+
const b = `${a} world`;
4+
</script>
5+
6+
<button onclick={a} />
7+
<button onclick={b} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- message: Unexpected number value in event handler.
2+
line: 7
3+
column: 18
4+
suggestions: null
5+
- message: Unexpected bigint value in event handler.
6+
line: 8
7+
column: 18
8+
suggestions: null
9+
- message: Unexpected regex value in event handler.
10+
line: 9
11+
column: 18
12+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
const a = 42;
3+
const b = 42n;
4+
const c = /reg/;
5+
</script>
6+
7+
<button onclick={a} />
8+
<button onclick={b} />
9+
<button onclick={c} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let a = 'hello!';
3+
function fn() {}
4+
</script>
5+
6+
<button onclick={() => a} />
7+
<button onclick={fn} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
let a = null;
3+
</script>
4+
5+
<button onclick={null} />
6+
<button onclick={a} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": "^5.0.0"
3+
}

0 commit comments

Comments
 (0)