Skip to content

Commit c7e8422

Browse files
committed
lint, tidy up
1 parent 2556031 commit c7e8422

File tree

8 files changed

+61
-87
lines changed

8 files changed

+61
-87
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import type { ComponentAnalysis } from '../../types.js';
1515
import type { SourceLocation } from '#shared';
1616

1717
export interface ClientTransformState extends TransformState {
18-
readonly state_fields: Record<string, StateField>;
19-
readonly backing_fields: Record<string, PrivateIdentifier>;
20-
2118
/**
2219
* `true` if the current lexical scope belongs to a class constructor. this allows
2320
* us to rewrite `this.foo` as `this.#foo.value`

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,46 +55,48 @@ function build_assignment(operator, left, right, context) {
5555
if (context.state.analysis.runes && left.type === 'MemberExpression') {
5656
const name = get_name(left.property);
5757

58-
// special case — state declaration in class constructor
59-
const ancestor = context.path.at(-4);
60-
61-
if (ancestor?.type === 'MethodDefinition' && ancestor.kind === 'constructor') {
62-
const rune = get_rune(right, context.state.scope);
63-
64-
if (rune) {
65-
const child_state = {
66-
...context.state,
67-
in_constructor: rune !== '$derived' && rune !== '$derived.by'
68-
};
69-
70-
const l = b.member(
71-
b.this,
72-
left.property.type === 'PrivateIdentifier'
73-
? left.property
74-
: context.state.backing_fields[name]
75-
);
58+
if (name !== null) {
59+
// special case — state declaration in class constructor
60+
const ancestor = context.path.at(-4);
61+
62+
if (ancestor?.type === 'MethodDefinition' && ancestor.kind === 'constructor') {
63+
const rune = get_rune(right, context.state.scope);
64+
65+
if (rune) {
66+
const child_state = {
67+
...context.state,
68+
in_constructor: rune !== '$derived' && rune !== '$derived.by'
69+
};
70+
71+
const l = b.member(
72+
b.this,
73+
left.property.type === 'PrivateIdentifier'
74+
? left.property
75+
: context.state.backing_fields[name]
76+
);
7677

77-
const r = /** @type {Expression} */ (context.visit(right, child_state));
78+
const r = /** @type {Expression} */ (context.visit(right, child_state));
7879

79-
return b.assignment(operator, l, r);
80+
return b.assignment(operator, l, r);
81+
}
8082
}
81-
}
8283

83-
// special case — assignment to private state field
84-
if (left.property.type === 'PrivateIdentifier') {
85-
const field = context.state.state_fields[name];
84+
// special case — assignment to private state field
85+
if (left.property.type === 'PrivateIdentifier') {
86+
const field = context.state.state_fields[name];
8687

87-
if (field) {
88-
let value = /** @type {Expression} */ (
89-
context.visit(build_assignment_value(operator, left, right))
90-
);
88+
if (field) {
89+
let value = /** @type {Expression} */ (
90+
context.visit(build_assignment_value(operator, left, right))
91+
);
9192

92-
const needs_proxy =
93-
field.type === '$state' &&
94-
is_non_coercive_operator(operator) &&
95-
should_proxy(value, context.state.scope);
93+
const needs_proxy =
94+
field.type === '$state' &&
95+
is_non_coercive_operator(operator) &&
96+
should_proxy(value, context.state.scope);
9697

97-
return b.call('$.set', left, value, needs_proxy && b.true);
98+
return b.call('$.set', left, value, needs_proxy && b.true);
99+
}
98100
}
99101
}
100102
}

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { Identifier } from './visitors/Identifier.js';
2323
import { IfBlock } from './visitors/IfBlock.js';
2424
import { KeyBlock } from './visitors/KeyBlock.js';
2525
import { LabeledStatement } from './visitors/LabeledStatement.js';
26-
import { MemberExpression } from './visitors/MemberExpression.js';
2726
import { PropertyDefinition } from './visitors/PropertyDefinition.js';
2827
import { RegularElement } from './visitors/RegularElement.js';
2928
import { RenderTag } from './visitors/RenderTag.js';
@@ -49,7 +48,6 @@ const global_visitors = {
4948
ExpressionStatement,
5049
Identifier,
5150
LabeledStatement,
52-
MemberExpression,
5351
PropertyDefinition,
5452
UpdateExpression,
5553
VariableDeclaration
@@ -99,7 +97,8 @@ export function server_component(analysis, options) {
9997
template: /** @type {any} */ (null),
10098
namespace: options.namespace,
10199
preserve_whitespace: options.preserveWhitespace,
102-
private_derived: new Map(),
100+
state_fields: {},
101+
backing_fields: {},
103102
skip_hydration_boundaries: false
104103
};
105104

@@ -395,7 +394,8 @@ export function server_module(analysis, options) {
395394
// to be present for `javascript_visitors_legacy` and so is included in module
396395
// transform state as well as component transform state
397396
legacy_reactive_statements: new Map(),
398-
private_derived: new Map()
397+
state_fields: {},
398+
backing_fields: {}
399399
};
400400

401401
const module = /** @type {Program} */ (

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import type { Expression, Statement, ModuleDeclaration, LabeledStatement } from
22
import type { AST, Namespace, ValidatedCompileOptions } from '#compiler';
33
import type { TransformState } from '../types.js';
44
import type { ComponentAnalysis } from '../../types.js';
5-
import type { StateField } from '../client/types.js';
65

76
export interface ServerTransformState extends TransformState {
87
/** The $: calls, which will be ordered in the end */
98
readonly legacy_reactive_statements: Map<LabeledStatement, Statement>;
10-
readonly private_derived: Map<string, StateField>;
119
}
1210

1311
export interface ComponentServerTransformState extends ServerTransformState {

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,27 @@ export function AssignmentExpression(node, context) {
2525
*/
2626
function build_assignment(operator, left, right, context) {
2727
if (context.state.analysis.runes && left.type === 'MemberExpression') {
28-
// special case — state declaration in class constructor
29-
const ancestor = context.path.at(-4);
28+
const name = get_name(left.property);
3029

31-
if (ancestor?.type === 'MethodDefinition' && ancestor.kind === 'constructor') {
32-
const rune = get_rune(right, context.state.scope);
30+
if (name !== null) {
31+
// special case — state declaration in class constructor
32+
const ancestor = context.path.at(-4);
3333

34-
if (rune) {
35-
const name = get_name(left.property);
36-
const key =
37-
left.property.type === 'PrivateIdentifier' || rune === '$state' || rune === '$state.raw'
38-
? left.property
39-
: context.state.backing_fields[name];
34+
if (ancestor?.type === 'MethodDefinition' && ancestor.kind === 'constructor') {
35+
const rune = get_rune(right, context.state.scope);
4036

41-
const l = b.member(b.this, key, key.type === 'Literal');
37+
if (rune) {
38+
const key =
39+
left.property.type === 'PrivateIdentifier' || rune === '$state' || rune === '$state.raw'
40+
? left.property
41+
: context.state.backing_fields[name];
4242

43-
const r = /** @type {Expression} */ (context.visit(right));
43+
const l = b.member(b.this, key, key.type === 'Literal');
4444

45-
return b.assignment(operator, l, r);
45+
const r = /** @type {Expression} */ (context.visit(right));
46+
47+
return b.assignment(operator, l, r);
48+
}
4649
}
4750
}
4851
}

packages/svelte/src/compiler/phases/3-transform/server/visitors/ClassBody.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,9 @@ export function ClassBody(node, context) {
6969
const backing = backing_fields[name];
7070
const member = b.member(b.this, backing);
7171

72-
const should_proxy = field.type === '$state' && true; // TODO
73-
74-
const key = b.key(name);
75-
7672
body.push(
7773
b.prop_def(backing, null),
78-
79-
b.method('get', key, [], [b.return(b.call(member))])
74+
b.method('get', b.key(name), [], [b.return(b.call(member))])
8075
);
8176
}
8277
}
@@ -108,8 +103,6 @@ export function ClassBody(node, context) {
108103
const backing = backing_fields[name];
109104
const member = b.member(b.this, backing);
110105

111-
const should_proxy = field.type === '$state' && true; // TODO
112-
113106
body.push(
114107
b.prop_def(
115108
backing,

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

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import type { Scope } from '../scope.js';
2-
import type { AST, ValidatedModuleCompileOptions } from '#compiler';
2+
import type { AST, StateField, ValidatedModuleCompileOptions } from '#compiler';
33
import type { Analysis } from '../types.js';
4+
import type { PrivateIdentifier } from 'estree';
45

56
export interface TransformState {
67
readonly analysis: Analysis;
78
readonly options: ValidatedModuleCompileOptions;
89
readonly scope: Scope;
910
readonly scopes: Map<AST.SvelteNode, Scope>;
11+
12+
readonly state_fields: Record<string, StateField>;
13+
readonly backing_fields: Record<string, PrivateIdentifier>;
1014
}

0 commit comments

Comments
 (0)