1
+ /** @import { CallExpression, Expression, Identifier, Literal, MethodDefinition, PropertyDefinition, VariableDeclarator } from 'estree' */
2
+ /** @import { Binding } from '#compiler' */
3
+ /** @import { ComponentVisitors, StateField } from '../types.js' */
1
4
import { get_rune } from '../../../scope.js' ;
2
5
import { is_hoistable_function , transform_inspect_rune } from '../../utils.js' ;
3
6
import * as b from '../../../../utils/builders.js' ;
@@ -12,13 +15,13 @@ import {
12
15
import { extract_paths } from '../../../../utils/ast.js' ;
13
16
import { regex_invalid_identifier_chars } from '../../../patterns.js' ;
14
17
15
- /** @type {import('../types.js'). ComponentVisitors } */
18
+ /** @type {ComponentVisitors } */
16
19
export const javascript_visitors_runes = {
17
20
ClassBody ( node , { state, visit } ) {
18
- /** @type {Map<string, import('../types.js'). StateField> } */
21
+ /** @type {Map<string, StateField> } */
19
22
const public_state = new Map ( ) ;
20
23
21
- /** @type {Map<string, import('../types.js'). StateField> } */
24
+ /** @type {Map<string, StateField> } */
22
25
const private_state = new Map ( ) ;
23
26
24
27
/** @type {string[] } */
@@ -46,7 +49,7 @@ export const javascript_visitors_runes = {
46
49
rune === '$derived' ||
47
50
rune === '$derived.by'
48
51
) {
49
- /** @type {import('../types.js'). StateField } */
52
+ /** @type {StateField } */
50
53
const field = {
51
54
kind :
52
55
rune === '$state'
@@ -81,7 +84,7 @@ export const javascript_visitors_runes = {
81
84
field . id = b . private_id ( deconflicted ) ;
82
85
}
83
86
84
- /** @type {Array<import('estree'). MethodDefinition | import('estree'). PropertyDefinition> } */
87
+ /** @type {Array<MethodDefinition | PropertyDefinition> } */
85
88
const body = [ ] ;
86
89
87
90
const child_state = { ...state , public_state, private_state } ;
@@ -104,7 +107,7 @@ export const javascript_visitors_runes = {
104
107
let value = null ;
105
108
106
109
if ( definition . value . arguments . length > 0 ) {
107
- const init = /** @type {import('estree'). Expression } **/ (
110
+ const init = /** @type {Expression } **/ (
108
111
visit ( definition . value . arguments [ 0 ] , child_state )
109
112
) ;
110
113
@@ -182,7 +185,7 @@ export const javascript_visitors_runes = {
182
185
}
183
186
}
184
187
185
- body . push ( /** @type {import('estree'). MethodDefinition } **/ ( visit ( definition , child_state ) ) ) ;
188
+ body . push ( /** @type {MethodDefinition } **/ ( visit ( definition , child_state ) ) ) ;
186
189
}
187
190
188
191
if ( state . options . dev && public_state . size > 0 ) {
@@ -225,15 +228,11 @@ export const javascript_visitors_runes = {
225
228
if ( init != null && is_hoistable_function ( init ) ) {
226
229
const hoistable_function = visit ( init ) ;
227
230
state . hoisted . push (
228
- b . declaration (
229
- 'const' ,
230
- declarator . id ,
231
- /** @type {import('estree').Expression } */ ( hoistable_function )
232
- )
231
+ b . declaration ( 'const' , declarator . id , /** @type {Expression } */ ( hoistable_function ) )
233
232
) ;
234
233
continue ;
235
234
}
236
- declarations . push ( /** @type {import('estree'). VariableDeclarator } */ ( visit ( declarator ) ) ) ;
235
+ declarations . push ( /** @type {VariableDeclarator } */ ( visit ( declarator ) ) ) ;
237
236
continue ;
238
237
}
239
238
@@ -246,7 +245,7 @@ export const javascript_visitors_runes = {
246
245
}
247
246
248
247
if ( declarator . id . type === 'Identifier' ) {
249
- /** @type {import('estree'). Expression[] } */
248
+ /** @type {Expression[] } */
250
249
const args = [ b . id ( '$$props' ) , b . array ( seen . map ( ( name ) => b . literal ( name ) ) ) ] ;
251
250
252
251
if ( state . options . dev ) {
@@ -260,20 +259,16 @@ export const javascript_visitors_runes = {
260
259
261
260
for ( const property of declarator . id . properties ) {
262
261
if ( property . type === 'Property' ) {
263
- const key = /** @type {import('estree').Identifier | import('estree').Literal } */ (
264
- property . key
265
- ) ;
262
+ const key = /** @type {Identifier | Literal } */ ( property . key ) ;
266
263
const name = key . type === 'Identifier' ? key . name : /** @type {string } */ ( key . value ) ;
267
264
268
265
seen . push ( name ) ;
269
266
270
267
let id =
271
268
property . value . type === 'AssignmentPattern' ? property . value . left : property . value ;
272
269
assert . equal ( id . type , 'Identifier' ) ;
273
- const binding = /** @type {import('#compiler').Binding } */ ( state . scope . get ( id . name ) ) ;
274
- let initial =
275
- binding . initial &&
276
- /** @type {import('estree').Expression } */ ( visit ( binding . initial ) ) ;
270
+ const binding = /** @type {Binding } */ ( state . scope . get ( id . name ) ) ;
271
+ let initial = binding . initial && /** @type {Expression } */ ( visit ( binding . initial ) ) ;
277
272
// We're adding proxy here on demand and not within the prop runtime function so that
278
273
// people not using proxied state anywhere in their code don't have to pay the additional bundle size cost
279
274
if (
@@ -289,14 +284,12 @@ export const javascript_visitors_runes = {
289
284
}
290
285
} else {
291
286
// RestElement
292
- /** @type {import('estree'). Expression[] } */
287
+ /** @type {Expression[] } */
293
288
const args = [ b . id ( '$$props' ) , b . array ( seen . map ( ( name ) => b . literal ( name ) ) ) ] ;
294
289
295
290
if ( state . options . dev ) {
296
291
// include rest name, so we can provide informative error messages
297
- args . push (
298
- b . literal ( /** @type {import('estree').Identifier } */ ( property . argument ) . name )
299
- ) ;
292
+ args . push ( b . literal ( /** @type {Identifier } */ ( property . argument ) . name ) ) ;
300
293
}
301
294
302
295
declarations . push ( b . declarator ( property . argument , b . call ( '$.rest_props' , ...args ) ) ) ;
@@ -308,19 +301,17 @@ export const javascript_visitors_runes = {
308
301
continue ;
309
302
}
310
303
311
- const args = /** @type {import('estree'). CallExpression } */ ( init ) . arguments ;
304
+ const args = /** @type {CallExpression } */ ( init ) . arguments ;
312
305
const value =
313
- args . length === 0
314
- ? b . id ( 'undefined' )
315
- : /** @type {import('estree').Expression } */ ( visit ( args [ 0 ] ) ) ;
306
+ args . length === 0 ? b . id ( 'undefined' ) : /** @type {Expression } */ ( visit ( args [ 0 ] ) ) ;
316
307
317
308
if ( rune === '$state' || rune === '$state.frozen' ) {
318
309
/**
319
- * @param {import('estree'). Identifier } id
320
- * @param {import('estree'). Expression } value
310
+ * @param {Identifier } id
311
+ * @param {Expression } value
321
312
*/
322
313
const create_state_declarator = ( id , value ) => {
323
- const binding = /** @type {import('#compiler'). Binding } */ ( state . scope . get ( id . name ) ) ;
314
+ const binding = /** @type {Binding } */ ( state . scope . get ( id . name ) ) ;
324
315
if ( should_proxy_or_freeze ( value , state . scope ) ) {
325
316
value = b . call ( rune === '$state' ? '$.proxy' : '$.freeze' , value ) ;
326
317
}
@@ -341,9 +332,7 @@ export const javascript_visitors_runes = {
341
332
b . declarator ( b . id ( tmp ) , value ) ,
342
333
...paths . map ( ( path ) => {
343
334
const value = path . expression ?. ( b . id ( tmp ) ) ;
344
- const binding = state . scope . get (
345
- /** @type {import('estree').Identifier } */ ( path . node ) . name
346
- ) ;
335
+ const binding = state . scope . get ( /** @type {Identifier } */ ( path . node ) . name ) ;
347
336
return b . declarator (
348
337
path . node ,
349
338
binding ?. kind === 'state' || binding ?. kind === 'frozen_state'
@@ -426,7 +415,7 @@ export const javascript_visitors_runes = {
426
415
const func = context . visit ( node . expression . arguments [ 0 ] ) ;
427
416
return {
428
417
...node ,
429
- expression : b . call ( '$.user_effect' , /** @type {import('estree'). Expression } */ ( func ) )
418
+ expression : b . call ( '$.user_effect' , /** @type {Expression } */ ( func ) )
430
419
} ;
431
420
}
432
421
@@ -441,7 +430,7 @@ export const javascript_visitors_runes = {
441
430
const func = context . visit ( node . expression . arguments [ 0 ] ) ;
442
431
return {
443
432
...node ,
444
- expression : b . call ( '$.user_pre_effect' , /** @type {import('estree'). Expression } */ ( func ) )
433
+ expression : b . call ( '$.user_pre_effect' , /** @type {Expression } */ ( func ) )
445
434
} ;
446
435
}
447
436
}
@@ -460,24 +449,19 @@ export const javascript_visitors_runes = {
460
449
}
461
450
462
451
if ( rune === '$state.snapshot' ) {
463
- return b . call (
464
- '$.snapshot' ,
465
- /** @type {import('estree').Expression } */ ( context . visit ( node . arguments [ 0 ] ) )
466
- ) ;
452
+ return b . call ( '$.snapshot' , /** @type {Expression } */ ( context . visit ( node . arguments [ 0 ] ) ) ) ;
467
453
}
468
454
469
455
if ( rune === '$state.is' ) {
470
456
return b . call (
471
457
'$.is' ,
472
- /** @type {import('estree'). Expression } */ ( context . visit ( node . arguments [ 0 ] ) ) ,
473
- /** @type {import('estree'). Expression } */ ( context . visit ( node . arguments [ 1 ] ) )
458
+ /** @type {Expression } */ ( context . visit ( node . arguments [ 0 ] ) ) ,
459
+ /** @type {Expression } */ ( context . visit ( node . arguments [ 1 ] ) )
474
460
) ;
475
461
}
476
462
477
463
if ( rune === '$effect.root' ) {
478
- const args = /** @type {import('estree').Expression[] } */ (
479
- node . arguments . map ( ( arg ) => context . visit ( arg ) )
480
- ) ;
464
+ const args = /** @type {Expression[] } */ ( node . arguments . map ( ( arg ) => context . visit ( arg ) ) ) ;
481
465
return b . call ( '$.effect_root' , ...args ) ;
482
466
}
483
467
@@ -494,17 +478,17 @@ export const javascript_visitors_runes = {
494
478
if ( operator === '===' || operator === '!==' ) {
495
479
return b . call (
496
480
'$.strict_equals' ,
497
- /** @type {import('estree'). Expression } */ ( visit ( node . left ) ) ,
498
- /** @type {import('estree'). Expression } */ ( visit ( node . right ) ) ,
481
+ /** @type {Expression } */ ( visit ( node . left ) ) ,
482
+ /** @type {Expression } */ ( visit ( node . right ) ) ,
499
483
operator === '!==' && b . literal ( false )
500
484
) ;
501
485
}
502
486
503
487
if ( operator === '==' || operator === '!=' ) {
504
488
return b . call (
505
489
'$.equals' ,
506
- /** @type {import('estree'). Expression } */ ( visit ( node . left ) ) ,
507
- /** @type {import('estree'). Expression } */ ( visit ( node . right ) ) ,
490
+ /** @type {Expression } */ ( visit ( node . left ) ) ,
491
+ /** @type {Expression } */ ( visit ( node . right ) ) ,
508
492
operator === '!=' && b . literal ( false )
509
493
) ;
510
494
}
@@ -515,7 +499,7 @@ export const javascript_visitors_runes = {
515
499
} ;
516
500
517
501
/**
518
- * @param {import('estree'). Identifier | import('estree'). PrivateIdentifier | import('estree'). Literal } node
502
+ * @param {Identifier | PrivateIdentifier | Literal } node
519
503
*/
520
504
function get_name ( node ) {
521
505
if ( node . type === 'Literal' ) {
0 commit comments