-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't hoist function when already referenced in module scope
fixes #10279
- Loading branch information
1 parent
14d7b26
commit 1538264
Showing
4 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"svelte": patch | ||
--- | ||
|
||
fix: don't hoist function when already referenced in module scope |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 14 additions & 7 deletions
21
packages/svelte/tests/runtime-runes/samples/event-attribute-not-hoistable/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import { test } from '../../test'; | ||
|
||
// Checks that event handlers are not hoisted when one of them is not delegateable | ||
export default test({ | ||
html: `<button>0</button>`, | ||
html: `<button>0</button><button>x0</button><button>y0</button>`, | ||
|
||
async test({ assert, target }) { | ||
const [button] = target.querySelectorAll('button'); | ||
const [btn1, btn2, btn3] = target.querySelectorAll('button'); | ||
|
||
button.click(); | ||
btn1.click(); | ||
await Promise.resolve(); | ||
assert.htmlEqual(target.innerHTML, '<button>1</button>'); | ||
assert.htmlEqual(target.innerHTML, '<button>1</button><button>x0</button><button>y0</button>'); | ||
|
||
button.dispatchEvent(new MouseEvent('mouseenter')); | ||
btn1.dispatchEvent(new MouseEvent('mouseenter')); | ||
await Promise.resolve(); | ||
assert.htmlEqual(target.innerHTML, '<button>2</button>'); | ||
assert.htmlEqual(target.innerHTML, '<button>2</button><button>x0</button><button>y0</button>'); | ||
|
||
btn2.click(); | ||
await Promise.resolve(); | ||
assert.htmlEqual(target.innerHTML, '<button>2</button><button>x1</button><button>y0</button>'); | ||
|
||
btn3.click(); | ||
await Promise.resolve(); | ||
assert.htmlEqual(target.innerHTML, '<button>2</button><button>x1</button><button>y1</button>'); | ||
} | ||
}); |
35 changes: 30 additions & 5 deletions
35
packages/svelte/tests/runtime-runes/samples/event-attribute-not-hoistable/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,36 @@ | ||
<script context="module"> | ||
function declared_in_module_scope() { | ||
return 'x'; | ||
} | ||
let a = declared_in_module_scope(); | ||
let b = 'x'; | ||
try { | ||
b = doesnt_exist(); | ||
} catch (e) { | ||
b = 'y'; | ||
} | ||
</script> | ||
|
||
<script> | ||
let count = $state(0) | ||
let count1 = $state(0); | ||
let count2 = $state(0); | ||
let count3 = $state(0); | ||
function increment() { | ||
count += 1 | ||
count1 += 1; | ||
} | ||
function declared_in_module_scope() { | ||
count2 += 1; | ||
} | ||
function doesnt_exist() { | ||
count3 += 1; | ||
} | ||
</script> | ||
|
||
<button onclick={increment} onmouseenter={increment}> | ||
{count} | ||
</button> | ||
<!-- Checks that event handlers are not hoisted when one of them is not delegateable --> | ||
<button onclick={increment} onmouseenter={increment}>{count1}</button> | ||
|
||
<!-- Checks that event handler is not hoisted if the same name is used in the module context --> | ||
<button onclick={declared_in_module_scope}>{a}{count2}</button> | ||
<button onclick={doesnt_exist}>{b}{count3}</button> |