1
+ /** @import { ClassDeclaration, Expression, FunctionDeclaration, Identifier, ImportDeclaration, MemberExpression, Node, Pattern, VariableDeclarator } from 'estree' */
2
+ /** @import { Context, Visitor, Visitors } from 'zimmerframe' */
3
+ /** @import { AnimateDirective, Binding, DeclarationKind, EachBlock, ElementLike, LetDirective, SvelteNode, TransitionDirective, UseDirective } from '#compiler' */
1
4
import is_reference from 'is-reference' ;
2
5
import { walk } from 'zimmerframe' ;
3
6
import { is_element_node } from './nodes.js' ;
@@ -30,20 +33,20 @@ export class Scope {
30
33
/**
31
34
* A map of every identifier declared by this scope, and all the
32
35
* identifiers that reference it
33
- * @type {Map<string, import('#compiler'). Binding> }
36
+ * @type {Map<string, Binding> }
34
37
*/
35
38
declarations = new Map ( ) ;
36
39
37
40
/**
38
41
* A map of declarators to the bindings they declare
39
- * @type {Map<import('estree'). VariableDeclarator | import('#compiler'). LetDirective, import('#compiler'). Binding[]> }
42
+ * @type {Map<VariableDeclarator | LetDirective, Binding[]> }
40
43
*/
41
44
declarators = new Map ( ) ;
42
45
43
46
/**
44
47
* A set of all the names referenced with this scope
45
48
* — useful for generating unique names
46
- * @type {Map<string, { node: import('estree'). Identifier; path: import('#compiler'). SvelteNode[] }[]> }
49
+ * @type {Map<string, { node: Identifier; path: SvelteNode[] }[]> }
47
50
*/
48
51
references = new Map ( ) ;
49
52
@@ -67,11 +70,11 @@ export class Scope {
67
70
}
68
71
69
72
/**
70
- * @param {import('estree'). Identifier } node
71
- * @param {import('#compiler'). Binding['kind'] } kind
72
- * @param {import('#compiler'). DeclarationKind } declaration_kind
73
- * @param {null | import('estree'). Expression | import('estree'). FunctionDeclaration | import('estree'). ClassDeclaration | import('estree'). ImportDeclaration | import('../types/template.js'). EachBlock } initial
74
- * @returns {import('#compiler'). Binding }
73
+ * @param {Identifier } node
74
+ * @param {Binding['kind'] } kind
75
+ * @param {DeclarationKind } declaration_kind
76
+ * @param {null | Expression | FunctionDeclaration | ClassDeclaration | ImportDeclaration | EachBlock } initial
77
+ * @returns {Binding }
75
78
*/
76
79
declare ( node , kind , declaration_kind , initial = null ) {
77
80
if ( node . name === '$' ) {
@@ -103,7 +106,7 @@ export class Scope {
103
106
e . declaration_duplicate ( node , node . name ) ;
104
107
}
105
108
106
- /** @type {import('#compiler'). Binding } */
109
+ /** @type {Binding } */
107
110
const binding = {
108
111
node,
109
112
references : [ ] ,
@@ -157,15 +160,15 @@ export class Scope {
157
160
158
161
/**
159
162
* @param {string } name
160
- * @returns {import('#compiler'). Binding | null }
163
+ * @returns {Binding | null }
161
164
*/
162
165
get ( name ) {
163
166
return this . declarations . get ( name ) ?? this . parent ?. get ( name ) ?? null ;
164
167
}
165
168
166
169
/**
167
- * @param {import('estree'). VariableDeclarator | import('#compiler'). LetDirective } node
168
- * @returns {import('#compiler'). Binding[] }
170
+ * @param {VariableDeclarator | LetDirective } node
171
+ * @returns {Binding[] }
169
172
*/
170
173
get_bindings ( node ) {
171
174
const bindings = this . declarators . get ( node ) ;
@@ -184,8 +187,8 @@ export class Scope {
184
187
}
185
188
186
189
/**
187
- * @param {import('estree'). Identifier } node
188
- * @param {import('#compiler'). SvelteNode[] } path
190
+ * @param {Identifier } node
191
+ * @param {SvelteNode[] } path
189
192
*/
190
193
reference ( node , path ) {
191
194
path = [ ...path ] ; // ensure that mutations to path afterwards don't affect this reference
@@ -231,7 +234,7 @@ export class ScopeRoot {
231
234
}
232
235
233
236
/**
234
- * @param {import('#compiler'). SvelteNode } ast
237
+ * @param {SvelteNode } ast
235
238
* @param {ScopeRoot } root
236
239
* @param {boolean } allow_reactive_declarations
237
240
* @param {Scope | null } parent
@@ -241,7 +244,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
241
244
242
245
/**
243
246
* A map of node->associated scope. A node appearing in this map does not necessarily mean that it created a scope
244
- * @type {Map<import('#compiler'). SvelteNode, Scope> }
247
+ * @type {Map<SvelteNode, Scope> }
245
248
*/
246
249
const scopes = new Map ( ) ;
247
250
const scope = new Scope ( root , parent , false ) ;
@@ -250,21 +253,21 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
250
253
/** @type {State } */
251
254
const state = { scope } ;
252
255
253
- /** @type {[Scope, { node: import('estree'). Identifier; path: import('#compiler'). SvelteNode[] }][] } */
256
+ /** @type {[Scope, { node: Identifier; path: SvelteNode[] }][] } */
254
257
const references = [ ] ;
255
258
256
- /** @type {[Scope, import('estree'). Pattern | import('estree'). MemberExpression][] } */
259
+ /** @type {[Scope, Pattern | MemberExpression][] } */
257
260
const updates = [ ] ;
258
261
259
262
/**
260
263
* An array of reactive declarations, i.e. the `a` in `$: a = b * 2`
261
- * @type {import('estree'). Identifier[] }
264
+ * @type {Identifier[] }
262
265
*/
263
266
const possible_implicit_declarations = [ ] ;
264
267
265
268
/**
266
269
* @param {Scope } scope
267
- * @param {import('estree'). Pattern[] } params
270
+ * @param {Pattern[] } params
268
271
*/
269
272
function add_params ( scope , params ) {
270
273
for ( const param of params ) {
@@ -275,7 +278,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
275
278
}
276
279
277
280
/**
278
- * @type {import('zimmerframe'). Visitor<import('estree'). Node, State, import('#compiler'). SvelteNode> }
281
+ * @type {Visitor<Node, State, SvelteNode> }
279
282
*/
280
283
const create_block_scope = ( node , { state, next } ) => {
281
284
const scope = state . scope . child ( true ) ;
@@ -285,7 +288,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
285
288
} ;
286
289
287
290
/**
288
- * @type {import('zimmerframe'). Visitor<import('#compiler'). ElementLike, State, import('#compiler'). SvelteNode> }
291
+ * @type {Visitor<ElementLike, State, SvelteNode> }
289
292
*/
290
293
const SvelteFragment = ( node , { state, next } ) => {
291
294
const [ scope ] = analyze_let_directives ( node , state . scope ) ;
@@ -294,7 +297,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
294
297
} ;
295
298
296
299
/**
297
- * @param {import('#compiler'). ElementLike } node
300
+ * @param {ElementLike } node
298
301
* @param {Scope } parent
299
302
*/
300
303
function analyze_let_directives ( node , parent ) {
@@ -303,7 +306,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
303
306
304
307
for ( const attribute of node . attributes ) {
305
308
if ( attribute . type === 'LetDirective' ) {
306
- /** @type {import('#compiler'). Binding[] } */
309
+ /** @type {Binding[] } */
307
310
const bindings = [ ] ;
308
311
scope . declarators . set ( attribute , bindings ) ;
309
312
@@ -317,7 +320,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
317
320
bindings . push ( binding ) ;
318
321
}
319
322
} else {
320
- /** @type {import('estree'). Identifier } */
323
+ /** @type {Identifier } */
321
324
const id = {
322
325
name : attribute . name ,
323
326
type : 'Identifier' ,
@@ -336,7 +339,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
336
339
}
337
340
338
341
/**
339
- * @type {import('zimmerframe'). Visitor<import('#compiler'). AnimateDirective | import('#compiler'). TransitionDirective | import('#compiler'). UseDirective, State, import('#compiler'). SvelteNode> }
342
+ * @type {Visitor<AnimateDirective | TransitionDirective | UseDirective, State, SvelteNode> }
340
343
*/
341
344
const SvelteDirective = ( node , { state, path, visit } ) => {
342
345
state . scope . reference ( b . id ( node . name . split ( '.' ) [ 0 ] ) , path ) ;
@@ -350,7 +353,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
350
353
// references
351
354
Identifier ( node , { path, state } ) {
352
355
const parent = path . at ( - 1 ) ;
353
- if ( parent && is_reference ( node , /** @type {import('estree'). Node } */ ( parent ) ) ) {
356
+ if ( parent && is_reference ( node , /** @type {Node } */ ( parent ) ) ) {
354
357
references . push ( [ state . scope , { node, path : path . slice ( ) } ] ) ;
355
358
}
356
359
} ,
@@ -432,12 +435,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
432
435
} ,
433
436
434
437
UpdateExpression ( node , { state, next } ) {
435
- updates . push ( [
436
- state . scope ,
437
- /** @type {import('estree').Identifier | import('estree').MemberExpression } */ (
438
- node . argument
439
- )
440
- ] ) ;
438
+ updates . push ( [ state . scope , /** @type {Identifier | MemberExpression } */ ( node . argument ) ] ) ;
441
439
next ( ) ;
442
440
} ,
443
441
@@ -489,7 +487,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
489
487
VariableDeclaration ( node , { state, path, next } ) {
490
488
const is_parent_const_tag = path . at ( - 1 ) ?. type === 'ConstTag' ;
491
489
for ( const declarator of node . declarations ) {
492
- /** @type {import('#compiler'). Binding[] } */
490
+ /** @type {Binding[] } */
493
491
const bindings = [ ] ;
494
492
495
493
state . scope . declarators . set ( declarator , bindings ) ;
@@ -525,7 +523,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
525
523
526
524
EachBlock ( node , { state, visit } ) {
527
525
// Array part is still from the scope above
528
- /** @type {Set<import('estree'). Identifier> } */
526
+ /** @type {Set<Identifier> } */
529
527
const references_within = new Set ( ) ;
530
528
const idx = references . length ;
531
529
visit ( node . expression ) ;
@@ -600,7 +598,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
600
598
item : node . context . type === 'Identifier' ? node . context : b . id ( '$$item' ) ,
601
599
declarations : scope . declarations ,
602
600
references : [ ...references_within ]
603
- . map ( ( id ) => /** @type {import('#compiler'). Binding } */ ( state . scope . get ( id . name ) ) )
601
+ . map ( ( id ) => /** @type {Binding } */ ( state . scope . get ( id . name ) ) )
604
602
. filter ( Boolean ) ,
605
603
is_controlled : false
606
604
} ;
@@ -673,9 +671,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
673
671
BindDirective ( node , context ) {
674
672
updates . push ( [
675
673
context . state . scope ,
676
- /** @type {import('estree').Identifier | import('estree').MemberExpression } */ (
677
- node . expression
678
- )
674
+ /** @type {Identifier | MemberExpression } */ ( node . expression )
679
675
] ) ;
680
676
context . next ( ) ;
681
677
} ,
@@ -718,7 +714,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
718
714
object = object . object ;
719
715
}
720
716
721
- const binding = scope . get ( /** @type {import('estree'). Identifier } */ ( object ) . name ) ;
717
+ const binding = scope . get ( /** @type {Identifier } */ ( object ) . name ) ;
722
718
if ( binding ) binding . mutated = true ;
723
719
} else {
724
720
unwrap_pattern ( node ) . forEach ( ( node ) => {
@@ -742,15 +738,15 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
742
738
743
739
/**
744
740
* @template {{ scope: Scope }} State
745
- * @param {Map<import('#compiler'). SvelteNode, Scope> } scopes
746
- * @returns {import('zimmerframe'). Visitors<import('#compiler'). SvelteNode, State> }
741
+ * @param {Map<SvelteNode, Scope> } scopes
742
+ * @returns {Visitors<SvelteNode, State> }
747
743
*/
748
744
export function set_scope ( scopes ) {
749
745
return {
750
746
/**
751
747
*
752
- * @param {import('#compiler'). SvelteNode } node
753
- * @param {import('zimmerframe'). Context<import('#compiler'). SvelteNode, State> } context
748
+ * @param {SvelteNode } node
749
+ * @param {Context<SvelteNode, State> } context
754
750
*/
755
751
_ ( node , { next, state } ) {
756
752
const scope = scopes . get ( node ) ;
@@ -761,7 +757,7 @@ export function set_scope(scopes) {
761
757
762
758
/**
763
759
* Returns the name of the rune if the given expression is a `CallExpression` using a rune.
764
- * @param {import('estree'). Node | import('../types/template.js'). EachBlock | null | undefined } node
760
+ * @param {Node | EachBlock | null | undefined } node
765
761
* @param {Scope } scope
766
762
* @returns {Runes[number] | null }
767
763
*/
0 commit comments