diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 1cdd218ea8..bb13b586d7 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -14,7 +14,7 @@ import { } from '../../utils/ast.js'; import * as b from '#compiler/builders'; import { Scope, ScopeRoot, create_scopes, get_rune, set_scope } from '../scope.js'; -import check_graph_for_cycles from './utils/check_graph_for_cycles.js'; +import check_graph_for_cycles from '../../utils/check_graph_for_cycles.js'; import { create_attribute, is_custom_element_node } from '../nodes.js'; import { analyze_css } from './css/css-analyze.js'; import { prune } from './css/css-prune.js'; diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js index ebb2fc2b67..15cc7ed343 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js @@ -1,12 +1,11 @@ /** @import { Expression, Identifier } from 'estree' */ /** @import { Context } from '../types' */ import is_reference from 'is-reference'; -import { should_proxy } from '../../3-transform/client/utils.js'; +import { should_proxy, get_rune } from '../../scope.js'; import * as e from '../../../errors.js'; import * as w from '../../../warnings.js'; import { is_rune } from '../../../../utils.js'; import { mark_subtree_dynamic } from './shared/fragment.js'; -import { get_rune } from '../../scope.js'; import { is_component_node } from '../../nodes.js'; /** diff --git a/packages/svelte/src/compiler/phases/3-transform/client/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/utils.js index 9cdfa5cae1..8d1eae94d3 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -127,44 +127,6 @@ export function is_prop_source(binding, state) { ); } -/** - * @param {Expression} node - * @param {Scope | null} scope - */ -export function should_proxy(node, scope) { - if ( - !node || - node.type === 'Literal' || - node.type === 'TemplateLiteral' || - node.type === 'ArrowFunctionExpression' || - node.type === 'FunctionExpression' || - node.type === 'UnaryExpression' || - node.type === 'BinaryExpression' || - (node.type === 'Identifier' && node.name === 'undefined') - ) { - return false; - } - - if (node.type === 'Identifier' && scope !== null) { - const binding = scope.get(node.name); - // Let's see if the reference is something that can be proxied - if ( - binding !== null && - !binding.reassigned && - binding.initial !== null && - binding.initial.type !== 'FunctionDeclaration' && - binding.initial.type !== 'ClassDeclaration' && - binding.initial.type !== 'ImportDeclaration' && - binding.initial.type !== 'EachBlock' && - binding.initial.type !== 'SnippetBlock' - ) { - return should_proxy(binding.initial, null); - } - } - - return true; -} - /** * An async thunk. If an `await` inside restores the reaction context via `$.save`, * the body exits through `$.unsave` so the context cannot leak into foreign microtasks diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/AssignmentExpression.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/AssignmentExpression.js index 7ab148f0a3..630c89da00 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/AssignmentExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/AssignmentExpression.js @@ -9,10 +9,10 @@ import { is_expression_async } from '../../../../utils/ast.js'; import { dev, locate_node } from '../../../../state.js'; -import { build_getter, should_proxy } from '../utils.js'; +import { build_getter } from '../utils.js'; +import { should_proxy, get_rune } from '../../../scope.js'; import { visit_assignment_expression } from '../../shared/assignments.js'; import { validate_mutation } from './shared/utils.js'; -import { get_rune } from '../../../scope.js'; import { get_name } from '../../../nodes.js'; /** diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js index f69bc5fe6e..cd1235d089 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/CallExpression.js @@ -2,8 +2,7 @@ /** @import { Context } from '../types' */ import { dev, is_ignored } from '../../../../state.js'; import * as b from '#compiler/builders'; -import { get_rune } from '../../../scope.js'; -import { should_proxy } from '../utils.js'; +import { get_rune, should_proxy } from '../../../scope.js'; import { get_inspect_args } from '../../utils.js'; /** diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js index 246feaccf6..81aee35336 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js @@ -5,14 +5,8 @@ import { dev, is_ignored, locate_node } from '../../../../state.js'; import { extract_paths, save } from '../../../../utils/ast.js'; import * as b from '#compiler/builders'; import * as assert from '../../../../utils/assert.js'; -import { get_rune } from '../../../scope.js'; -import { - async_thunk, - get_prop_source, - is_prop_source, - is_state_source, - should_proxy -} from '../utils.js'; +import { get_rune, should_proxy } from '../../../scope.js'; +import { async_thunk, get_prop_source, is_prop_source, is_state_source } from '../utils.js'; import { get_value } from './shared/declarations.js'; /** diff --git a/packages/svelte/src/compiler/phases/3-transform/utils.js b/packages/svelte/src/compiler/phases/3-transform/utils.js index c7fce61ff9..a57fa85ec2 100644 --- a/packages/svelte/src/compiler/phases/3-transform/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/utils.js @@ -9,7 +9,7 @@ import { import * as e from '../../errors.js'; import { walk } from 'zimmerframe'; import { extract_identifiers } from '../../utils/ast.js'; -import check_graph_for_cycles from '../2-analyze/utils/check_graph_for_cycles.js'; +import check_graph_for_cycles from '../../utils/check_graph_for_cycles.js'; import is_reference from 'is-reference'; import { set_scope } from '../scope.js'; diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index e3560753b4..ab92f8111f 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -1469,3 +1469,41 @@ function get_global_keypath(node, scope) { return n.name + joined; } + +/** + * @param {Expression} node + * @param {Scope | null} scope + */ +export function should_proxy(node, scope) { + if ( + !node || + node.type === 'Literal' || + node.type === 'TemplateLiteral' || + node.type === 'ArrowFunctionExpression' || + node.type === 'FunctionExpression' || + node.type === 'UnaryExpression' || + node.type === 'BinaryExpression' || + (node.type === 'Identifier' && node.name === 'undefined') + ) { + return false; + } + + if (node.type === 'Identifier' && scope !== null) { + const binding = scope.get(node.name); + // Let's see if the reference is something that can be proxied + if ( + binding !== null && + !binding.reassigned && + binding.initial !== null && + binding.initial.type !== 'FunctionDeclaration' && + binding.initial.type !== 'ClassDeclaration' && + binding.initial.type !== 'ImportDeclaration' && + binding.initial.type !== 'EachBlock' && + binding.initial.type !== 'SnippetBlock' + ) { + return should_proxy(binding.initial, null); + } + } + + return true; +} diff --git a/packages/svelte/src/compiler/phases/2-analyze/utils/check_graph_for_cycles.js b/packages/svelte/src/compiler/utils/check_graph_for_cycles.js similarity index 100% rename from packages/svelte/src/compiler/phases/2-analyze/utils/check_graph_for_cycles.js rename to packages/svelte/src/compiler/utils/check_graph_for_cycles.js