From 27931d5f756ea29390eec38e1d9984804e39bde3 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 13 Jun 2016 13:17:56 -0700 Subject: [PATCH] Cleanup, fix linter errors, follow hoisted declarations. --- src/compiler/binder.ts | 11 ++-- src/compiler/transformers/generators.ts | 59 ++++++++------------ src/compiler/types.ts | 11 ++-- tests/baselines/reference/exportEqualsAmd.js | 2 +- tests/baselines/reference/exportEqualsUmd.js | 2 +- 5 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index bbeb5253fff..a5a7804b2af 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2484,6 +2484,7 @@ namespace ts { const modifierFlags = getModifierFlags(node); const body = node.body; const typeParameters = node.typeParameters; + const asteriskToken = node.asteriskToken; // A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded, // generic, or has a decorator. @@ -2495,7 +2496,7 @@ namespace ts { } // Currently, we only support generators that were originally async function bodies. - if (node.asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) { + if (asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) { transformFlags |= TransformFlags.AssertGenerator; } @@ -2546,7 +2547,7 @@ namespace ts { transformFlags = TransformFlags.AssertTypeScript; } else { - transformFlags = subtreeFlags; + transformFlags = subtreeFlags | TransformFlags.ContainsHoistedDeclaration; // If a FunctionDeclaration is exported, then it is either ES6 or TypeScript syntax. if (modifierFlags & ModifierFlags.Export) { @@ -2566,7 +2567,7 @@ namespace ts { } // Currently, we only support generators that were originally async function bodies. - if (node.asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) { + if (asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) { transformFlags |= TransformFlags.AssertGenerator; } } @@ -2592,7 +2593,7 @@ namespace ts { } // Currently, we only support generators that were originally async function bodies. - if (node.asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) { + if (asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) { transformFlags |= TransformFlags.AssertGenerator; } @@ -2725,7 +2726,7 @@ namespace ts { } function computeVariableDeclarationList(node: VariableDeclarationList, subtreeFlags: TransformFlags) { - let transformFlags = subtreeFlags; + let transformFlags = subtreeFlags | TransformFlags.ContainsHoistedDeclaration; if (subtreeFlags & TransformFlags.ContainsBindingPattern) { transformFlags |= TransformFlags.AssertES6; diff --git a/src/compiler/transformers/generators.ts b/src/compiler/transformers/generators.ts index 9e00b6e6ea5..29aa55314d0 100644 --- a/src/compiler/transformers/generators.ts +++ b/src/compiler/transformers/generators.ts @@ -137,12 +137,11 @@ namespace ts { Endfinally // Marks the end of a `finally` block } - type OperationArguments = - [Label] | - [Label, Expression] | - [Statement] | - [Expression] | - [Expression, Expression]; + type OperationArguments = [Label] + | [Label, Expression] + | [Statement] + | [Expression] + | [Expression, Expression]; // whether a generated code block is opening or closing at the current operation for a FunctionBuilder const enum BlockAction { @@ -394,8 +393,16 @@ namespace ts { case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: return visitAccessorDeclaration(node); + case SyntaxKind.VariableStatement: + return visitVariableStatement(node); + case SyntaxKind.BreakStatement: + return visitBreakStatement(node); + case SyntaxKind.ContinueStatement: + return visitContinueStatement(node); + case SyntaxKind.ReturnStatement: + return visitReturnStatement(node); default: - if (node.transformFlags & (TransformFlags.ContainsGenerator | TransformFlags.ContainsYield)) { + if (node.transformFlags & (TransformFlags.ContainsGenerator | TransformFlags.ContainsYield | TransformFlags.ContainsHoistedDeclaration)) { return visitEachChild(node, visitor, context); } else { @@ -943,19 +950,20 @@ namespace ts { // _a); const properties = node.properties; - const numInitialProperties = countInitialNodesWithoutYield(node.properties); + const multiLine = node.multiLine; + const numInitialProperties = countInitialNodesWithoutYield(properties); const temp = declareLocal(); emitAssignment(temp, createObjectLiteral( - visitNodes(node.properties, visitor, isObjectLiteralElement, 0, numInitialProperties), + visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties), /*location*/ undefined, - node.multiLine + multiLine ) ); - const expressions = reduceLeft(node.properties, reduceProperty, [], numInitialProperties); - addNode(expressions, getMutableClone(temp), node.multiLine); + const expressions = reduceLeft(properties, reduceProperty, [], numInitialProperties); + addNode(expressions, getMutableClone(temp), multiLine); return inlineExpressions(expressions); function reduceProperty(expressions: Expression[], property: ObjectLiteralElement) { @@ -965,7 +973,7 @@ namespace ts { } const expression = createExpressionForObjectLiteralElement(node, property, temp); - addNode(expressions, visitNode(expression, visitor, isExpression), node.multiLine); + addNode(expressions, visitNode(expression, visitor, isExpression), multiLine); return expressions; } } @@ -1867,7 +1875,8 @@ namespace ts { labelOffsets = []; } - const label = nextLabelId++; + const label = nextLabelId; + nextLabelId++; labelOffsets[label] = -1; return label; } @@ -2023,7 +2032,6 @@ namespace ts { const exception = peekBlock(); Debug.assert(exception.state < ExceptionBlockState.Finally); - const state = exception.state; const endLabel = exception.endLabel; emitBreak(endLabel); @@ -2344,14 +2352,6 @@ namespace ts { return createCall(createPropertyAccess(state, "sent"), /*typeArguments*/ undefined, [], location); } - /** - * Emits a NOP (no operation). This is often used to ensure a new operation location exists - * when marking labels. - */ - function emitNop(): void { - emitWorker(OpCode.Nop); - } - /** * Emits a Statement. * @@ -2421,16 +2421,6 @@ namespace ts { emitWorker(OpCode.Yield, [expression], location); } - /** - * Emits a YieldStar operation for the provided expression. - * - * @param expression A value for the operation. - * @param location An optional source map location for the assignment. - */ - function emitYieldStar(expression: Expression, location?: TextRange): void { - emitWorker(OpCode.YieldStar, [expression], location); - } - /** * Emits a Return operation for the provided expression. * @@ -2651,7 +2641,6 @@ namespace ts { return; } - let isLabel: boolean = false; for (let label = 0; label < labelOffsets.length; label++) { if (labelOffsets[label] === operationIndex) { flushLabel(); @@ -2659,7 +2648,7 @@ namespace ts { labelNumbers = []; } if (labelNumbers[labelNumber] === undefined) { - labelNumbers[labelNumber] = [label] + labelNumbers[labelNumber] = [label]; } else { labelNumbers[labelNumber].push(label); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 349c637d336..25bbeff92b7 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2987,6 +2987,7 @@ namespace ts { ContainsBlockScopedBinding = 1 << 20, ContainsBindingPattern = 1 << 21, ContainsYield = 1 << 22, + ContainsHoistedDeclaration = 1 << 23, HasComputedFlags = 1 << 29, // Transform flags have been computed. @@ -3002,12 +3003,12 @@ namespace ts { // - Bitmasks that exclude flags from propagating out of a specific context // into the subtree flags of their container. NodeExcludes = TypeScript | Jsx | ES7 | ES6 | DestructuringAssignment | HasComputedFlags, - ArrowFunctionExcludes = NodeExcludes | ContainsDecorators | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsParameterPropertyAssignments | ContainsBlockScopedBinding | ContainsYield, - FunctionExcludes = NodeExcludes | ContainsDecorators | ContainsDefaultValueAssignments | ContainsCapturedLexicalThis | ContainsLexicalThis | ContainsParameterPropertyAssignments | ContainsBlockScopedBinding | ContainsYield, - ConstructorExcludes = NodeExcludes | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding | ContainsYield, - MethodOrAccessorExcludes = NodeExcludes | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding | ContainsYield, + ArrowFunctionExcludes = NodeExcludes | ContainsDecorators | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsParameterPropertyAssignments | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclaration, + FunctionExcludes = NodeExcludes | ContainsDecorators | ContainsDefaultValueAssignments | ContainsCapturedLexicalThis | ContainsLexicalThis | ContainsParameterPropertyAssignments | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclaration, + ConstructorExcludes = NodeExcludes | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclaration, + MethodOrAccessorExcludes = NodeExcludes | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclaration, ClassExcludes = NodeExcludes | ContainsDecorators | ContainsPropertyInitializer | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsComputedPropertyName | ContainsParameterPropertyAssignments | ContainsLexicalThisInComputedPropertyName, - ModuleExcludes = NodeExcludes | ContainsDecorators | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding, + ModuleExcludes = NodeExcludes | ContainsDecorators | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding | ContainsHoistedDeclaration, TypeExcludes = ~ContainsTypeScript, ObjectLiteralExcludes = NodeExcludes | ContainsDecorators | ContainsComputedPropertyName | ContainsLexicalThisInComputedPropertyName, ArrayLiteralOrCallOrNewExcludes = NodeExcludes | ContainsSpreadElementExpression, diff --git a/tests/baselines/reference/exportEqualsAmd.js b/tests/baselines/reference/exportEqualsAmd.js index a0f029589d6..385012a6e0f 100644 --- a/tests/baselines/reference/exportEqualsAmd.js +++ b/tests/baselines/reference/exportEqualsAmd.js @@ -4,6 +4,6 @@ export = { ["hi"]: "there" }; //// [exportEqualsAmd.js] define(["require", "exports"], function (require, exports) { "use strict"; - return (_a = {}, _a["hi"] = "there", _a); + return _a = {}, _a["hi"] = "there", _a; var _a; }); diff --git a/tests/baselines/reference/exportEqualsUmd.js b/tests/baselines/reference/exportEqualsUmd.js index a9b75978d06..cd4c99f504d 100644 --- a/tests/baselines/reference/exportEqualsUmd.js +++ b/tests/baselines/reference/exportEqualsUmd.js @@ -11,6 +11,6 @@ export = { ["hi"]: "there" }; } })(["require", "exports"], function (require, exports) { "use strict"; - return (_a = {}, _a["hi"] = "there", _a); + return _a = {}, _a["hi"] = "there", _a; var _a; });