Skip to content

Commit ad4cef0

Browse files
committed
Removes 'TypeScript' transform flag
1 parent 5576c3e commit ad4cef0

File tree

7 files changed

+74
-174
lines changed

7 files changed

+74
-174
lines changed

src/compiler/transformers/ts.ts

Lines changed: 44 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,9 @@ namespace ts {
208208
* @param node The node to visit.
209209
*/
210210
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) {
213212
return visitTypeScript(node);
214213
}
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-
220214
return node;
221215
}
222216

@@ -296,15 +290,9 @@ namespace ts {
296290
(<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference)) {
297291
// do not emit ES6 imports and exports since they are illegal inside a namespace
298292
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);
304293
}
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);
308296
}
309297

310298
return node;
@@ -365,7 +353,7 @@ namespace ts {
365353
* @param node The node to visit.
366354
*/
367355
function visitTypeScript(node: Node): VisitResult<Node> {
368-
if (hasModifier(node, ModifierFlags.Ambient) && isStatement(node)) {
356+
if (isStatement(node) && hasModifier(node, ModifierFlags.Ambient)) {
369357
// TypeScript ambient declarations are elided, but some comments may be preserved.
370358
// See the implementation of `getLeadingComments` in comments.ts for more details.
371359
return createNotEmittedStatement(node);
@@ -443,7 +431,7 @@ namespace ts {
443431
return createNotEmittedStatement(node);
444432

445433
case SyntaxKind.ClassDeclaration:
446-
// This is a class declaration with TypeScript syntax extensions.
434+
// This may be a class declaration with TypeScript syntax extensions.
447435
//
448436
// TypeScript class syntax extensions include:
449437
// - decorators
@@ -455,7 +443,7 @@ namespace ts {
455443
return visitClassDeclaration(<ClassDeclaration>node);
456444

457445
case SyntaxKind.ClassExpression:
458-
// This is a class expression with TypeScript syntax extensions.
446+
// This may be a class expression with TypeScript syntax extensions.
459447
//
460448
// TypeScript class syntax extensions include:
461449
// - decorators
@@ -467,7 +455,7 @@ namespace ts {
467455
return visitClassExpression(<ClassExpression>node);
468456

469457
case SyntaxKind.HeritageClause:
470-
// This is a heritage clause with TypeScript syntax extensions.
458+
// This may be a heritage clause with TypeScript syntax extensions.
471459
//
472460
// TypeScript heritage clause extensions include:
473461
// - `implements` clause
@@ -503,7 +491,7 @@ namespace ts {
503491
return visitArrowFunction(<ArrowFunction>node);
504492

505493
case SyntaxKind.Parameter:
506-
// This is a parameter declaration with TypeScript syntax extensions.
494+
// This may be a parameter declaration with TypeScript syntax extensions.
507495
//
508496
// TypeScript parameter declaration syntax extensions include:
509497
// - decorators
@@ -556,7 +544,8 @@ namespace ts {
556544
return visitImportEqualsDeclaration(<ImportEqualsDeclaration>node);
557545

558546
default:
559-
return Debug.failBadSyntaxKind(node);
547+
// node contains some other TypeScript syntax
548+
return visitEachChild(node, visitor, context);
560549
}
561550
}
562551

@@ -607,18 +596,22 @@ namespace ts {
607596
return facts;
608597
}
609598

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+
621610
function visitClassDeclaration(node: ClassDeclaration): VisitResult<Statement> {
611+
if (!isClassLikeDeclarationWithTypeScriptSyntax(node) && !(currentNamespace && hasModifier(node, ModifierFlags.Export))) {
612+
return visitEachChild(node, visitor, context);
613+
}
614+
622615
const savedPendingExpressions = pendingExpressions;
623616
pendingExpressions = undefined;
624617

@@ -890,16 +883,11 @@ namespace ts {
890883
return statement;
891884
}
892885

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-
*/
902886
function visitClassExpression(node: ClassExpression): Expression {
887+
if (!isClassLikeDeclarationWithTypeScriptSyntax(node)) {
888+
return visitEachChild(node, visitor, context);
889+
}
890+
903891
const savedPendingExpressions = pendingExpressions;
904892
pendingExpressions = undefined;
905893

@@ -2237,18 +2225,11 @@ namespace ts {
22372225
* @param node The HeritageClause to transform.
22382226
*/
22392227
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;
22492231
}
2250-
2251-
return undefined;
2232+
return visitEachChild(node, visitor, context);
22522233
}
22532234

22542235
/**
@@ -2299,16 +2280,6 @@ namespace ts {
22992280
);
23002281
}
23012282

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-
*/
23122283
function visitMethodDeclaration(node: MethodDeclaration) {
23132284
if (!shouldEmitFunctionLikeDeclaration(node)) {
23142285
return undefined;
@@ -2344,15 +2315,6 @@ namespace ts {
23442315
return !(nodeIsMissing(node.body) && hasModifier(node, ModifierFlags.Abstract));
23452316
}
23462317

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-
*/
23562318
function visitGetAccessor(node: GetAccessorDeclaration) {
23572319
if (!shouldEmitAccessorDeclaration(node)) {
23582320
return undefined;
@@ -2375,15 +2337,6 @@ namespace ts {
23752337
return updated;
23762338
}
23772339

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-
*/
23872340
function visitSetAccessor(node: SetAccessorDeclaration) {
23882341
if (!shouldEmitAccessorDeclaration(node)) {
23892342
return undefined;
@@ -2405,16 +2358,6 @@ namespace ts {
24052358
return updated;
24062359
}
24072360

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-
*/
24182361
function visitFunctionDeclaration(node: FunctionDeclaration): VisitResult<Statement> {
24192362
if (!shouldEmitFunctionLikeDeclaration(node)) {
24202363
return createNotEmittedStatement(node);
@@ -2438,14 +2381,6 @@ namespace ts {
24382381
return updated;
24392382
}
24402383

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-
*/
24492384
function visitFunctionExpression(node: FunctionExpression): Expression {
24502385
if (!shouldEmitFunctionLikeDeclaration(node)) {
24512386
return createOmittedExpression();
@@ -2463,11 +2398,6 @@ namespace ts {
24632398
return updated;
24642399
}
24652400

2466-
/**
2467-
* @remarks
2468-
* This function will be called when one of the following conditions are met:
2469-
* - The node has type annotations
2470-
*/
24712401
function visitArrowFunction(node: ArrowFunction) {
24722402
const updated = updateArrowFunction(
24732403
node,
@@ -2481,22 +2411,12 @@ namespace ts {
24812411
return updated;
24822412
}
24832413

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-
*/
24942414
function visitParameter(node: ParameterDeclaration) {
24952415
if (parameterIsThisKeyword(node)) {
24962416
return undefined;
24972417
}
2498-
2499-
const parameter = createParameter(
2418+
const updated = updateParameter(
2419+
node,
25002420
/*decorators*/ undefined,
25012421
/*modifiers*/ undefined,
25022422
node.dotDotDotToken,
@@ -2505,24 +2425,17 @@ namespace ts {
25052425
/*type*/ undefined,
25062426
visitNode(node.initializer, visitor, isExpression)
25072427
);
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;
25182437
}
25192438

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-
*/
25262439
function visitVariableStatement(node: VariableStatement): Statement | undefined {
25272440
if (isExportOfNamespace(node)) {
25282441
const variables = getInitializedVariables(node.declarationList);
@@ -2576,12 +2489,6 @@ namespace ts {
25762489
visitNode(node.initializer, visitor, isExpression));
25772490
}
25782491

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-
*/
25852492
function visitParenthesizedExpression(node: ParenthesizedExpression): Expression {
25862493
const innerExpression = skipOuterExpressions(node.expression, ~OuterExpressionKinds.Assertions);
25872494
if (isAssertionExpression(innerExpression)) {

0 commit comments

Comments
 (0)