From ad4cef01a3961070bacc8880e21ba45ccd6244ba Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 25 Feb 2019 13:26:44 -0800 Subject: [PATCH] Removes 'TypeScript' transform flag --- src/compiler/transformers/ts.ts | 181 +++++------------- src/compiler/types.ts | 57 +++--- .../arrowFunctionWithObjectLiteralBody5.js | 2 +- .../arrowFunctionWithObjectLiteralBody6.js | 2 +- .../parseErrorIncorrectReturnToken.js | 4 +- ...erErrantEqualsGreaterThanAfterFunction2.js | 1 - .../reference/thisTypeInFunctionsNegative.js | 1 - 7 files changed, 74 insertions(+), 174 deletions(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 473f3ed0d14..702deeb9d81 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -208,15 +208,9 @@ namespace ts { * @param node The node to visit. */ function visitorWorker(node: Node): VisitResult { - if (node.transformFlags & TransformFlags.TypeScript) { - // This node is explicitly marked as TypeScript, so we should transform the node. + if (node.transformFlags & TransformFlags.ContainsTypeScript) { return visitTypeScript(node); } - else if (node.transformFlags & TransformFlags.ContainsTypeScript) { - // This node contains TypeScript, so we should visit its children. - return visitEachChild(node, visitor, context); - } - return node; } @@ -296,15 +290,9 @@ namespace ts { (node).moduleReference.kind === SyntaxKind.ExternalModuleReference)) { // do not emit ES6 imports and exports since they are illegal inside a namespace return undefined; - } - else if (node.transformFlags & TransformFlags.TypeScript || hasModifier(node, ModifierFlags.Export)) { - // This node is explicitly marked as TypeScript, or is exported at the namespace - // level, so we should transform the node. - return visitTypeScript(node); } - else if (node.transformFlags & TransformFlags.ContainsTypeScript) { - // This node contains TypeScript, so we should visit its children. - return visitEachChild(node, visitor, context); + else if (node.transformFlags & TransformFlags.ContainsTypeScript || hasModifier(node, ModifierFlags.Export)) { + return visitTypeScript(node); } return node; @@ -365,7 +353,7 @@ namespace ts { * @param node The node to visit. */ function visitTypeScript(node: Node): VisitResult { - if (hasModifier(node, ModifierFlags.Ambient) && isStatement(node)) { + if (isStatement(node) && hasModifier(node, ModifierFlags.Ambient)) { // TypeScript ambient declarations are elided, but some comments may be preserved. // See the implementation of `getLeadingComments` in comments.ts for more details. return createNotEmittedStatement(node); @@ -443,7 +431,7 @@ namespace ts { return createNotEmittedStatement(node); case SyntaxKind.ClassDeclaration: - // This is a class declaration with TypeScript syntax extensions. + // This may be a class declaration with TypeScript syntax extensions. // // TypeScript class syntax extensions include: // - decorators @@ -455,7 +443,7 @@ namespace ts { return visitClassDeclaration(node); case SyntaxKind.ClassExpression: - // This is a class expression with TypeScript syntax extensions. + // This may be a class expression with TypeScript syntax extensions. // // TypeScript class syntax extensions include: // - decorators @@ -467,7 +455,7 @@ namespace ts { return visitClassExpression(node); case SyntaxKind.HeritageClause: - // This is a heritage clause with TypeScript syntax extensions. + // This may be a heritage clause with TypeScript syntax extensions. // // TypeScript heritage clause extensions include: // - `implements` clause @@ -503,7 +491,7 @@ namespace ts { return visitArrowFunction(node); case SyntaxKind.Parameter: - // This is a parameter declaration with TypeScript syntax extensions. + // This may be a parameter declaration with TypeScript syntax extensions. // // TypeScript parameter declaration syntax extensions include: // - decorators @@ -556,7 +544,8 @@ namespace ts { return visitImportEqualsDeclaration(node); default: - return Debug.failBadSyntaxKind(node); + // node contains some other TypeScript syntax + return visitEachChild(node, visitor, context); } } @@ -607,18 +596,22 @@ namespace ts { return facts; } - /** - * Transforms a class declaration with TypeScript syntax into compatible ES6. - * - * This function will only be called when one of the following conditions are met: - * - The class has decorators. - * - The class has property declarations with initializers. - * - The class contains a constructor that contains parameters with accessibility modifiers. - * - The class is an export in a TypeScript namespace. - * - * @param node The node to transform. - */ + function hasTypeScriptClassSyntax(node: Node) { + return !!(node.transformFlags & TransformFlags.ContainsTypeScriptClassSyntax); + } + + function isClassLikeDeclarationWithTypeScriptSyntax(node: ClassLikeDeclaration) { + return some(node.decorators) + || some(node.typeParameters) + || some(node.heritageClauses, hasTypeScriptClassSyntax) + || some(node.members, hasTypeScriptClassSyntax); + } + function visitClassDeclaration(node: ClassDeclaration): VisitResult { + if (!isClassLikeDeclarationWithTypeScriptSyntax(node) && !(currentNamespace && hasModifier(node, ModifierFlags.Export))) { + return visitEachChild(node, visitor, context); + } + const savedPendingExpressions = pendingExpressions; pendingExpressions = undefined; @@ -890,16 +883,11 @@ namespace ts { return statement; } - /** - * Transforms a class expression with TypeScript syntax into compatible ES6. - * - * This function will only be called when one of the following conditions are met: - * - The class has property declarations with initializers. - * - The class contains a constructor that contains parameters with accessibility modifiers. - * - * @param node The node to transform. - */ function visitClassExpression(node: ClassExpression): Expression { + if (!isClassLikeDeclarationWithTypeScriptSyntax(node)) { + return visitEachChild(node, visitor, context); + } + const savedPendingExpressions = pendingExpressions; pendingExpressions = undefined; @@ -2237,18 +2225,11 @@ namespace ts { * @param node The HeritageClause to transform. */ function visitHeritageClause(node: HeritageClause): HeritageClause | undefined { - if (node.token === SyntaxKind.ExtendsKeyword) { - const types = visitNodes(node.types, visitor, isExpressionWithTypeArguments, 0, 1); - return setTextRange( - createHeritageClause( - SyntaxKind.ExtendsKeyword, - types - ), - node - ); + if (node.token === SyntaxKind.ImplementsKeyword) { + // implements clauses are elided + return undefined; } - - return undefined; + return visitEachChild(node, visitor, context); } /** @@ -2299,16 +2280,6 @@ namespace ts { ); } - /** - * Visits a method declaration of a class. - * - * This function will be called when one of the following conditions are met: - * - The node is an overload - * - The node is marked as abstract, public, private, protected, or readonly - * - The node has a computed property name - * - * @param node The method node. - */ function visitMethodDeclaration(node: MethodDeclaration) { if (!shouldEmitFunctionLikeDeclaration(node)) { return undefined; @@ -2344,15 +2315,6 @@ namespace ts { return !(nodeIsMissing(node.body) && hasModifier(node, ModifierFlags.Abstract)); } - /** - * Visits a get accessor declaration of a class. - * - * This function will be called when one of the following conditions are met: - * - The node is marked as abstract, public, private, or protected - * - The node has a computed property name - * - * @param node The get accessor node. - */ function visitGetAccessor(node: GetAccessorDeclaration) { if (!shouldEmitAccessorDeclaration(node)) { return undefined; @@ -2375,15 +2337,6 @@ namespace ts { return updated; } - /** - * Visits a set accessor declaration of a class. - * - * This function will be called when one of the following conditions are met: - * - The node is marked as abstract, public, private, or protected - * - The node has a computed property name - * - * @param node The set accessor node. - */ function visitSetAccessor(node: SetAccessorDeclaration) { if (!shouldEmitAccessorDeclaration(node)) { return undefined; @@ -2405,16 +2358,6 @@ namespace ts { return updated; } - /** - * Visits a function declaration. - * - * This function will be called when one of the following conditions are met: - * - The node is an overload - * - The node is exported from a TypeScript namespace - * - The node has decorators - * - * @param node The function node. - */ function visitFunctionDeclaration(node: FunctionDeclaration): VisitResult { if (!shouldEmitFunctionLikeDeclaration(node)) { return createNotEmittedStatement(node); @@ -2438,14 +2381,6 @@ namespace ts { return updated; } - /** - * Visits a function expression node. - * - * This function will be called when one of the following conditions are met: - * - The node has type annotations - * - * @param node The function expression node. - */ function visitFunctionExpression(node: FunctionExpression): Expression { if (!shouldEmitFunctionLikeDeclaration(node)) { return createOmittedExpression(); @@ -2463,11 +2398,6 @@ namespace ts { return updated; } - /** - * @remarks - * This function will be called when one of the following conditions are met: - * - The node has type annotations - */ function visitArrowFunction(node: ArrowFunction) { const updated = updateArrowFunction( node, @@ -2481,22 +2411,12 @@ namespace ts { return updated; } - /** - * Visits a parameter declaration node. - * - * This function will be called when one of the following conditions are met: - * - The node has an accessibility modifier. - * - The node has a questionToken. - * - The node's kind is ThisKeyword. - * - * @param node The parameter declaration node. - */ function visitParameter(node: ParameterDeclaration) { if (parameterIsThisKeyword(node)) { return undefined; } - - const parameter = createParameter( + const updated = updateParameter( + node, /*decorators*/ undefined, /*modifiers*/ undefined, node.dotDotDotToken, @@ -2505,24 +2425,17 @@ namespace ts { /*type*/ undefined, visitNode(node.initializer, visitor, isExpression) ); - - // While we emit the source map for the node after skipping decorators and modifiers, - // we need to emit the comments for the original range. - setOriginalNode(parameter, node); - setTextRange(parameter, moveRangePastModifiers(node)); - setCommentRange(parameter, node); - setSourceMapRange(parameter, moveRangePastModifiers(node)); - setEmitFlags(parameter.name, EmitFlags.NoTrailingSourceMap); - - return parameter; + if (updated !== node) { + // While we emit the source map for the node after skipping decorators and modifiers, + // we need to emit the comments for the original range. + setCommentRange(updated, node); + setTextRange(updated, moveRangePastModifiers(node)); + setSourceMapRange(updated, moveRangePastModifiers(node)); + setEmitFlags(updated.name, EmitFlags.NoTrailingSourceMap); + } + return updated; } - /** - * Visits a variable statement in a namespace. - * - * This function will be called when one of the following conditions are met: - * - The node is exported from a TypeScript namespace. - */ function visitVariableStatement(node: VariableStatement): Statement | undefined { if (isExportOfNamespace(node)) { const variables = getInitializedVariables(node.declarationList); @@ -2576,12 +2489,6 @@ namespace ts { visitNode(node.initializer, visitor, isExpression)); } - /** - * Visits a parenthesized expression that contains either a type assertion or an `as` - * expression. - * - * @param node The parenthesized expression node. - */ function visitParenthesizedExpression(node: ParenthesizedExpression): Expression { const innerExpression = skipOuterExpressions(node.expression, ~OuterExpressionKinds.Assertions); if (isAssertionExpression(innerExpression)) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e67a40f2c29..042eefabf50 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5041,37 +5041,34 @@ namespace ts { // Facts // - Flags used to indicate that a node or subtree contains syntax that requires transformation. - TypeScript = 1 << 0, - ContainsTypeScript = 1 << 1, - ContainsJsx = 1 << 2, - ContainsESNext = 1 << 3, - ContainsES2018 = 1 << 4, - ContainsES2017 = 1 << 5, - ContainsES2016 = 1 << 6, - ContainsES2015 = 1 << 7, - Generator = 1 << 8, - ContainsGenerator = 1 << 9, - DestructuringAssignment = 1 << 10, - ContainsDestructuringAssignment = 1 << 11, + ContainsTypeScript = 1 << 0, + ContainsJsx = 1 << 1, + ContainsESNext = 1 << 2, + ContainsES2018 = 1 << 3, + ContainsES2017 = 1 << 4, + ContainsES2016 = 1 << 5, + ContainsES2015 = 1 << 6, + Generator = 1 << 7, + ContainsGenerator = 1 << 8, + DestructuringAssignment = 1 << 9, + ContainsDestructuringAssignment = 1 << 10, // Markers // - Flags used to indicate that a subtree contains a specific transformation. - ContainsTypeScriptClassSyntax = 1 << 12, // Decorators, Property Initializers, Parameter Property Initializers - - ContainsLexicalThis = 1 << 13, - ContainsCapturedLexicalThis = 1 << 14, - - ContainsLexicalThisInComputedPropertyName = 1 << 15, - ContainsRestOrSpread = 1 << 16, - ContainsObjectRestOrSpread = 1 << 17, - ContainsComputedPropertyName = 1 << 18, - ContainsBlockScopedBinding = 1 << 19, - ContainsBindingPattern = 1 << 20, - ContainsYield = 1 << 21, - ContainsHoistedDeclarationOrCompletion = 1 << 22, - ContainsDynamicImport = 1 << 23, - Super = 1 << 24, - ContainsSuper = 1 << 25, + ContainsTypeScriptClassSyntax = 1 << 11, // Decorators, Property Initializers, Parameter Property Initializers + ContainsLexicalThis = 1 << 12, + ContainsCapturedLexicalThis = 1 << 13, + ContainsLexicalThisInComputedPropertyName = 1 << 14, + ContainsRestOrSpread = 1 << 15, + ContainsObjectRestOrSpread = 1 << 16, + ContainsComputedPropertyName = 1 << 17, + ContainsBlockScopedBinding = 1 << 18, + ContainsBindingPattern = 1 << 19, + ContainsYield = 1 << 20, + ContainsHoistedDeclarationOrCompletion = 1 << 21, + ContainsDynamicImport = 1 << 22, + Super = 1 << 23, + ContainsSuper = 1 << 24, // Please leave this as 1 << 29. // It is the maximum bit we can set before we outgrow the size of a v8 small integer (SMI) on an x86 system. @@ -5080,7 +5077,7 @@ namespace ts { // Assertions // - Bitmasks that are used to assert facts about the syntax of a node and its subtree. - AssertTypeScript = TypeScript | ContainsTypeScript, + AssertTypeScript = ContainsTypeScript, AssertJsx = ContainsJsx, AssertESNext = ContainsESNext, AssertES2018 = ContainsES2018, @@ -5093,7 +5090,7 @@ namespace ts { // Scope Exclusions // - Bitmasks that exclude flags from propagating out of a specific context // into the subtree flags of their container. - OuterExpressionExcludes = TypeScript | DestructuringAssignment | Generator | HasComputedFlags, + OuterExpressionExcludes = DestructuringAssignment | Generator | HasComputedFlags, PropertyAccessExcludes = OuterExpressionExcludes | Super, NodeExcludes = PropertyAccessExcludes | ContainsSuper, ArrowFunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, diff --git a/tests/baselines/reference/arrowFunctionWithObjectLiteralBody5.js b/tests/baselines/reference/arrowFunctionWithObjectLiteralBody5.js index f9d924d6d9b..c097ccf0d50 100644 --- a/tests/baselines/reference/arrowFunctionWithObjectLiteralBody5.js +++ b/tests/baselines/reference/arrowFunctionWithObjectLiteralBody5.js @@ -11,4 +11,4 @@ var d = () => ((({ name: "foo", message: "bar" }))); var a = function () { return ({ name: "foo", message: "bar" }); }; var b = function () { return ({ name: "foo", message: "bar" }); }; var c = function () { return ({ name: "foo", message: "bar" }); }; -var d = function () { return (({ name: "foo", message: "bar" })); }; +var d = function () { return ({ name: "foo", message: "bar" }); }; diff --git a/tests/baselines/reference/arrowFunctionWithObjectLiteralBody6.js b/tests/baselines/reference/arrowFunctionWithObjectLiteralBody6.js index 6128a6bef86..270a9927835 100644 --- a/tests/baselines/reference/arrowFunctionWithObjectLiteralBody6.js +++ b/tests/baselines/reference/arrowFunctionWithObjectLiteralBody6.js @@ -11,4 +11,4 @@ var d = () => ((({ name: "foo", message: "bar" }))); var a = () => ({ name: "foo", message: "bar" }); var b = () => ({ name: "foo", message: "bar" }); var c = () => ({ name: "foo", message: "bar" }); -var d = () => (({ name: "foo", message: "bar" })); +var d = () => ({ name: "foo", message: "bar" }); diff --git a/tests/baselines/reference/parseErrorIncorrectReturnToken.js b/tests/baselines/reference/parseErrorIncorrectReturnToken.js index 3d2a1d17a4a..52a6fcf25cc 100644 --- a/tests/baselines/reference/parseErrorIncorrectReturnToken.js +++ b/tests/baselines/reference/parseErrorIncorrectReturnToken.js @@ -17,9 +17,7 @@ let o = { string; // should be => not : // doesn't work in non-type contexts, where the return type is optional var f = function (n) { return function (string) { return n.toString(); }; }; -var o = { - m: function (n) { } -}; +var o = {}; string; { return n.toString(); diff --git a/tests/baselines/reference/parserErrantEqualsGreaterThanAfterFunction2.js b/tests/baselines/reference/parserErrantEqualsGreaterThanAfterFunction2.js index 83586363466..5b4b71cab1f 100644 --- a/tests/baselines/reference/parserErrantEqualsGreaterThanAfterFunction2.js +++ b/tests/baselines/reference/parserErrantEqualsGreaterThanAfterFunction2.js @@ -2,5 +2,4 @@ function f(p: A) => p; //// [parserErrantEqualsGreaterThanAfterFunction2.js] -function f(p) { } p; diff --git a/tests/baselines/reference/thisTypeInFunctionsNegative.js b/tests/baselines/reference/thisTypeInFunctionsNegative.js index 9150536f4e7..ed454ee8b70 100644 --- a/tests/baselines/reference/thisTypeInFunctionsNegative.js +++ b/tests/baselines/reference/thisTypeInFunctionsNegative.js @@ -321,7 +321,6 @@ function modifiers() { return this.n; } function restParam(...) { return this.n; } function optional() { return this.n; } function decorated() { return this.n; } -function initializer(, C) { } (); number; {