Skip to content

Commit 20dac2a

Browse files
gtm-nayanbenmccann
andauthored
fix: handle patterns in destructured literals (#8871)
fixes #8863 --------- Co-authored-by: Ben McCann <[email protected]>
1 parent 258ccf2 commit 20dac2a

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

.changeset/lucky-knives-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: handle destructured primitive literals

packages/playground/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ To prevent any changes you make in this directory from being accidentally commit
44

55
If you would actually like to make some changes to the files here for everyone then run `git update-index --no-skip-worktree ./**/*.*` before committing.
66

7-
If you're using VS Code, you can use the "Playground: Full" launch configuration to run the playground and attach the debugger to both the compiler and the browser.
7+
If you're using VS Code, you can use the "Playground: Full" launch configuration to run the playground and attach the debugger to both the compiler and the browser. This will SSR the component and then also hydrate it on the client side using rollup to bundle any other imports.
8+
9+
You can also just compile the `App.svelte` file by running `node compile.js` if you'd like to check some compiler behaviour in isolation.

packages/playground/compile.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { readFileSync } from 'node:fs';
2+
import { compile } from '../svelte/src/compiler/index.js';
3+
4+
const code = readFileSync('src/App.svelte', 'utf8');
5+
6+
console.log(compile(code));

packages/svelte/src/compiler/compile/Component.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,26 +1292,22 @@ export default class Component {
12921292
// everything except const values can be changed by e.g. svelte devtools
12931293
// which means we can't hoist it
12941294
if (node.kind !== 'const' && this.compile_options.dev) return false;
1295-
const { name } = /** @type {import('estree').Identifier} */ (d.id);
1296-
const v = this.var_lookup.get(name);
1297-
if (v.reassigned) return false;
1298-
if (v.export_name) return false;
1299-
if (this.var_lookup.get(name).reassigned) return false;
1300-
if (
1301-
this.vars.find(
1302-
/** @param {any} variable */ (variable) => variable.name === name && variable.module
1303-
)
1304-
) {
1305-
return false;
1295+
for (const name of extract_names(d.id)) {
1296+
const v = this.var_lookup.get(name);
1297+
if (v.reassigned) return false;
1298+
if (v.export_name) return false;
1299+
1300+
if (this.vars.find((variable) => variable.name === name && variable.module)) {
1301+
return false;
1302+
}
13061303
}
13071304
return true;
13081305
});
13091306
if (all_hoistable) {
13101307
node.declarations.forEach((d) => {
1311-
const variable = this.var_lookup.get(
1312-
/** @type {import('estree').Identifier} */ (d.id).name
1313-
);
1314-
variable.hoistable = true;
1308+
for (const name of extract_names(d.id)) {
1309+
this.var_lookup.get(name).hoistable = true;
1310+
}
13151311
});
13161312
hoistable_nodes.add(node);
13171313
body.splice(i--, 1);

packages/svelte/test/js/samples/hoisted-const/expected.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function create_fragment(ctx) {
1515
return {
1616
c() {
1717
b = element("b");
18-
b.textContent = `${get_answer()}`;
18+
b.textContent = `${get_answer()} ${length}`;
1919
},
2020
m(target, anchor) {
2121
insert(target, b, anchor);
@@ -32,6 +32,7 @@ function create_fragment(ctx) {
3232
}
3333

3434
const ANSWER = 42;
35+
const { length } = 'abc';
3536

3637
function get_answer() {
3738
return ANSWER;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
const ANSWER = 42;
3+
const { length } = 'abc';
34
function get_answer() { return ANSWER; }
45
</script>
56

6-
<b>{get_answer()}</b>
7+
<b>{get_answer()} {length}</b>

0 commit comments

Comments
 (0)