From 610104bef8ab3493daa0595d099321556aad7b0d Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 24 Aug 2017 13:40:20 -0700 Subject: [PATCH 1/2] Switch to arrow for ts class wrapper IIFE --- src/compiler/factory.ts | 43 +++++++++++++- src/compiler/transformers/es2015.ts | 59 ++----------------- src/compiler/transformers/ts.ts | 5 +- src/compiler/types.ts | 1 + src/compiler/utilities.ts | 6 +- .../reference/decoratorOnClassMethod11.js | 1 + .../reference/decoratorOnClassMethod12.js | 1 + .../inferringClassMembersFromAssignments.js | 1 + tests/baselines/reference/superAccess2.js | 1 + ...thisInArrowFunctionInStaticInitializer1.js | 1 + .../reference/thisInConstructorParameter2.js | 1 + .../reference/thisInInvalidContexts.js | 1 + .../thisInInvalidContextsExternalModule.js | 1 + .../reference/thisInOuterClassBody.js | 1 + .../reference/typeOfThisInStaticMembers2.js | 1 + 15 files changed, 64 insertions(+), 60 deletions(-) diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 9d7f6c82a7ae7..4492c3ed47426 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -2372,6 +2372,24 @@ namespace ts { ); } + export function createImmediatelyInvokedArrowFunction(statements: Statement[]): CallExpression; + export function createImmediatelyInvokedArrowFunction(statements: Statement[], param: ParameterDeclaration, paramValue: Expression): CallExpression; + export function createImmediatelyInvokedArrowFunction(statements: Statement[], param?: ParameterDeclaration, paramValue?: Expression) { + return createCall( + createArrowFunction( + /*modifiers*/ undefined, + /*typeParameters*/ undefined, + /*parameters*/ param ? [param] : [], + /*type*/ undefined, + /*equalsGreaterThanToken*/ undefined, + createBlock(statements, /*multiLine*/ true) + ), + /*typeArguments*/ undefined, + /*argumentsArray*/ paramValue ? [paramValue] : [] + ); + } + + export function createComma(left: Expression, right: Expression) { return createBinary(left, SyntaxKind.CommaToken, right); } @@ -4036,8 +4054,31 @@ namespace ts { } } + /** + * Determines whether a node is a parenthesized expression that can be ignored when recreating outer expressions. + * + * A parenthesized expression can be ignored when all of the following are true: + * + * - It's `pos` and `end` are not -1 + * - It does not have a custom source map range + * - It does not have a custom comment range + * - It does not have synthetic leading or trailing comments + * + * If an outermost parenthesized expression is ignored, but the containing expression requires a parentheses around + * the expression to maintain precedence, a new parenthesized expression should be created automatically when + * the containing expression is created/updated. + */ + function isIgnorableParen(node: Expression) { + return node.kind === SyntaxKind.ParenthesizedExpression + && nodeIsSynthesized(node) + && nodeIsSynthesized(getSourceMapRange(node)) + && nodeIsSynthesized(getCommentRange(node)) + && !some(getSyntheticLeadingComments(node)) + && !some(getSyntheticTrailingComments(node)); + } + export function recreateOuterExpressions(outerExpression: Expression | undefined, innerExpression: Expression, kinds = OuterExpressionKinds.All): Expression { - if (outerExpression && isOuterExpression(outerExpression, kinds)) { + if (outerExpression && isOuterExpression(outerExpression, kinds) && !isIgnorableParen(outerExpression)) { return updateOuterExpression( outerExpression, recreateOuterExpressions(outerExpression.expression, innerExpression) diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index ce907d49bee8d..e9c84be51766a 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -339,64 +339,12 @@ namespace ts { && !(node).expression; } - function isClassLikeVariableStatement(node: Node) { - if (!isVariableStatement(node)) return false; - const variable = singleOrUndefined((node).declarationList.declarations); - return variable - && variable.initializer - && isIdentifier(variable.name) - && (isClassLike(variable.initializer) - || (isAssignmentExpression(variable.initializer) - && isIdentifier(variable.initializer.left) - && isClassLike(variable.initializer.right))); - } - - function isTypeScriptClassWrapper(node: Node) { - const call = tryCast(node, isCallExpression); - if (!call || isParseTreeNode(call) || - some(call.typeArguments) || - some(call.arguments)) { - return false; - } - - const func = tryCast(skipOuterExpressions(call.expression), isFunctionExpression); - if (!func || isParseTreeNode(func) || - some(func.typeParameters) || - some(func.parameters) || - func.type || - !func.body) { - return false; - } - - const statements = func.body.statements; - if (statements.length < 2) { - return false; - } - - const firstStatement = statements[0]; - if (isParseTreeNode(firstStatement) || - !isClassLike(firstStatement) && - !isClassLikeVariableStatement(firstStatement)) { - return false; - } - - const lastStatement = elementAt(statements, -1); - const returnStatement = tryCast(isVariableStatement(lastStatement) ? elementAt(statements, -2) : lastStatement, isReturnStatement); - if (!returnStatement || - !returnStatement.expression || - !isIdentifier(skipOuterExpressions(returnStatement.expression))) { - return false; - } - - return true; - } - function shouldVisitNode(node: Node): boolean { return (node.transformFlags & TransformFlags.ContainsES2015) !== 0 || convertedLoopState !== undefined || (hierarchyFacts & HierarchyFacts.ConstructorWithCapturedSuper && (isStatement(node) || (node.kind === SyntaxKind.Block))) || (isIterationStatement(node, /*lookInLabeledStatements*/ false) && shouldConvertIterationStatementBody(node)) - || isTypeScriptClassWrapper(node); + || (getEmitFlags(node) & EmitFlags.TypeScriptClassWrapper) !== 0; } function visitor(node: Node): VisitResult { @@ -3308,13 +3256,14 @@ namespace ts { * @param node a CallExpression. */ function visitCallExpression(node: CallExpression) { - if (isTypeScriptClassWrapper(node)) { + if (getEmitFlags(node) & EmitFlags.TypeScriptClassWrapper) { return visitTypeScriptClassWrapper(node); } if (node.transformFlags & TransformFlags.ES2015) { return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ true); } + return updateCall( node, visitNode(node.expression, callExpressionVisitor, isExpression), @@ -3357,7 +3306,7 @@ namespace ts { // We skip any outer expressions in a number of places to get to the innermost // expression, but we will restore them later to preserve comments and source maps. - const body = cast(skipOuterExpressions(node.expression), isFunctionExpression).body; + const body = cast(cast(skipOuterExpressions(node.expression), isArrowFunction).body, isBlock); // The class statements are the statements generated by visiting the first statement of the // body (1), while all other statements are added to remainingStatements (2) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 4c679ea1eee84..71a3c7dcb7a0a 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -613,13 +613,16 @@ namespace ts { addRange(statements, context.endLexicalEnvironment()); + const iife = createImmediatelyInvokedArrowFunction(statements); + setEmitFlags(iife, EmitFlags.TypeScriptClassWrapper); + const varStatement = createVariableStatement( /*modifiers*/ undefined, createVariableDeclarationList([ createVariableDeclaration( getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ false), /*type*/ undefined, - createImmediatelyInvokedFunctionExpression(statements) + iife ) ]) ); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index fe18839b9048c..3ef38e0e4b741 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -4184,6 +4184,7 @@ namespace ts { HasEndOfDeclarationMarker = 1 << 22, // Declaration has an associated NotEmittedStatement to mark the end of the declaration Iterator = 1 << 23, // The expression to a `yield*` should be treated as an Iterator when down-leveling, not an Iterable. NoAsciiEscaping = 1 << 24, // When synthesizing nodes that lack an original node or textSourceNode, we want to write the text on the node with ASCII escaping substitutions. + /*@internal*/ TypeScriptClassWrapper = 1 << 25, // The node is an IIFE class wrapper created by the ts transform. } export interface EmitHelper { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index d906ab4f6b1e7..de752faaf6ae8 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2068,9 +2068,9 @@ namespace ts { || kind === SyntaxKind.SourceFile; } - export function nodeIsSynthesized(node: TextRange): boolean { - return positionIsSynthesized(node.pos) - || positionIsSynthesized(node.end); + export function nodeIsSynthesized(range: TextRange): boolean { + return positionIsSynthesized(range.pos) + || positionIsSynthesized(range.end); } export function getOriginalSourceFile(sourceFile: SourceFile) { diff --git a/tests/baselines/reference/decoratorOnClassMethod11.js b/tests/baselines/reference/decoratorOnClassMethod11.js index 2600681c64746..e29c4076c9154 100644 --- a/tests/baselines/reference/decoratorOnClassMethod11.js +++ b/tests/baselines/reference/decoratorOnClassMethod11.js @@ -17,6 +17,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, }; var M; (function (M) { + var _this = this; var C = /** @class */ (function () { function C() { } diff --git a/tests/baselines/reference/decoratorOnClassMethod12.js b/tests/baselines/reference/decoratorOnClassMethod12.js index 494cb083a20f3..0063e0225bc44 100644 --- a/tests/baselines/reference/decoratorOnClassMethod12.js +++ b/tests/baselines/reference/decoratorOnClassMethod12.js @@ -28,6 +28,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, }; var M; (function (M) { + var _this = this; var S = /** @class */ (function () { function S() { } diff --git a/tests/baselines/reference/inferringClassMembersFromAssignments.js b/tests/baselines/reference/inferringClassMembersFromAssignments.js index 3fa72bce2cf5b..90f87734b2e1e 100644 --- a/tests/baselines/reference/inferringClassMembersFromAssignments.js +++ b/tests/baselines/reference/inferringClassMembersFromAssignments.js @@ -124,6 +124,7 @@ var stringOrNumberOrUndefined = C.inStaticNestedArrowFunction; //// [output.js] +var _this = this; var C = /** @class */ (function () { function C() { var _this = this; diff --git a/tests/baselines/reference/superAccess2.js b/tests/baselines/reference/superAccess2.js index de755361a16ca..d3aac217e2229 100644 --- a/tests/baselines/reference/superAccess2.js +++ b/tests/baselines/reference/superAccess2.js @@ -35,6 +35,7 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); +var _this = this; var P = /** @class */ (function () { function P() { } diff --git a/tests/baselines/reference/thisInArrowFunctionInStaticInitializer1.js b/tests/baselines/reference/thisInArrowFunctionInStaticInitializer1.js index 97e958ace5ec1..2d58fd455ac4a 100644 --- a/tests/baselines/reference/thisInArrowFunctionInStaticInitializer1.js +++ b/tests/baselines/reference/thisInArrowFunctionInStaticInitializer1.js @@ -9,6 +9,7 @@ class Vector { } //// [thisInArrowFunctionInStaticInitializer1.js] +var _this = this; function log(a) { } var Vector = /** @class */ (function () { function Vector() { diff --git a/tests/baselines/reference/thisInConstructorParameter2.js b/tests/baselines/reference/thisInConstructorParameter2.js index 6a27183800c2d..a5e4f4d8b5712 100644 --- a/tests/baselines/reference/thisInConstructorParameter2.js +++ b/tests/baselines/reference/thisInConstructorParameter2.js @@ -10,6 +10,7 @@ class P { } //// [thisInConstructorParameter2.js] +var _this = this; var P = /** @class */ (function () { function P(z, zz) { if (z === void 0) { z = this; } diff --git a/tests/baselines/reference/thisInInvalidContexts.js b/tests/baselines/reference/thisInInvalidContexts.js index 635eff25cf4a1..1e24cc4d9e2c2 100644 --- a/tests/baselines/reference/thisInInvalidContexts.js +++ b/tests/baselines/reference/thisInInvalidContexts.js @@ -59,6 +59,7 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); +var _this = this; //'this' in static member initializer var ErrClass1 = /** @class */ (function () { function ErrClass1() { diff --git a/tests/baselines/reference/thisInInvalidContextsExternalModule.js b/tests/baselines/reference/thisInInvalidContextsExternalModule.js index ea69a582730e3..daadace2cfa09 100644 --- a/tests/baselines/reference/thisInInvalidContextsExternalModule.js +++ b/tests/baselines/reference/thisInInvalidContextsExternalModule.js @@ -60,6 +60,7 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); +var _this = this; //'this' in static member initializer var ErrClass1 = /** @class */ (function () { function ErrClass1() { diff --git a/tests/baselines/reference/thisInOuterClassBody.js b/tests/baselines/reference/thisInOuterClassBody.js index 2b4e3a1aabdb6..d5bf689b49a13 100644 --- a/tests/baselines/reference/thisInOuterClassBody.js +++ b/tests/baselines/reference/thisInOuterClassBody.js @@ -21,6 +21,7 @@ class Foo { } //// [thisInOuterClassBody.js] +var _this = this; var Foo = /** @class */ (function () { function Foo() { this.x = this; diff --git a/tests/baselines/reference/typeOfThisInStaticMembers2.js b/tests/baselines/reference/typeOfThisInStaticMembers2.js index 72243cdd3e619..1cfec3a3fa1d6 100644 --- a/tests/baselines/reference/typeOfThisInStaticMembers2.js +++ b/tests/baselines/reference/typeOfThisInStaticMembers2.js @@ -8,6 +8,7 @@ class C2 { } //// [typeOfThisInStaticMembers2.js] +var _this = this; var C = /** @class */ (function () { function C() { } From ccd0158c406727d7a1ec81bdc579e8c6e6607f9d Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 24 Aug 2017 15:06:06 -0700 Subject: [PATCH 2/2] Added additional test --- .../reference/asyncArrowInClassES5.js | 21 +++++++++++++++++++ .../reference/asyncArrowInClassES5.symbols | 12 +++++++++++ .../reference/asyncArrowInClassES5.types | 13 ++++++++++++ tests/cases/compiler/asyncArrowInClassES5.ts | 9 ++++++++ 4 files changed, 55 insertions(+) create mode 100644 tests/baselines/reference/asyncArrowInClassES5.js create mode 100644 tests/baselines/reference/asyncArrowInClassES5.symbols create mode 100644 tests/baselines/reference/asyncArrowInClassES5.types create mode 100644 tests/cases/compiler/asyncArrowInClassES5.ts diff --git a/tests/baselines/reference/asyncArrowInClassES5.js b/tests/baselines/reference/asyncArrowInClassES5.js new file mode 100644 index 0000000000000..f0204f7319bda --- /dev/null +++ b/tests/baselines/reference/asyncArrowInClassES5.js @@ -0,0 +1,21 @@ +//// [asyncArrowInClassES5.ts] +// https://github.com/Microsoft/TypeScript/issues/16924 +// Should capture `this` + +class Test { + static member = async (x: string) => { }; +} + + +//// [asyncArrowInClassES5.js] +// https://github.com/Microsoft/TypeScript/issues/16924 +// Should capture `this` +var _this = this; +var Test = /** @class */ (function () { + function Test() { + } + Test.member = function (x) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { + return [2 /*return*/]; + }); }); }; + return Test; +}()); diff --git a/tests/baselines/reference/asyncArrowInClassES5.symbols b/tests/baselines/reference/asyncArrowInClassES5.symbols new file mode 100644 index 0000000000000..a6cc9f75d6e3a --- /dev/null +++ b/tests/baselines/reference/asyncArrowInClassES5.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/asyncArrowInClassES5.ts === +// https://github.com/Microsoft/TypeScript/issues/16924 +// Should capture `this` + +class Test { +>Test : Symbol(Test, Decl(asyncArrowInClassES5.ts, 0, 0)) + + static member = async (x: string) => { }; +>member : Symbol(Test.member, Decl(asyncArrowInClassES5.ts, 3, 12)) +>x : Symbol(x, Decl(asyncArrowInClassES5.ts, 4, 27)) +} + diff --git a/tests/baselines/reference/asyncArrowInClassES5.types b/tests/baselines/reference/asyncArrowInClassES5.types new file mode 100644 index 0000000000000..8da6d5d2eaf26 --- /dev/null +++ b/tests/baselines/reference/asyncArrowInClassES5.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/asyncArrowInClassES5.ts === +// https://github.com/Microsoft/TypeScript/issues/16924 +// Should capture `this` + +class Test { +>Test : Test + + static member = async (x: string) => { }; +>member : (x: string) => Promise +>async (x: string) => { } : (x: string) => Promise +>x : string +} + diff --git a/tests/cases/compiler/asyncArrowInClassES5.ts b/tests/cases/compiler/asyncArrowInClassES5.ts new file mode 100644 index 0000000000000..2712372d35706 --- /dev/null +++ b/tests/cases/compiler/asyncArrowInClassES5.ts @@ -0,0 +1,9 @@ +// @noEmitHelpers: true +// @lib: es2015 +// @target: es5 +// https://github.com/Microsoft/TypeScript/issues/16924 +// Should capture `this` + +class Test { + static member = async (x: string) => { }; +}