From 9d3562b4b153a8519b74b644dba383a2258953c6 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 10 Jul 2023 10:50:17 -0700 Subject: [PATCH] Revert "look at whether the variable was mutated or reassigned instead" This reverts commit 73586aa9ad1ec1b866716569f592a51952a686d3. --- .../svelte/src/compiler/compile/Component.js | 19 ++++++++++++++++--- .../compile/nodes/shared/Expression.js | 7 ++----- packages/svelte/src/compiler/interfaces.d.ts | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/svelte/src/compiler/compile/Component.js b/packages/svelte/src/compiler/compile/Component.js index 17a1adc2cd28..37a03c78d2a0 100644 --- a/packages/svelte/src/compiler/compile/Component.js +++ b/packages/svelte/src/compiler/compile/Component.js @@ -770,12 +770,25 @@ export default class Component { if (name[0] === '$') { return this.error(/** @type {any} */ (node), compiler_errors.illegal_declaration); } - const { type } = node; + const writable = + node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let'); + + let immutable = false; + if (node.type === 'VariableDeclaration' && node.kind === 'const') { + immutable = true; + for (const declaration of node.declarations) { + if (declaration.init.type !== 'Literal') { + immutable = false; + } + } + } + this.add_var(node, { name, initialised: instance_scope.initialised_declarations.has(name), - imported: type.startsWith('Import'), - writable: type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let') + imported: node.type.startsWith('Import'), + writable, + immutable }); this.node_for_declaration.set(name, node); }); diff --git a/packages/svelte/src/compiler/compile/nodes/shared/Expression.js b/packages/svelte/src/compiler/compile/nodes/shared/Expression.js index 69310f77b418..7836809b25f9 100644 --- a/packages/svelte/src/compiler/compile/nodes/shared/Expression.js +++ b/packages/svelte/src/compiler/compile/nodes/shared/Expression.js @@ -129,11 +129,8 @@ export default class Expression { .forEach((name) => dependencies.add(name)); } } else { - if (!lazy) { - const variable = component.var_lookup.get(name); - if (!variable || !variable.imported || variable.mutated || variable.reassigned) { - dependencies.add(name); - } + if (!lazy && !component.var_lookup.get(name)?.immutable) { + dependencies.add(name); } component.add_reference(node, name); component.warn_if_undefined(name, nodes[0], template_scope, owner); diff --git a/packages/svelte/src/compiler/interfaces.d.ts b/packages/svelte/src/compiler/interfaces.d.ts index 64be9698f5b3..b38f2a597af0 100644 --- a/packages/svelte/src/compiler/interfaces.d.ts +++ b/packages/svelte/src/compiler/interfaces.d.ts @@ -383,6 +383,7 @@ export interface Var { writable?: boolean; // used internally, but not exposed + immutable?: boolean; global?: boolean; internal?: boolean; // event handlers, bindings initialised?: boolean;