mirror of https://github.com/sveltejs/svelte
fix: null and warnings for local handlers (#15460)
* fix null and warning for local handlers * test * changeset * treat `let handler = () => {...}` the same as `function handler() {...}` --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/15467/head
parent
2c4d85bcec
commit
c5912aad71
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: null and warnings for local handlers
|
@ -0,0 +1,48 @@
|
|||||||
|
import { assertType } from 'vitest';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
mode: ['client'],
|
||||||
|
|
||||||
|
compileOptions: {
|
||||||
|
dev: true
|
||||||
|
},
|
||||||
|
|
||||||
|
test({ assert, target, warnings, logs }) {
|
||||||
|
/** @type {any} */
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
const handler = (/** @type {any} */ e) => {
|
||||||
|
error = e.error;
|
||||||
|
e.stopImmediatePropagation();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('error', handler, true);
|
||||||
|
|
||||||
|
const [b1, b2, b3] = target.querySelectorAll('button');
|
||||||
|
|
||||||
|
b1.click();
|
||||||
|
assert.deepEqual(logs, []);
|
||||||
|
assert.equal(error, null);
|
||||||
|
|
||||||
|
error = null;
|
||||||
|
logs.length = 0;
|
||||||
|
|
||||||
|
b2.click();
|
||||||
|
assert.deepEqual(logs, ['clicked']);
|
||||||
|
assert.equal(error, null);
|
||||||
|
|
||||||
|
error = null;
|
||||||
|
logs.length = 0;
|
||||||
|
|
||||||
|
b3.click();
|
||||||
|
assert.deepEqual(logs, []);
|
||||||
|
assert.deepEqual(warnings, [
|
||||||
|
'`click` handler at main.svelte:10:17 should be a function. Did you mean to add a leading `() =>`?'
|
||||||
|
]);
|
||||||
|
assert.isNotNull(error);
|
||||||
|
assert.match(error.message, /is not a function/);
|
||||||
|
|
||||||
|
window.removeEventListener('error', handler, true);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,10 @@
|
|||||||
|
<script>
|
||||||
|
let ignore = null;
|
||||||
|
let handler = () => console.log("clicked");
|
||||||
|
let bad = "invalid";
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={ignore}>click</button>
|
||||||
|
<button onclick={handler}>click</button>
|
||||||
|
<button onclick={bad}>click</button>
|
Loading…
Reference in new issue