From 033a466e846d198e90c9a2942d04f44b52951cdb Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Fri, 25 Apr 2025 14:54:34 -0600 Subject: [PATCH] misc --- .../2-analyze/visitors/ClassDeclaration.js | 5 ++++- .../2-analyze/visitors/shared/class-analysis.js | 10 ++++++---- packages/svelte/src/utils.js | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDeclaration.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDeclaration.js index 7e1df6aa29..b49ad099e5 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDeclaration.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDeclaration.js @@ -22,5 +22,8 @@ export function ClassDeclaration(node, context) { w.perf_avoid_nested_class(node); } - context.next({ ...context.state, class_state: new ClassAnalysis() }); + context.next({ + ...context.state, + class_state: context.state.analysis.runes ? new ClassAnalysis() : null + }); } diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/class-analysis.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/class-analysis.js index d6709eabc4..777762bc32 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/class-analysis.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/class-analysis.js @@ -6,6 +6,7 @@ import { get_parent } from '../../../../utils/ast.js'; import { get_rune } from '../../../scope.js'; import * as e from '../../../../errors.js'; import { locate_node } from '../../../../state.js'; +import { is_state_creation_rune } from '../../../../../utils.js'; /** @typedef {'$state' | '$state.raw' | '$derived' | '$derived.by' | 'regular'} PropertyAssignmentType */ /** @typedef {{ type: PropertyAssignmentType; node: AssignmentExpression | PropertyDefinition; }} PropertyAssignmentDetails */ @@ -67,7 +68,7 @@ export class ClassAnalysis { * @template {AST.SvelteNode} T * @param {AST.SvelteNode} node * @param {T[]} path - * @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' } } }} + * @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' | 'PrivateIdentifier' } } }} */ is_class_property_assignment_at_constructor_root(node, path) { if ( @@ -76,7 +77,8 @@ export class ClassAnalysis { node.operator === '=' && node.left.type === 'MemberExpression' && node.left.object.type === 'ThisExpression' && - node.left.property.type === 'Identifier' + (node.left.property.type === 'Identifier' || + node.left.property.type === 'PrivateIdentifier') ) ) { return false; @@ -137,8 +139,8 @@ export class ClassAnalysis { if (rune === null) { return 'regular'; } - if (property_assignment_types.has(rune)) { - return /** @type {PropertyAssignmentType} */ (rune); + if (is_state_creation_rune(rune)) { + return rune; } // this does mean we return `regular` for some other runes (like `$trace` or `$state.raw`) // -- this is ok because the rune placement rules will throw if they're invalid. diff --git a/packages/svelte/src/utils.js b/packages/svelte/src/utils.js index ada318e85a..2758b9fcbc 100644 --- a/packages/svelte/src/utils.js +++ b/packages/svelte/src/utils.js @@ -447,14 +447,28 @@ const RUNES = /** @type {const} */ ([ '$host' ]); +/** @typedef {RUNES[number]} RuneName */ + /** * @param {string} name - * @returns {name is RUNES[number]} + * @returns {name is RuneName} */ export function is_rune(name) { return RUNES.includes(/** @type {RUNES[number]} */ (name)); } +/** @typedef {'$state' | '$state.raw' | '$derived' | '$derived.by'} StateCreationRuneName */ + +/** + * @param {string} name + * @returns {name is StateCreationRuneName} + */ +export function is_state_creation_rune(name) { + return ( + name === '$state' || name === '$state.raw' || name === '$derived' || name === '$derived.by' + ); +} + /** List of elements that require raw contents and should not have SSR comments put in them */ const RAW_TEXT_ELEMENTS = /** @type {const} */ (['textarea', 'script', 'style', 'title']);