Skip to content

Commit 3c4a8d4

Browse files
authored
chore: make Binding a class (#15359)
* chore: make Binding a class * reorder * regenerate
1 parent 1e1aea4 commit 3c4a8d4

File tree

3 files changed

+83
-86
lines changed

3 files changed

+83
-86
lines changed

packages/svelte/src/compiler/phases/scope.js

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, MemberExpression, Node, Pattern, VariableDeclarator } from 'estree' */
22
/** @import { Context, Visitor } from 'zimmerframe' */
3-
/** @import { AST, Binding, DeclarationKind } from '#compiler' */
3+
/** @import { AST, BindingKind, DeclarationKind } from '#compiler' */
44
import is_reference from 'is-reference';
55
import { walk } from 'zimmerframe';
66
import { create_expression_metadata } from './nodes.js';
@@ -16,6 +16,69 @@ import { is_reserved, is_rune } from '../../utils.js';
1616
import { determine_slot } from '../utils/slot.js';
1717
import { validate_identifier_name } from './2-analyze/visitors/shared/utils.js';
1818

19+
export class Binding {
20+
/** @type {Scope} */
21+
scope;
22+
23+
/** @type {Identifier} */
24+
node;
25+
26+
/** @type {BindingKind} */
27+
kind;
28+
29+
/** @type {DeclarationKind} */
30+
declaration_kind;
31+
32+
/**
33+
* What the value was initialized with.
34+
* For destructured props such as `let { foo = 'bar' } = $props()` this is `'bar'` and not `$props()`
35+
* @type {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | AST.EachBlock | AST.SnippetBlock}
36+
*/
37+
initial;
38+
39+
/** @type {Array<{ node: Identifier; path: AST.SvelteNode[] }>} */
40+
references = [];
41+
42+
/**
43+
* For `legacy_reactive`: its reactive dependencies
44+
* @type {Binding[]}
45+
*/
46+
legacy_dependencies = [];
47+
48+
/**
49+
* Legacy props: the `class` in `{ export klass as class}`. $props(): The `class` in { class: klass } = $props()
50+
* @type {string | null}
51+
*/
52+
prop_alias = null;
53+
54+
/**
55+
* Additional metadata, varies per binding type
56+
* @type {null | { inside_rest?: boolean }}
57+
*/
58+
metadata = null;
59+
60+
is_called = false;
61+
mutated = false;
62+
reassigned = false;
63+
updated = false;
64+
65+
/**
66+
*
67+
* @param {Scope} scope
68+
* @param {Identifier} node
69+
* @param {BindingKind} kind
70+
* @param {DeclarationKind} declaration_kind
71+
* @param {Binding['initial']} initial
72+
*/
73+
constructor(scope, node, kind, declaration_kind, initial) {
74+
this.scope = scope;
75+
this.node = node;
76+
this.initial = initial;
77+
this.kind = kind;
78+
this.declaration_kind = declaration_kind;
79+
}
80+
}
81+
1982
export class Scope {
2083
/** @type {ScopeRoot} */
2184
root;
@@ -100,22 +163,7 @@ export class Scope {
100163
e.declaration_duplicate(node, node.name);
101164
}
102165

103-
/** @type {Binding} */
104-
const binding = {
105-
node,
106-
references: [],
107-
legacy_dependencies: [],
108-
initial,
109-
reassigned: false,
110-
mutated: false,
111-
updated: false,
112-
scope: this,
113-
kind,
114-
declaration_kind,
115-
is_called: false,
116-
prop_alias: null,
117-
metadata: null
118-
};
166+
const binding = new Binding(this, node, kind, declaration_kind, initial);
119167

120168
validate_identifier_name(binding, this.function_depth);
121169

packages/svelte/src/compiler/types/index.d.ts

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import type {
2-
ClassDeclaration,
3-
Expression,
4-
FunctionDeclaration,
5-
Identifier,
6-
ImportDeclaration
7-
} from 'estree';
81
import type { SourceMap } from 'magic-string';
9-
import type { Scope } from '../phases/scope.js';
2+
import type { Binding } from '../phases/scope.js';
103
import type { AST, Namespace } from './template.js';
114
import type { ICompileDiagnostic } from '../utils/compile_diagnostic.js';
125

@@ -241,6 +234,20 @@ export type ValidatedCompileOptions = ValidatedModuleCompileOptions &
241234
hmr: CompileOptions['hmr'];
242235
};
243236

237+
export type BindingKind =
238+
| 'normal' // A variable that is not in any way special
239+
| 'prop' // A normal prop (possibly reassigned or mutated)
240+
| 'bindable_prop' // A prop one can `bind:` to (possibly reassigned or mutated)
241+
| 'rest_prop' // A rest prop
242+
| 'raw_state' // A state variable
243+
| 'state' // A deeply reactive state variable
244+
| 'derived' // A derived variable
245+
| 'each' // An each block parameter
246+
| 'snippet' // A snippet parameter
247+
| 'store_sub' // A $store value
248+
| 'legacy_reactive' // A `$:` declaration
249+
| 'template'; // A binding declared in the template, e.g. in an `await` block or `const` tag
250+
244251
export type DeclarationKind =
245252
| 'var'
246253
| 'let'
@@ -251,66 +258,6 @@ export type DeclarationKind =
251258
| 'rest_param'
252259
| 'synthetic';
253260

254-
export interface Binding {
255-
node: Identifier;
256-
/**
257-
* - `normal`: A variable that is not in any way special
258-
* - `prop`: A normal prop (possibly reassigned or mutated)
259-
* - `bindable_prop`: A prop one can `bind:` to (possibly reassigned or mutated)
260-
* - `rest_prop`: A rest prop
261-
* - `state`: A state variable
262-
* - `derived`: A derived variable
263-
* - `each`: An each block parameter
264-
* - `snippet`: A snippet parameter
265-
* - `store_sub`: A $store value
266-
* - `legacy_reactive`: A `$:` declaration
267-
* - `template`: A binding declared in the template, e.g. in an `await` block or `const` tag
268-
*/
269-
kind:
270-
| 'normal'
271-
| 'prop'
272-
| 'bindable_prop'
273-
| 'rest_prop'
274-
| 'state'
275-
| 'raw_state'
276-
| 'derived'
277-
| 'each'
278-
| 'snippet'
279-
| 'store_sub'
280-
| 'legacy_reactive'
281-
| 'template'
282-
| 'snippet';
283-
declaration_kind: DeclarationKind;
284-
/**
285-
* What the value was initialized with.
286-
* For destructured props such as `let { foo = 'bar' } = $props()` this is `'bar'` and not `$props()`
287-
*/
288-
initial:
289-
| null
290-
| Expression
291-
| FunctionDeclaration
292-
| ClassDeclaration
293-
| ImportDeclaration
294-
| AST.EachBlock
295-
| AST.SnippetBlock;
296-
is_called: boolean;
297-
references: { node: Identifier; path: AST.SvelteNode[] }[];
298-
mutated: boolean;
299-
reassigned: boolean;
300-
/** `true` if mutated _or_ reassigned */
301-
updated: boolean;
302-
scope: Scope;
303-
/** For `legacy_reactive`: its reactive dependencies */
304-
legacy_dependencies: Binding[];
305-
/** Legacy props: the `class` in `{ export klass as class}`. $props(): The `class` in { class: klass } = $props() */
306-
prop_alias: string | null;
307-
/** Additional metadata, varies per binding type */
308-
metadata: {
309-
/** `true` if is (inside) a rest parameter */
310-
inside_rest?: boolean;
311-
} | null;
312-
}
313-
314261
export interface ExpressionMetadata {
315262
/** All the bindings that are referenced inside this expression */
316263
dependencies: Set<Binding>;
@@ -322,5 +269,7 @@ export interface ExpressionMetadata {
322269

323270
export * from './template.js';
324271

272+
export { Binding, Scope } from '../phases/scope.js';
273+
325274
// TODO this chain is a bit weird
326275
export { ReactiveStatement } from '../phases/types.js';

packages/svelte/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,8 @@ declare module 'svelte/animate' {
622622
}
623623

624624
declare module 'svelte/compiler' {
625-
import type { Expression, Identifier, ArrayExpression, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, MemberExpression, Node, ObjectExpression, Pattern, Program, ChainExpression, SimpleCallExpression, SequenceExpression } from 'estree';
626625
import type { SourceMap } from 'magic-string';
626+
import type { ArrayExpression, ArrowFunctionExpression, VariableDeclaration, VariableDeclarator, Expression, Identifier, MemberExpression, Node, ObjectExpression, Pattern, Program, ChainExpression, SimpleCallExpression, SequenceExpression } from 'estree';
627627
import type { Location } from 'locate-character';
628628
/**
629629
* `compile` converts your `.svelte` source code into a JavaScript module that exports a component

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy