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;