From 7341f403afb1eb6e39223979c3155b51108c9a2a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 15 May 2025 17:34:52 -0400 Subject: [PATCH] simplify --- .../2-analyze/visitors/CallExpression.js | 34 ++++++++++++++++--- .../phases/2-analyze/visitors/ClassBody.js | 33 +----------------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js index 009f688e08..ba690eb661 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js @@ -118,10 +118,7 @@ export function CallExpression(node, context) { const valid = is_variable_declaration(parent, context) || is_class_property_definition(parent) || - context.state.class?.is_class_property_assignment_at_constructor_root( - parent, - context.path.slice(0, -1) - ); + is_class_property_assignment_at_constructor_root(parent, context); if (!valid) { e.state_invalid_placement(node, rune); @@ -292,3 +289,32 @@ function is_variable_declaration(parent, context) { function is_class_property_definition(parent) { return parent.type === 'PropertyDefinition' && !parent.static && !parent.computed; } + +/** + * @param {AST.SvelteNode} node + * @param {Context} context + * @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' | 'PrivateIdentifier' | 'Literal' } } }} + */ +function is_class_property_assignment_at_constructor_root(node, context) { + if ( + !( + node.type === 'AssignmentExpression' && + node.operator === '=' && + node.left.type === 'MemberExpression' && + node.left.object.type === 'ThisExpression' && + ((node.left.property.type === 'Identifier' && !node.left.computed) || + node.left.property.type === 'PrivateIdentifier' || + node.left.property.type === 'Literal') + ) + ) { + return false; + } + + // AssignmentExpression (here) -> ExpressionStatement (-1) -> BlockStatement (-2) -> FunctionExpression (-3) -> MethodDefinition (-4) + const maybe_constructor = get_parent(context.path, -5); + return ( + maybe_constructor && + maybe_constructor.type === 'MethodDefinition' && + maybe_constructor.kind === 'constructor' + ); +} diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js index 233223de69..9392c298d8 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassBody.js @@ -1,8 +1,6 @@ -/** @import { AssignmentExpression, ClassBody, PropertyDefinition, Expression, Identifier, PrivateIdentifier, Literal, MethodDefinition } from 'estree' */ -/** @import { AST } from '#compiler' */ +/** @import { AssignmentExpression, ClassBody, PropertyDefinition, Expression, PrivateIdentifier, MethodDefinition } from 'estree' */ /** @import { Context } from '../types' */ /** @import { StateCreationRuneName } from '../../../../utils.js' */ -import { get_parent } from '../../../utils/ast.js'; import { get_rune } from '../../scope.js'; import * as e from '../../../errors.js'; import { is_state_creation_rune } from '../../../../utils.js'; @@ -102,33 +100,4 @@ function get_name(node) { class ClassAnalysis { /** @type {Record} */ fields = {}; - - /** - * @template {AST.SvelteNode} T - * @param {AST.SvelteNode} node - * @param {T[]} path - * @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' | 'PrivateIdentifier' | 'Literal' } } }} - */ - is_class_property_assignment_at_constructor_root(node, path) { - if ( - !( - node.type === 'AssignmentExpression' && - node.operator === '=' && - node.left.type === 'MemberExpression' && - node.left.object.type === 'ThisExpression' && - ((node.left.property.type === 'Identifier' && !node.left.computed) || - node.left.property.type === 'PrivateIdentifier' || - node.left.property.type === 'Literal') - ) - ) { - return false; - } - // AssignmentExpression (here) -> ExpressionStatement (-1) -> BlockStatement (-2) -> FunctionExpression (-3) -> MethodDefinition (-4) - const maybe_constructor = get_parent(path, -4); - return ( - maybe_constructor && - maybe_constructor.type === 'MethodDefinition' && - maybe_constructor.kind === 'constructor' - ); - } }