@@ -208,15 +208,9 @@ namespace ts {
208
208
* @param node The node to visit.
209
209
*/
210
210
function visitorWorker ( node : Node ) : VisitResult < Node > {
211
- if ( node . transformFlags & TransformFlags . TypeScript ) {
212
- // This node is explicitly marked as TypeScript, so we should transform the node.
211
+ if ( node . transformFlags & TransformFlags . ContainsTypeScript ) {
213
212
return visitTypeScript ( node ) ;
214
213
}
215
- else if ( node . transformFlags & TransformFlags . ContainsTypeScript ) {
216
- // This node contains TypeScript, so we should visit its children.
217
- return visitEachChild ( node , visitor , context ) ;
218
- }
219
-
220
214
return node ;
221
215
}
222
216
@@ -296,15 +290,9 @@ namespace ts {
296
290
( < ImportEqualsDeclaration > node ) . moduleReference . kind === SyntaxKind . ExternalModuleReference ) ) {
297
291
// do not emit ES6 imports and exports since they are illegal inside a namespace
298
292
return undefined ;
299
- }
300
- else if ( node . transformFlags & TransformFlags . TypeScript || hasModifier ( node , ModifierFlags . Export ) ) {
301
- // This node is explicitly marked as TypeScript, or is exported at the namespace
302
- // level, so we should transform the node.
303
- return visitTypeScript ( node ) ;
304
293
}
305
- else if ( node . transformFlags & TransformFlags . ContainsTypeScript ) {
306
- // This node contains TypeScript, so we should visit its children.
307
- return visitEachChild ( node , visitor , context ) ;
294
+ else if ( node . transformFlags & TransformFlags . ContainsTypeScript || hasModifier ( node , ModifierFlags . Export ) ) {
295
+ return visitTypeScript ( node ) ;
308
296
}
309
297
310
298
return node ;
@@ -365,7 +353,7 @@ namespace ts {
365
353
* @param node The node to visit.
366
354
*/
367
355
function visitTypeScript ( node : Node ) : VisitResult < Node > {
368
- if ( hasModifier ( node , ModifierFlags . Ambient ) && isStatement ( node ) ) {
356
+ if ( isStatement ( node ) && hasModifier ( node , ModifierFlags . Ambient ) ) {
369
357
// TypeScript ambient declarations are elided, but some comments may be preserved.
370
358
// See the implementation of `getLeadingComments` in comments.ts for more details.
371
359
return createNotEmittedStatement ( node ) ;
@@ -443,7 +431,7 @@ namespace ts {
443
431
return createNotEmittedStatement ( node ) ;
444
432
445
433
case SyntaxKind . ClassDeclaration :
446
- // This is a class declaration with TypeScript syntax extensions.
434
+ // This may be a class declaration with TypeScript syntax extensions.
447
435
//
448
436
// TypeScript class syntax extensions include:
449
437
// - decorators
@@ -455,7 +443,7 @@ namespace ts {
455
443
return visitClassDeclaration ( < ClassDeclaration > node ) ;
456
444
457
445
case SyntaxKind . ClassExpression :
458
- // This is a class expression with TypeScript syntax extensions.
446
+ // This may be a class expression with TypeScript syntax extensions.
459
447
//
460
448
// TypeScript class syntax extensions include:
461
449
// - decorators
@@ -467,7 +455,7 @@ namespace ts {
467
455
return visitClassExpression ( < ClassExpression > node ) ;
468
456
469
457
case SyntaxKind . HeritageClause :
470
- // This is a heritage clause with TypeScript syntax extensions.
458
+ // This may be a heritage clause with TypeScript syntax extensions.
471
459
//
472
460
// TypeScript heritage clause extensions include:
473
461
// - `implements` clause
@@ -503,7 +491,7 @@ namespace ts {
503
491
return visitArrowFunction ( < ArrowFunction > node ) ;
504
492
505
493
case SyntaxKind . Parameter :
506
- // This is a parameter declaration with TypeScript syntax extensions.
494
+ // This may be a parameter declaration with TypeScript syntax extensions.
507
495
//
508
496
// TypeScript parameter declaration syntax extensions include:
509
497
// - decorators
@@ -556,7 +544,8 @@ namespace ts {
556
544
return visitImportEqualsDeclaration ( < ImportEqualsDeclaration > node ) ;
557
545
558
546
default :
559
- return Debug . failBadSyntaxKind ( node ) ;
547
+ // node contains some other TypeScript syntax
548
+ return visitEachChild ( node , visitor , context ) ;
560
549
}
561
550
}
562
551
@@ -607,18 +596,22 @@ namespace ts {
607
596
return facts ;
608
597
}
609
598
610
- /**
611
- * Transforms a class declaration with TypeScript syntax into compatible ES6.
612
- *
613
- * This function will only be called when one of the following conditions are met:
614
- * - The class has decorators.
615
- * - The class has property declarations with initializers.
616
- * - The class contains a constructor that contains parameters with accessibility modifiers.
617
- * - The class is an export in a TypeScript namespace.
618
- *
619
- * @param node The node to transform.
620
- */
599
+ function hasTypeScriptClassSyntax ( node : Node ) {
600
+ return ! ! ( node . transformFlags & TransformFlags . ContainsTypeScriptClassSyntax ) ;
601
+ }
602
+
603
+ function isClassLikeDeclarationWithTypeScriptSyntax ( node : ClassLikeDeclaration ) {
604
+ return some ( node . decorators )
605
+ || some ( node . typeParameters )
606
+ || some ( node . heritageClauses , hasTypeScriptClassSyntax )
607
+ || some ( node . members , hasTypeScriptClassSyntax ) ;
608
+ }
609
+
621
610
function visitClassDeclaration ( node : ClassDeclaration ) : VisitResult < Statement > {
611
+ if ( ! isClassLikeDeclarationWithTypeScriptSyntax ( node ) && ! ( currentNamespace && hasModifier ( node , ModifierFlags . Export ) ) ) {
612
+ return visitEachChild ( node , visitor , context ) ;
613
+ }
614
+
622
615
const savedPendingExpressions = pendingExpressions ;
623
616
pendingExpressions = undefined ;
624
617
@@ -890,16 +883,11 @@ namespace ts {
890
883
return statement ;
891
884
}
892
885
893
- /**
894
- * Transforms a class expression with TypeScript syntax into compatible ES6.
895
- *
896
- * This function will only be called when one of the following conditions are met:
897
- * - The class has property declarations with initializers.
898
- * - The class contains a constructor that contains parameters with accessibility modifiers.
899
- *
900
- * @param node The node to transform.
901
- */
902
886
function visitClassExpression ( node : ClassExpression ) : Expression {
887
+ if ( ! isClassLikeDeclarationWithTypeScriptSyntax ( node ) ) {
888
+ return visitEachChild ( node , visitor , context ) ;
889
+ }
890
+
903
891
const savedPendingExpressions = pendingExpressions ;
904
892
pendingExpressions = undefined ;
905
893
@@ -2237,18 +2225,11 @@ namespace ts {
2237
2225
* @param node The HeritageClause to transform.
2238
2226
*/
2239
2227
function visitHeritageClause ( node : HeritageClause ) : HeritageClause | undefined {
2240
- if ( node . token === SyntaxKind . ExtendsKeyword ) {
2241
- const types = visitNodes ( node . types , visitor , isExpressionWithTypeArguments , 0 , 1 ) ;
2242
- return setTextRange (
2243
- createHeritageClause (
2244
- SyntaxKind . ExtendsKeyword ,
2245
- types
2246
- ) ,
2247
- node
2248
- ) ;
2228
+ if ( node . token === SyntaxKind . ImplementsKeyword ) {
2229
+ // implements clauses are elided
2230
+ return undefined ;
2249
2231
}
2250
-
2251
- return undefined ;
2232
+ return visitEachChild ( node , visitor , context ) ;
2252
2233
}
2253
2234
2254
2235
/**
@@ -2299,16 +2280,6 @@ namespace ts {
2299
2280
) ;
2300
2281
}
2301
2282
2302
- /**
2303
- * Visits a method declaration of a class.
2304
- *
2305
- * This function will be called when one of the following conditions are met:
2306
- * - The node is an overload
2307
- * - The node is marked as abstract, public, private, protected, or readonly
2308
- * - The node has a computed property name
2309
- *
2310
- * @param node The method node.
2311
- */
2312
2283
function visitMethodDeclaration ( node : MethodDeclaration ) {
2313
2284
if ( ! shouldEmitFunctionLikeDeclaration ( node ) ) {
2314
2285
return undefined ;
@@ -2344,15 +2315,6 @@ namespace ts {
2344
2315
return ! ( nodeIsMissing ( node . body ) && hasModifier ( node , ModifierFlags . Abstract ) ) ;
2345
2316
}
2346
2317
2347
- /**
2348
- * Visits a get accessor declaration of a class.
2349
- *
2350
- * This function will be called when one of the following conditions are met:
2351
- * - The node is marked as abstract, public, private, or protected
2352
- * - The node has a computed property name
2353
- *
2354
- * @param node The get accessor node.
2355
- */
2356
2318
function visitGetAccessor ( node : GetAccessorDeclaration ) {
2357
2319
if ( ! shouldEmitAccessorDeclaration ( node ) ) {
2358
2320
return undefined ;
@@ -2375,15 +2337,6 @@ namespace ts {
2375
2337
return updated ;
2376
2338
}
2377
2339
2378
- /**
2379
- * Visits a set accessor declaration of a class.
2380
- *
2381
- * This function will be called when one of the following conditions are met:
2382
- * - The node is marked as abstract, public, private, or protected
2383
- * - The node has a computed property name
2384
- *
2385
- * @param node The set accessor node.
2386
- */
2387
2340
function visitSetAccessor ( node : SetAccessorDeclaration ) {
2388
2341
if ( ! shouldEmitAccessorDeclaration ( node ) ) {
2389
2342
return undefined ;
@@ -2405,16 +2358,6 @@ namespace ts {
2405
2358
return updated ;
2406
2359
}
2407
2360
2408
- /**
2409
- * Visits a function declaration.
2410
- *
2411
- * This function will be called when one of the following conditions are met:
2412
- * - The node is an overload
2413
- * - The node is exported from a TypeScript namespace
2414
- * - The node has decorators
2415
- *
2416
- * @param node The function node.
2417
- */
2418
2361
function visitFunctionDeclaration ( node : FunctionDeclaration ) : VisitResult < Statement > {
2419
2362
if ( ! shouldEmitFunctionLikeDeclaration ( node ) ) {
2420
2363
return createNotEmittedStatement ( node ) ;
@@ -2438,14 +2381,6 @@ namespace ts {
2438
2381
return updated ;
2439
2382
}
2440
2383
2441
- /**
2442
- * Visits a function expression node.
2443
- *
2444
- * This function will be called when one of the following conditions are met:
2445
- * - The node has type annotations
2446
- *
2447
- * @param node The function expression node.
2448
- */
2449
2384
function visitFunctionExpression ( node : FunctionExpression ) : Expression {
2450
2385
if ( ! shouldEmitFunctionLikeDeclaration ( node ) ) {
2451
2386
return createOmittedExpression ( ) ;
@@ -2463,11 +2398,6 @@ namespace ts {
2463
2398
return updated ;
2464
2399
}
2465
2400
2466
- /**
2467
- * @remarks
2468
- * This function will be called when one of the following conditions are met:
2469
- * - The node has type annotations
2470
- */
2471
2401
function visitArrowFunction ( node : ArrowFunction ) {
2472
2402
const updated = updateArrowFunction (
2473
2403
node ,
@@ -2481,22 +2411,12 @@ namespace ts {
2481
2411
return updated ;
2482
2412
}
2483
2413
2484
- /**
2485
- * Visits a parameter declaration node.
2486
- *
2487
- * This function will be called when one of the following conditions are met:
2488
- * - The node has an accessibility modifier.
2489
- * - The node has a questionToken.
2490
- * - The node's kind is ThisKeyword.
2491
- *
2492
- * @param node The parameter declaration node.
2493
- */
2494
2414
function visitParameter ( node : ParameterDeclaration ) {
2495
2415
if ( parameterIsThisKeyword ( node ) ) {
2496
2416
return undefined ;
2497
2417
}
2498
-
2499
- const parameter = createParameter (
2418
+ const updated = updateParameter (
2419
+ node ,
2500
2420
/*decorators*/ undefined ,
2501
2421
/*modifiers*/ undefined ,
2502
2422
node . dotDotDotToken ,
@@ -2505,24 +2425,17 @@ namespace ts {
2505
2425
/*type*/ undefined ,
2506
2426
visitNode ( node . initializer , visitor , isExpression )
2507
2427
) ;
2508
-
2509
- // While we emit the source map for the node after skipping decorators and modifiers,
2510
- // we need to emit the comments for the original range.
2511
- setOriginalNode ( parameter , node ) ;
2512
- setTextRange ( parameter , moveRangePastModifiers ( node ) ) ;
2513
- setCommentRange ( parameter , node ) ;
2514
- setSourceMapRange ( parameter , moveRangePastModifiers ( node ) ) ;
2515
- setEmitFlags ( parameter . name , EmitFlags . NoTrailingSourceMap ) ;
2516
-
2517
- return parameter ;
2428
+ if ( updated !== node ) {
2429
+ // While we emit the source map for the node after skipping decorators and modifiers,
2430
+ // we need to emit the comments for the original range.
2431
+ setCommentRange ( updated , node ) ;
2432
+ setTextRange ( updated , moveRangePastModifiers ( node ) ) ;
2433
+ setSourceMapRange ( updated , moveRangePastModifiers ( node ) ) ;
2434
+ setEmitFlags ( updated . name , EmitFlags . NoTrailingSourceMap ) ;
2435
+ }
2436
+ return updated ;
2518
2437
}
2519
2438
2520
- /**
2521
- * Visits a variable statement in a namespace.
2522
- *
2523
- * This function will be called when one of the following conditions are met:
2524
- * - The node is exported from a TypeScript namespace.
2525
- */
2526
2439
function visitVariableStatement ( node : VariableStatement ) : Statement | undefined {
2527
2440
if ( isExportOfNamespace ( node ) ) {
2528
2441
const variables = getInitializedVariables ( node . declarationList ) ;
@@ -2576,12 +2489,6 @@ namespace ts {
2576
2489
visitNode ( node . initializer , visitor , isExpression ) ) ;
2577
2490
}
2578
2491
2579
- /**
2580
- * Visits a parenthesized expression that contains either a type assertion or an `as`
2581
- * expression.
2582
- *
2583
- * @param node The parenthesized expression node.
2584
- */
2585
2492
function visitParenthesizedExpression ( node : ParenthesizedExpression ) : Expression {
2586
2493
const innerExpression = skipOuterExpressions ( node . expression , ~ OuterExpressionKinds . Assertions ) ;
2587
2494
if ( isAssertionExpression ( innerExpression ) ) {
0 commit comments