Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle patterns in destructured literals #8871

Merged
merged 11 commits into from Jul 11, 2023
5 changes: 5 additions & 0 deletions .changeset/lucky-knives-crash.md
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: handle destructured primitive literals
6 changes: 6 additions & 0 deletions packages/playground/compile.js
@@ -0,0 +1,6 @@
import { readFileSync } from 'fs';
benmccann marked this conversation as resolved.
Show resolved Hide resolved
gtm-nayan marked this conversation as resolved.
Show resolved Hide resolved
import { compile } from '../svelte/src/compiler/index.js';

const code = readFileSync('src/App.svelte', 'utf8');

console.log(compile(code));
31 changes: 14 additions & 17 deletions packages/svelte/src/compiler/compile/Component.js
Expand Up @@ -1339,37 +1339,34 @@ 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
benmccann marked this conversation as resolved.
Show resolved Hide resolved
)
) {
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);
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/test/js/samples/hoisted-const/expected.js
Expand Up @@ -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);
Expand All @@ -32,6 +32,7 @@ function create_fragment(ctx) {
}

const ANSWER = 42;
const { length } = 'abc';

function get_answer() {
return ANSWER;
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/test/js/samples/hoisted-const/input.svelte
@@ -1,6 +1,7 @@
<script>
const ANSWER = 42;
const { length } = 'abc';
benmccann marked this conversation as resolved.
Show resolved Hide resolved
function get_answer() { return ANSWER; }
</script>

<b>{get_answer()}</b>
<b>{get_answer()} {length}</b>
1 change: 1 addition & 0 deletions packages/svelte/vitest.config.js
Expand Up @@ -22,6 +22,7 @@ export default defineConfig({
test: {
dir: 'test',
reporters: ['dot'],
include: ['**/*.test.js'],
gtm-nayan marked this conversation as resolved.
Show resolved Hide resolved
exclude: [...configDefaults.exclude, '**/samples/**'],
globalSetup: './test/vitest-global-setup.js'
}
Expand Down