Skip to content

Commit adb6e71

Browse files
feat: It is now at least backwards compatible. though the new stuff may still be wrong
1 parent 4d6422c commit adb6e71

File tree

18 files changed

+598
-481
lines changed

18 files changed

+598
-481
lines changed

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
Statement,
44
LabeledStatement,
55
Identifier,
6-
PrivateIdentifier,
76
Expression,
87
AssignmentExpression,
98
UpdateExpression,
@@ -13,11 +12,10 @@ import type { AST, Namespace, ValidatedCompileOptions } from '#compiler';
1312
import type { TransformState } from '../types.js';
1413
import type { ComponentAnalysis } from '../../types.js';
1514
import type { SourceLocation } from '#shared';
16-
import type { StateCreationRuneName } from '../../../../utils.js';
17-
import type { ClassAnalysis } from './visitors/shared/class-analysis.js';
15+
import type { ClassAnalysis } from '../shared/types.js';
1816

1917
export interface ClientTransformState extends TransformState {
20-
readonly class_analysis: ClassAnalysis | null;
18+
readonly class_analysis: ClassAnalysis<Context> | null;
2119

2220
/**
2321
* `true` if the current lexical scope belongs to a class constructor. this allows
@@ -95,11 +93,6 @@ export interface ComponentClientTransformState extends ClientTransformState {
9593
readonly module_level_snippets: VariableDeclaration[];
9694
}
9795

98-
export interface StateField {
99-
kind: StateCreationRuneName;
100-
id: PrivateIdentifier;
101-
}
102-
10396
export type Context = import('zimmerframe').Context<AST.SvelteNode, ClientTransformState>;
10497
export type Visitors = import('zimmerframe').Visitors<AST.SvelteNode, any>;
10598

packages/svelte/src/compiler/phases/3-transform/client/visitors/AssignmentExpression.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ function build_assignment(operator, left, right, context) {
5757
left.type === 'MemberExpression' &&
5858
left.property.type === 'PrivateIdentifier'
5959
) {
60-
const private_state = context.state.class_analysis?.private_state.get(left.property.name);
60+
const private_state = context.state.class_analysis?.get_field(left.property.name, true);
6161

6262
if (private_state !== undefined) {
6363
let value = /** @type {Expression} */ (
6464
context.visit(build_assignment_value(operator, left, right))
6565
);
6666

6767
const needs_proxy =
68-
private_state.kind === '$state' &&
68+
private_state?.kind === '$state' &&
6969
is_non_coercive_operator(operator) &&
7070
should_proxy(value, context.state.scope);
7171

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
/** @import { ClassBody, Identifier, Literal, MethodDefinition, PrivateIdentifier, PropertyDefinition } from 'estree' */
2-
/** @import { Context, StateField } from '../types' */
3-
import { regex_invalid_identifier_chars } from '../../../patterns.js';
4-
import { ClassAnalysis } from './shared/class-analysis.js';
1+
/** @import { ClassBody } from 'estree' */
2+
/** @import { Context } from '../types' */
3+
import { create_client_class_analysis } from './shared/client-class-analysis.js';
54

65
/**
76
* @param {ClassBody} node
@@ -13,46 +12,8 @@ export function ClassBody(node, context) {
1312
return;
1413
}
1514

16-
const class_analysis = new ClassAnalysis();
17-
18-
for (const definition of node.body) {
19-
class_analysis.register_body_definition(definition, context.state.scope);
20-
}
21-
22-
class_analysis.finalize_property_definitions();
23-
24-
/** @type {Array<MethodDefinition | PropertyDefinition>} */
25-
const body = [];
26-
27-
const child_state = {
28-
...context.state,
29-
class_analysis
30-
};
31-
32-
// we need to visit the constructor first so that it can add to the field maps.
33-
const constructor_node = node.body.find(
34-
(child) => child.type === 'MethodDefinition' && child.kind === 'constructor'
35-
);
36-
const constructor = constructor_node && context.visit(constructor_node, child_state);
37-
38-
// Replace parts of the class body
39-
for (const definition of node.body) {
40-
if (definition === constructor_node) {
41-
body.push(/** @type {MethodDefinition} */ (constructor));
42-
continue;
43-
}
44-
45-
const state_field = class_analysis.build_state_field_from_body_definition(definition, context);
46-
47-
if (state_field) {
48-
body.push(...state_field);
49-
continue;
50-
}
51-
52-
body.push(/** @type {MethodDefinition} **/ (context.visit(definition, child_state)));
53-
}
54-
55-
body.push(...class_analysis.constructor_state_fields);
15+
const class_analysis = create_client_class_analysis(node.body);
16+
const body = class_analysis.generate_body(context);
5617

5718
return { ...node, body };
5819
}

packages/svelte/src/compiler/phases/3-transform/client/visitors/MemberExpression.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as b from '#compiler/builders';
99
export function MemberExpression(node, context) {
1010
// rewrite `this.#foo` as `this.#foo.v` inside a constructor
1111
if (node.property.type === 'PrivateIdentifier') {
12-
const field = context.state.class_analysis?.private_state.get(node.property.name);
12+
const field = context.state.class_analysis?.get_field(node.property.name, true);
1313
if (field) {
1414
return context.state.in_constructor &&
1515
(field.kind === '$state.raw' || field.kind === '$state')

packages/svelte/src/compiler/phases/3-transform/client/visitors/UpdateExpression.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function UpdateExpression(node, context) {
1515
argument.type === 'MemberExpression' &&
1616
argument.object.type === 'ThisExpression' &&
1717
argument.property.type === 'PrivateIdentifier' &&
18-
context.state.class_analysis?.private_state.has(argument.property.name)
18+
context.state.class_analysis?.get_field(argument.property.name, true)
1919
) {
2020
let fn = '$.update';
2121
if (node.prefix) fn += '_pre';

0 commit comments

Comments
 (0)