Skip to content

Commit

Permalink
look at whether the variable was mutated or reassigned instead
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 10, 2023
1 parent a0393b2 commit 73586aa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
19 changes: 3 additions & 16 deletions packages/svelte/src/compiler/compile/Component.js
Expand Up @@ -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);
});
Expand Down
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/src/compiler/interfaces.d.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 73586aa

Please sign in to comment.