Skip to content

Commit 033a466

Browse files
misc
1 parent fb8d6d7 commit 033a466

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/ClassDeclaration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ export function ClassDeclaration(node, context) {
2222
w.perf_avoid_nested_class(node);
2323
}
2424

25-
context.next({ ...context.state, class_state: new ClassAnalysis() });
25+
context.next({
26+
...context.state,
27+
class_state: context.state.analysis.runes ? new ClassAnalysis() : null
28+
});
2629
}

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/class-analysis.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { get_parent } from '../../../../utils/ast.js';
66
import { get_rune } from '../../../scope.js';
77
import * as e from '../../../../errors.js';
88
import { locate_node } from '../../../../state.js';
9+
import { is_state_creation_rune } from '../../../../../utils.js';
910

1011
/** @typedef {'$state' | '$state.raw' | '$derived' | '$derived.by' | 'regular'} PropertyAssignmentType */
1112
/** @typedef {{ type: PropertyAssignmentType; node: AssignmentExpression | PropertyDefinition; }} PropertyAssignmentDetails */
@@ -67,7 +68,7 @@ export class ClassAnalysis {
6768
* @template {AST.SvelteNode} T
6869
* @param {AST.SvelteNode} node
6970
* @param {T[]} path
70-
* @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' } } }}
71+
* @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' | 'PrivateIdentifier' } } }}
7172
*/
7273
is_class_property_assignment_at_constructor_root(node, path) {
7374
if (
@@ -76,7 +77,8 @@ export class ClassAnalysis {
7677
node.operator === '=' &&
7778
node.left.type === 'MemberExpression' &&
7879
node.left.object.type === 'ThisExpression' &&
79-
node.left.property.type === 'Identifier'
80+
(node.left.property.type === 'Identifier' ||
81+
node.left.property.type === 'PrivateIdentifier')
8082
)
8183
) {
8284
return false;
@@ -137,8 +139,8 @@ export class ClassAnalysis {
137139
if (rune === null) {
138140
return 'regular';
139141
}
140-
if (property_assignment_types.has(rune)) {
141-
return /** @type {PropertyAssignmentType} */ (rune);
142+
if (is_state_creation_rune(rune)) {
143+
return rune;
142144
}
143145
// this does mean we return `regular` for some other runes (like `$trace` or `$state.raw`)
144146
// -- this is ok because the rune placement rules will throw if they're invalid.

packages/svelte/src/utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,28 @@ const RUNES = /** @type {const} */ ([
447447
'$host'
448448
]);
449449

450+
/** @typedef {RUNES[number]} RuneName */
451+
450452
/**
451453
* @param {string} name
452-
* @returns {name is RUNES[number]}
454+
* @returns {name is RuneName}
453455
*/
454456
export function is_rune(name) {
455457
return RUNES.includes(/** @type {RUNES[number]} */ (name));
456458
}
457459

460+
/** @typedef {'$state' | '$state.raw' | '$derived' | '$derived.by'} StateCreationRuneName */
461+
462+
/**
463+
* @param {string} name
464+
* @returns {name is StateCreationRuneName}
465+
*/
466+
export function is_state_creation_rune(name) {
467+
return (
468+
name === '$state' || name === '$state.raw' || name === '$derived' || name === '$derived.by'
469+
);
470+
}
471+
458472
/** List of elements that require raw contents and should not have SSR comments put in them */
459473
const RAW_TEXT_ELEMENTS = /** @type {const} */ (['textarea', 'script', 'style', 'title']);
460474

0 commit comments

Comments
 (0)