From 3576c7443e71bbec2a559e588d9125bfb2980c95 Mon Sep 17 00:00:00 2001 From: Nguyen Tran <88808276+ngtr6788@users.noreply.github.com> Date: Tue, 27 Jun 2023 02:22:08 -0400 Subject: [PATCH] fix: Make sure 'func' doesn't clash with destructured contexts (#8840) Fixes: #8753 --- .changeset/eight-emus-allow.md | 5 ++++ .../compiler/compile/nodes/shared/Context.js | 1 + .../await-block-func-function/_config.js | 25 +++++++++++++++++++ .../await-block-func-function/main.svelte | 11 ++++++++ .../const-tag-func-function/_config.js | 5 ++++ .../const-tag-func-function/main.svelte | 8 ++++++ .../each-block-func-function/_config.js | 9 +++++++ .../each-block-func-function/main.svelte | 3 +++ 8 files changed, 67 insertions(+) create mode 100644 .changeset/eight-emus-allow.md create mode 100644 packages/svelte/test/runtime/samples/await-block-func-function/_config.js create mode 100644 packages/svelte/test/runtime/samples/await-block-func-function/main.svelte create mode 100644 packages/svelte/test/runtime/samples/const-tag-func-function/_config.js create mode 100644 packages/svelte/test/runtime/samples/const-tag-func-function/main.svelte create mode 100644 packages/svelte/test/runtime/samples/each-block-func-function/_config.js create mode 100644 packages/svelte/test/runtime/samples/each-block-func-function/main.svelte diff --git a/.changeset/eight-emus-allow.md b/.changeset/eight-emus-allow.md new file mode 100644 index 0000000000..4c6e084391 --- /dev/null +++ b/.changeset/eight-emus-allow.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure identifiers in destructuring contexts don't clash with existing ones diff --git a/packages/svelte/src/compiler/compile/nodes/shared/Context.js b/packages/svelte/src/compiler/compile/nodes/shared/Context.js index ee7a75eef4..053422d211 100644 --- a/packages/svelte/src/compiler/compile/nodes/shared/Context.js +++ b/packages/svelte/src/compiler/compile/nodes/shared/Context.js @@ -36,6 +36,7 @@ export function unpack_destructuring({ if (in_rest_element) { context_rest_properties.set(node.name, node); } + component.used_names.add(node.name); } else if (node.type === 'ArrayPattern') { node.elements.forEach((element, i) => { if (!element) { diff --git a/packages/svelte/test/runtime/samples/await-block-func-function/_config.js b/packages/svelte/test/runtime/samples/await-block-func-function/_config.js new file mode 100644 index 0000000000..e62b15fa9b --- /dev/null +++ b/packages/svelte/test/runtime/samples/await-block-func-function/_config.js @@ -0,0 +1,25 @@ +export default { + get props() { + return { + thePromise: new Promise((_) => {}) + }; + }, + + html: ` + Waiting... + `, + + async test({ assert, component, target }) { + await (component.thePromise = Promise.resolve({ func: 12345 })); + + assert.htmlEqual(target.innerHTML, '12345'); + + try { + await (component.thePromise = Promise.reject({ func: 67890 })); + } catch (e) { + // do nothing + } + + assert.htmlEqual(target.innerHTML, '67890'); + } +}; diff --git a/packages/svelte/test/runtime/samples/await-block-func-function/main.svelte b/packages/svelte/test/runtime/samples/await-block-func-function/main.svelte new file mode 100644 index 0000000000..6d36038d6f --- /dev/null +++ b/packages/svelte/test/runtime/samples/await-block-func-function/main.svelte @@ -0,0 +1,11 @@ + + +{#await thePromise} + Waiting... +{:then { func }} + {(() => func)()} +{:catch { func: func_1 }} + {(() => func_1)()} +{/await} diff --git a/packages/svelte/test/runtime/samples/const-tag-func-function/_config.js b/packages/svelte/test/runtime/samples/const-tag-func-function/_config.js new file mode 100644 index 0000000000..e1a6191fcb --- /dev/null +++ b/packages/svelte/test/runtime/samples/const-tag-func-function/_config.js @@ -0,0 +1,5 @@ +export default { + html: ` + [12,13,14] + ` +}; diff --git a/packages/svelte/test/runtime/samples/const-tag-func-function/main.svelte b/packages/svelte/test/runtime/samples/const-tag-func-function/main.svelte new file mode 100644 index 0000000000..01dce82004 --- /dev/null +++ b/packages/svelte/test/runtime/samples/const-tag-func-function/main.svelte @@ -0,0 +1,8 @@ + + +{#if true} + {@const [func_1] = [[12, 13, 14]]} + {(() => JSON.stringify(func_1))()} +{/if} diff --git a/packages/svelte/test/runtime/samples/each-block-func-function/_config.js b/packages/svelte/test/runtime/samples/each-block-func-function/_config.js new file mode 100644 index 0000000000..af96b8ce83 --- /dev/null +++ b/packages/svelte/test/runtime/samples/each-block-func-function/_config.js @@ -0,0 +1,9 @@ +export default { + html: ` +
1
+2
+3
+4
+5
+ ` +}; diff --git a/packages/svelte/test/runtime/samples/each-block-func-function/main.svelte b/packages/svelte/test/runtime/samples/each-block-func-function/main.svelte new file mode 100644 index 0000000000..812e7940d5 --- /dev/null +++ b/packages/svelte/test/runtime/samples/each-block-func-function/main.svelte @@ -0,0 +1,3 @@ +{#each [1, 2, 3, 4, 5] as func} +{(() => func)()}
+{/each}