18
18
CLASS = 10; // only used in assertValidReturnValue
19
19
*/
20
20
21
- function createMetadataMethodsForProperty ( metadataMap , kind , property ) {
21
+ function createMetadataMethodsForProperty (
22
+ metadataMap ,
23
+ kind ,
24
+ property ,
25
+ decoratorFinishedRef
26
+ ) {
22
27
return {
23
28
getMetadata : function ( key ) {
24
- if ( typeof key !== "symbol" ) {
25
- throw new TypeError ( "Metadata keys must be symbols, received: " + key ) ;
26
- }
29
+ assertNotFinished ( decoratorFinishedRef , "getMetadata" ) ;
30
+ assertMetadataKey ( key ) ;
27
31
28
32
var metadataForKey = metadataMap [ key ] ;
29
33
@@ -44,9 +48,8 @@ function createMetadataMethodsForProperty(metadataMap, kind, property) {
44
48
}
45
49
} ,
46
50
setMetadata : function ( key , value ) {
47
- if ( typeof key !== "symbol" ) {
48
- throw new TypeError ( "Metadata keys must be symbols, received: " + key ) ;
49
- }
51
+ assertNotFinished ( decoratorFinishedRef , "setMetadata" ) ;
52
+ assertMetadataKey ( key ) ;
50
53
51
54
var metadataForKey = metadataMap [ key ] ;
52
55
@@ -120,21 +123,24 @@ function convertMetadataMapToFinal(obj, metadataMap) {
120
123
obj [ Symbol . metadata || Symbol . for ( "Symbol.metadata" ) ] = metadataMap ;
121
124
}
122
125
123
- function createAddInitializerMethod ( initializers ) {
126
+ function createAddInitializerMethod ( initializers , decoratorFinishedRef ) {
124
127
return function addInitializer ( initializer ) {
128
+ assertNotFinished ( decoratorFinishedRef , "addInitializer" ) ;
125
129
assertCallable ( initializer , "An initializer" ) ;
126
130
initializers . push ( initializer ) ;
127
131
} ;
128
132
}
129
133
130
- function memberDecCtx (
134
+ function memberDec (
135
+ dec ,
131
136
name ,
132
137
desc ,
133
138
metadataMap ,
134
139
initializers ,
135
140
kind ,
136
141
isStatic ,
137
- isPrivate
142
+ isPrivate ,
143
+ value
138
144
) {
139
145
var kindStr ;
140
146
@@ -162,8 +168,13 @@ function memberDecCtx(
162
168
isPrivate : isPrivate ,
163
169
} ;
164
170
171
+ var decoratorFinishedRef = { v : false } ;
172
+
165
173
if ( kind !== 0 /* FIELD */ ) {
166
- ctx . addInitializer = createAddInitializerMethod ( initializers ) ;
174
+ ctx . addInitializer = createAddInitializerMethod (
175
+ initializers ,
176
+ decoratorFinishedRef
177
+ ) ;
167
178
}
168
179
169
180
var metadataKind , metadataName ;
@@ -202,10 +213,36 @@ function memberDecCtx(
202
213
metadataName = name ;
203
214
}
204
215
205
- return Object . assign (
206
- ctx ,
207
- createMetadataMethodsForProperty ( metadataMap , metadataKind , metadataName )
208
- ) ;
216
+ try {
217
+ return dec (
218
+ value ,
219
+ Object . assign (
220
+ ctx ,
221
+ createMetadataMethodsForProperty (
222
+ metadataMap ,
223
+ metadataKind ,
224
+ metadataName ,
225
+ decoratorFinishedRef
226
+ )
227
+ )
228
+ ) ;
229
+ } finally {
230
+ decoratorFinishedRef . v = true ;
231
+ }
232
+ }
233
+
234
+ function assertNotFinished ( decoratorFinishedRef , fnName ) {
235
+ if ( decoratorFinishedRef . v ) {
236
+ throw new Error (
237
+ "attempted to call " + fnName + " after decoration was finished"
238
+ ) ;
239
+ }
240
+ }
241
+
242
+ function assertMetadataKey ( key ) {
243
+ if ( typeof key !== "symbol" ) {
244
+ throw new TypeError ( "Metadata keys must be symbols, received: " + key ) ;
245
+ }
209
246
}
210
247
211
248
function assertCallable ( fn , hint ) {
@@ -220,7 +257,7 @@ function assertValidReturnValue(kind, value) {
220
257
if ( kind === 1 /* ACCESSOR */ ) {
221
258
if ( type !== "object" || value === null ) {
222
259
throw new TypeError (
223
- "accessor decorators must return an object with get, set, or initializer properties or void 0"
260
+ "accessor decorators must return an object with get, set, or init properties or void 0"
224
261
) ;
225
262
}
226
263
if ( value . get !== undefined ) {
@@ -229,8 +266,11 @@ function assertValidReturnValue(kind, value) {
229
266
if ( value . set !== undefined ) {
230
267
assertCallable ( value . set , "accessor.set" ) ;
231
268
}
232
- if ( value . initialize !== undefined ) {
233
- assertCallable ( value . initialize , "accessor.initialize" ) ;
269
+ if ( value . init !== undefined ) {
270
+ assertCallable ( value . init , "accessor.init" ) ;
271
+ }
272
+ if ( value . initializer !== undefined ) {
273
+ assertCallable ( value . initializer , "accessor.initializer" ) ;
234
274
}
235
275
} else if ( type !== "function" ) {
236
276
var hint ;
@@ -245,6 +285,18 @@ function assertValidReturnValue(kind, value) {
245
285
}
246
286
}
247
287
288
+ function getInit ( desc ) {
289
+ var initializer ;
290
+ if (
291
+ ( initializer = desc . init ) == null &&
292
+ ( initializer = desc . initializer ) &&
293
+ typeof console !== "undefined"
294
+ ) {
295
+ console . warn ( ".initializer has been renamed to .init as of March 2022" ) ;
296
+ }
297
+ return initializer ;
298
+ }
299
+
248
300
function applyMemberDec (
249
301
ret ,
250
302
base ,
@@ -296,29 +348,28 @@ function applyMemberDec(
296
348
value = desc . set ;
297
349
}
298
350
299
- var ctx = memberDecCtx (
300
- name ,
301
- desc ,
302
- metadataMap ,
303
- initializers ,
304
- kind ,
305
- isStatic ,
306
- isPrivate
307
- ) ;
308
-
309
351
var newValue , get , set ;
310
352
311
353
if ( typeof decs === "function" ) {
312
- newValue = decs ( value , ctx ) ;
354
+ newValue = memberDec (
355
+ decs ,
356
+ name ,
357
+ desc ,
358
+ metadataMap ,
359
+ initializers ,
360
+ kind ,
361
+ isStatic ,
362
+ isPrivate ,
363
+ value
364
+ ) ;
313
365
314
366
if ( newValue !== void 0 ) {
315
367
assertValidReturnValue ( kind , newValue ) ;
316
368
317
369
if ( kind === 0 /* FIELD */ ) {
318
370
initializer = newValue ;
319
371
} else if ( kind === 1 /* ACCESSOR */ ) {
320
- initializer = newValue . initialize ;
321
-
372
+ initializer = getInit ( newValue ) ;
322
373
get = newValue . get || value . get ;
323
374
set = newValue . set || value . set ;
324
375
@@ -331,7 +382,17 @@ function applyMemberDec(
331
382
for ( var i = decs . length - 1 ; i >= 0 ; i -- ) {
332
383
var dec = decs [ i ] ;
333
384
334
- newValue = dec ( value , ctx ) ;
385
+ newValue = memberDec (
386
+ dec ,
387
+ name ,
388
+ desc ,
389
+ metadataMap ,
390
+ initializers ,
391
+ kind ,
392
+ isStatic ,
393
+ isPrivate ,
394
+ value
395
+ ) ;
335
396
336
397
if ( newValue !== void 0 ) {
337
398
assertValidReturnValue ( kind , newValue ) ;
@@ -340,8 +401,7 @@ function applyMemberDec(
340
401
if ( kind === 0 /* FIELD */ ) {
341
402
newInit = newValue ;
342
403
} else if ( kind === 1 /* ACCESSOR */ ) {
343
- newInit = newValue . initialize ;
344
-
404
+ newInit = getInit ( newValue ) ;
345
405
get = newValue . get || value . get ;
346
406
set = newValue . set || value . set ;
347
407
@@ -514,59 +574,57 @@ function applyMemberDecs(
514
574
515
575
function pushInitializers ( ret , initializers ) {
516
576
if ( initializers ) {
517
- if ( initializers . length > 0 ) {
518
- // Slice the array, which means that `addInitializer` can no longer add
519
- // additional initializers to the array
520
- initializers = initializers . slice ( ) ;
521
-
522
- ret . push ( function ( instance ) {
523
- for ( var i = 0 ; i < initializers . length ; i ++ ) {
524
- initializers [ i ] . call ( instance ) ;
525
- }
526
- return instance ;
527
- } ) ;
528
- } else {
529
- ret . push ( function ( instance ) {
530
- return instance ;
531
- } ) ;
532
- }
577
+ ret . push ( function ( instance ) {
578
+ for ( var i = 0 ; i < initializers . length ; i ++ ) {
579
+ initializers [ i ] . call ( instance ) ;
580
+ }
581
+ return instance ;
582
+ } ) ;
533
583
}
534
584
}
535
585
536
586
function applyClassDecs ( ret , targetClass , metadataMap , classDecs ) {
537
587
if ( classDecs . length > 0 ) {
538
588
var initializers = [ ] ;
539
589
var newClass = targetClass ;
540
-
541
590
var name = targetClass . name ;
542
- var ctx = Object . assign (
543
- {
544
- kind : "class" ,
545
- name : name ,
546
- addInitializer : createAddInitializerMethod ( initializers ) ,
547
- } ,
548
- createMetadataMethodsForProperty ( metadataMap , 0 /* CONSTRUCTOR */ , name )
549
- ) ;
550
591
551
592
for ( var i = classDecs . length - 1 ; i >= 0 ; i -- ) {
552
- var nextNewClass = classDecs [ i ] ( newClass , ctx ) ;
593
+ var decoratorFinishedRef = { v : false } ;
594
+
595
+ try {
596
+ var ctx = Object . assign (
597
+ {
598
+ kind : "class" ,
599
+ name : name ,
600
+ addInitializer : createAddInitializerMethod (
601
+ initializers ,
602
+ decoratorFinishedRef
603
+ ) ,
604
+ } ,
605
+ createMetadataMethodsForProperty (
606
+ metadataMap ,
607
+ 0 /* CONSTRUCTOR */ ,
608
+ name ,
609
+ decoratorFinishedRef
610
+ )
611
+ ) ;
612
+ var nextNewClass = classDecs [ i ] ( newClass , ctx ) ;
613
+ } finally {
614
+ decoratorFinishedRef . v = true ;
615
+ }
616
+
553
617
if ( nextNewClass !== undefined ) {
554
618
assertValidReturnValue ( 10 /* CLASS */ , nextNewClass ) ;
555
619
newClass = nextNewClass ;
556
620
}
557
621
}
558
622
559
- ret . push ( newClass ) ;
560
-
561
- if ( initializers . length > 0 ) {
562
- ret . push ( function ( ) {
563
- for ( var i = 0 ; i < initializers . length ; i ++ ) {
564
- initializers [ i ] . call ( newClass ) ;
565
- }
566
- } ) ;
567
- } else {
568
- ret . push ( function ( ) { } ) ;
569
- }
623
+ ret . push ( newClass , function ( ) {
624
+ for ( var i = 0 ; i < initializers . length ; i ++ ) {
625
+ initializers [ i ] . call ( newClass ) ;
626
+ }
627
+ } ) ;
570
628
}
571
629
}
572
630
0 commit comments