diff --git a/packages/svelte/src/compiler/compile/Component.js b/packages/svelte/src/compiler/compile/Component.js index 37a03c78d2a0..17a1adc2cd28 100644 --- a/packages/svelte/src/compiler/compile/Component.js +++ b/packages/svelte/src/compiler/compile/Component.js @@ -770,25 +770,12 @@ export default class Component { if (name[0] === '$') { return this.error(/** @type {any} */ (node), compiler_errors.illegal_declaration); } - 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; - } - } - } - + const { type } = node; this.add_var(node, { name, initialised: instance_scope.initialised_declarations.has(name), - imported: node.type.startsWith('Import'), - writable, - immutable + imported: type.startsWith('Import'), + writable: type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let') }); 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 7836809b25f9..69310f77b418 100644 --- a/packages/svelte/src/compiler/compile/nodes/shared/Expression.js +++ b/packages/svelte/src/compiler/compile/nodes/shared/Expression.js @@ -129,8 +129,11 @@ export default class Expression { .forEach((name) => dependencies.add(name)); } } else { - if (!lazy && !component.var_lookup.get(name)?.immutable) { - dependencies.add(name); + if (!lazy) { + const variable = component.var_lookup.get(name); + if (!variable || !variable.imported || variable.mutated || variable.reassigned) { + 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 b38f2a597af0..64be9698f5b3 100644 --- a/packages/svelte/src/compiler/interfaces.d.ts +++ b/packages/svelte/src/compiler/interfaces.d.ts @@ -383,7 +383,6 @@ export interface Var { writable?: boolean; // used internally, but not exposed - immutable?: boolean; global?: boolean; internal?: boolean; // event handlers, bindings initialised?: boolean;