From 003193723469efbae6465ac4e1e15d9032fa6c6e Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Wed, 28 Jun 2023 19:17:33 +0545 Subject: [PATCH 1/9] fix: handle patterns in destructured literals --- packages/playground/compile.js | 6 ++++ .../svelte/src/compiler/compile/Component.js | 31 +++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 packages/playground/compile.js diff --git a/packages/playground/compile.js b/packages/playground/compile.js new file mode 100644 index 000000000000..f0e6b4876359 --- /dev/null +++ b/packages/playground/compile.js @@ -0,0 +1,6 @@ +import { readFileSync } from 'fs'; +import { compile } from '../svelte/src/compiler/index.js'; + +const code = readFileSync('src/App.svelte', 'utf8'); + +console.log(compile(code)); diff --git a/packages/svelte/src/compiler/compile/Component.js b/packages/svelte/src/compiler/compile/Component.js index 03d2c2fad323..1140792a1cb6 100644 --- a/packages/svelte/src/compiler/compile/Component.js +++ b/packages/svelte/src/compiler/compile/Component.js @@ -1339,18 +1339,17 @@ export default class Component { for (let i = 0; i < body.length; i += 1) { const node = body[i]; if (node.type === 'VariableDeclaration') { - const all_hoistable = node.declarations.every( - /** @param {any} d */ (d) => { - if (!d.init) return false; - if (d.init.type !== 'Literal') return false; - // everything except const values can be changed by e.g. svelte devtools - // which means we can't hoist it - if (node.kind !== 'const' && this.compile_options.dev) return false; - const { name } = /** @type {import('estree').Identifier} */ (d.id); + const all_hoistable = node.declarations.every((d) => { + if (!d.init) return false; + if (d.init.type !== 'Literal') return false; + // everything except const values can be changed by e.g. svelte devtools + // which means we can't hoist it + if (node.kind !== 'const' && this.compile_options.dev) return false; + for (const name of extract_names(d.id)) { const v = this.var_lookup.get(name); if (v.reassigned) return false; if (v.export_name) return false; - if (this.var_lookup.get(name).reassigned) return false; + if ( this.vars.find( /** @param {any} variable */ (variable) => variable.name === name && variable.module @@ -1358,18 +1357,16 @@ export default class Component { ) { return false; } - return true; } - ); + return true; + }); if (all_hoistable) { - node.declarations.forEach( - /** @param {any} d */ (d) => { - const variable = this.var_lookup.get( - /** @type {import('estree').Identifier} */ (d.id).name - ); + node.declarations.forEach((d) => { + for (const name of extract_names(d.id)) { + const variable = this.var_lookup.get(name); variable.hoistable = true; } - ); + }); hoistable_nodes.add(node); body.splice(i--, 1); this.fully_hoisted.push(node); From 007edb74cfc61cc1727dc667d309e298d3d5a70f Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Thu, 29 Jun 2023 14:54:00 +0545 Subject: [PATCH 2/9] test --- packages/svelte/test/js/samples/hoisted-const/expected.js | 3 ++- packages/svelte/test/js/samples/hoisted-const/input.svelte | 3 ++- packages/svelte/vitest.config.js | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/svelte/test/js/samples/hoisted-const/expected.js b/packages/svelte/test/js/samples/hoisted-const/expected.js index 33005b121de5..48bdf1f28b20 100644 --- a/packages/svelte/test/js/samples/hoisted-const/expected.js +++ b/packages/svelte/test/js/samples/hoisted-const/expected.js @@ -15,7 +15,7 @@ function create_fragment(ctx) { return { c() { b = element("b"); - b.textContent = `${get_answer()}`; + b.textContent = `${get_answer()} ${length}`; }, m(target, anchor) { insert(target, b, anchor); @@ -32,6 +32,7 @@ function create_fragment(ctx) { } const ANSWER = 42; +const { length } = 'abc'; function get_answer() { return ANSWER; diff --git a/packages/svelte/test/js/samples/hoisted-const/input.svelte b/packages/svelte/test/js/samples/hoisted-const/input.svelte index d248a8315425..4553a1952e76 100644 --- a/packages/svelte/test/js/samples/hoisted-const/input.svelte +++ b/packages/svelte/test/js/samples/hoisted-const/input.svelte @@ -1,6 +1,7 @@ -{get_answer()} \ No newline at end of file +{get_answer()} {length} \ No newline at end of file diff --git a/packages/svelte/vitest.config.js b/packages/svelte/vitest.config.js index 4392caa0e1c3..75e6e57042f4 100644 --- a/packages/svelte/vitest.config.js +++ b/packages/svelte/vitest.config.js @@ -22,6 +22,7 @@ export default defineConfig({ test: { dir: 'test', reporters: ['dot'], + include: ['**/*.test.js'], exclude: [...configDefaults.exclude, '**/samples/**'], globalSetup: './test/vitest-global-setup.js' } From a86fa9bf8337b1d1c1cf375adebd764c27ad37fb Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Thu, 29 Jun 2023 14:55:05 +0545 Subject: [PATCH 3/9] changeset --- .changeset/lucky-knives-crash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lucky-knives-crash.md diff --git a/.changeset/lucky-knives-crash.md b/.changeset/lucky-knives-crash.md new file mode 100644 index 000000000000..b3506ecc4f71 --- /dev/null +++ b/.changeset/lucky-knives-crash.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: handle destructured primitive literals From e9f76e7b7fe9bb384ef49a9124d0958a0e620446 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 9 Jul 2023 07:19:38 +0545 Subject: [PATCH 4/9] remove include config --- packages/svelte/vitest.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/svelte/vitest.config.js b/packages/svelte/vitest.config.js index 75e6e57042f4..4392caa0e1c3 100644 --- a/packages/svelte/vitest.config.js +++ b/packages/svelte/vitest.config.js @@ -22,7 +22,6 @@ export default defineConfig({ test: { dir: 'test', reporters: ['dot'], - include: ['**/*.test.js'], exclude: [...configDefaults.exclude, '**/samples/**'], globalSetup: './test/vitest-global-setup.js' } From f8846d5265221e5592cfc185106c51bb89918900 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 9 Jul 2023 07:24:28 +0545 Subject: [PATCH 5/9] instructions for compile.js --- packages/playground/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/playground/README.md b/packages/playground/README.md index 6ac0393720cc..e86c6b2da034 100644 --- a/packages/playground/README.md +++ b/packages/playground/README.md @@ -4,4 +4,6 @@ To prevent any changes you make in this directory from being accidentally commit 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. -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. +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. + +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. From bd65818602daa5fc360a6a2e607f9d1715cde4a2 Mon Sep 17 00:00:00 2001 From: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> Date: Mon, 10 Jul 2023 08:46:16 +0545 Subject: [PATCH 6/9] Update packages/playground/compile.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/playground/compile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/compile.js b/packages/playground/compile.js index f0e6b4876359..8b6dac9f1a7f 100644 --- a/packages/playground/compile.js +++ b/packages/playground/compile.js @@ -1,4 +1,4 @@ -import { readFileSync } from 'fs'; +import { readFileSync } from 'node:fs'; import { compile } from '../svelte/src/compiler/index.js'; const code = readFileSync('src/App.svelte', 'utf8'); From a6b77a2bf7528b32fc13a014436296dc84505a0e Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 11 Jul 2023 08:26:10 -0700 Subject: [PATCH 7/9] Update packages/svelte/src/compiler/compile/Component.js --- packages/svelte/src/compiler/compile/Component.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/svelte/src/compiler/compile/Component.js b/packages/svelte/src/compiler/compile/Component.js index 2e35acbfa0a9..2ed0574620c9 100644 --- a/packages/svelte/src/compiler/compile/Component.js +++ b/packages/svelte/src/compiler/compile/Component.js @@ -1310,8 +1310,7 @@ export default class Component { if (all_hoistable) { node.declarations.forEach((d) => { for (const name of extract_names(d.id)) { - const variable = this.var_lookup.get(name); - variable.hoistable = true; + this.var_lookup.get(name).hoistable = true; } }); hoistable_nodes.add(node); From 1a5e65173bfb325df4046bf0f0d51ca710c693f4 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 11 Jul 2023 08:26:17 -0700 Subject: [PATCH 8/9] Update packages/svelte/src/compiler/compile/Component.js --- packages/svelte/src/compiler/compile/Component.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/compiler/compile/Component.js b/packages/svelte/src/compiler/compile/Component.js index 2ed0574620c9..df191b71f0ef 100644 --- a/packages/svelte/src/compiler/compile/Component.js +++ b/packages/svelte/src/compiler/compile/Component.js @@ -1299,7 +1299,7 @@ export default class Component { if ( this.vars.find( - /** @param {any} variable */ (variable) => variable.name === name && variable.module + (variable) => variable.name === name && variable.module ) ) { return false; From c4216266b84619c1c29e0ab52975878660c68eb1 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 11 Jul 2023 08:26:49 -0700 Subject: [PATCH 9/9] format --- packages/svelte/src/compiler/compile/Component.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/svelte/src/compiler/compile/Component.js b/packages/svelte/src/compiler/compile/Component.js index df191b71f0ef..06e849fe13b8 100644 --- a/packages/svelte/src/compiler/compile/Component.js +++ b/packages/svelte/src/compiler/compile/Component.js @@ -1297,11 +1297,7 @@ export default class Component { if (v.reassigned) return false; if (v.export_name) return false; - if ( - this.vars.find( - (variable) => variable.name === name && variable.module - ) - ) { + if (this.vars.find((variable) => variable.name === name && variable.module)) { return false; } }