From ffc476957129ec77ae9a32ac6972d131915c261a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 29 May 2025 09:22:15 -0400 Subject: [PATCH] handle cycles --- packages/svelte/src/compiler/phases/scope.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index b4cdb6b446..8fc4da8154 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -221,6 +221,8 @@ class Evaluation { * @param {Set} values */ constructor(scope, expression, values) { + current_evaluations.set(expression, this); + this.values = values; switch (expression.type) { @@ -543,6 +545,8 @@ class Evaluation { if (this.values.size > 1 || typeof this.value === 'symbol') { this.is_known = false; } + + current_evaluations.delete(expression); } } @@ -734,10 +738,20 @@ export class Scope { * @param {Set} [values] */ evaluate(expression, values = new Set()) { + const current = current_evaluations.get(expression); + if (current) return current; + return new Evaluation(this, expression, values); } } +/** + * Track which expressions are currently being evaluated — this allows + * us to prevent cyclical evaluations without passing the map around + * @type {Map} + */ +const current_evaluations = new Map(); + /** @type {Record any>} */ const binary = { '!=': (left, right) => left != right,