From 7c4c0060af20349a086f649fff3d6c7505870264 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 4 Apr 2016 13:01:12 -0700 Subject: [PATCH] Fixes indentation of class expression bodies with static initializers. --- src/compiler/printer.ts | 20 ++++++++++++++++++ src/compiler/transformers/es6.ts | 21 +++++++++++++------ src/compiler/transformers/ts.ts | 4 ++++ src/compiler/types.ts | 3 ++- .../classExpressionWithStaticProperties1.js | 8 +++---- .../classExpressionWithStaticProperties2.js | 8 +++---- ...classExpressionWithStaticPropertiesES61.js | 2 +- ...classExpressionWithStaticPropertiesES62.js | 2 +- 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/compiler/printer.ts b/src/compiler/printer.ts index 1300ce585aa..ccdd07d6610 100644 --- a/src/compiler/printer.ts +++ b/src/compiler/printer.ts @@ -1396,6 +1396,11 @@ const _super = (function (geti, seti) { const body = node.body; if (body) { if (isBlock(body)) { + const indentedFlag = getNodeEmitFlags(node) & NodeEmitFlags.Indented; + if (indentedFlag) { + increaseIndent(); + } + const savedTempFlags = tempFlags; tempFlags = 0; startLexicalEnvironment(); @@ -1403,6 +1408,11 @@ const _super = (function (geti, seti) { write(" {"); emitBlockFunctionBody(node, body); write("}"); + + if (indentedFlag) { + decreaseIndent(); + } + tempFlags = savedTempFlags; } else { @@ -1497,6 +1507,12 @@ const _super = (function (geti, seti) { emitModifiers(node, node.modifiers); write("class"); emitWithPrefix(" ", node.name); + + const indentedFlag = getNodeEmitFlags(node) & NodeEmitFlags.Indented; + if (indentedFlag) { + increaseIndent(); + } + emitTypeParameters(node, node.typeParameters); emitList(node, node.heritageClauses, ListFormat.ClassHeritageClauses); @@ -1507,6 +1523,10 @@ const _super = (function (geti, seti) { emitList(node, node.members, ListFormat.ClassMembers); write("}"); + if (indentedFlag) { + decreaseIndent(); + } + tempFlags = savedTempFlags; } diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es6.ts index 59bb6046693..9c371b21b9a 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es6.ts @@ -607,14 +607,23 @@ namespace ts { // }(D)) const baseTypeNode = getClassExtendsHeritageClauseElement(node); + const classFunction = createFunctionExpression( + /*asteriskToken*/ undefined, + /*name*/ undefined, + baseTypeNode ? [createParameter("_super")] : [], + transformClassBody(node, baseTypeNode !== undefined) + ); + + // To preserve the behavior of the old emitter, we explicitly indent + // the body of the function here if it was requested in an earlier + // transformation. + if (getNodeEmitFlags(node) & NodeEmitFlags.Indented) { + setNodeEmitFlags(classFunction, NodeEmitFlags.Indented); + } + return createParen( createCall( - createFunctionExpression( - /*asteriskToken*/ undefined, - /*name*/ undefined, - baseTypeNode ? [createParameter("_super")] : [], - transformClassBody(node, baseTypeNode !== undefined) - ), + classFunction, baseTypeNode ? [visitNode(baseTypeNode.expression, visitor, isExpression)] : [] diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 9de4ba36d0e..968ac152065 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -673,6 +673,10 @@ namespace ts { const expressions: Expression[] = []; const temp = createTempVariable(); hoistVariableDeclaration(temp); + + // To preserve the behavior of the old emitter, we explicitly indent + // the body of a class with static initializers. + setNodeEmitFlags(classExpression, NodeEmitFlags.Indented | getNodeEmitFlags(classExpression)); addNode(expressions, createAssignment(temp, classExpression), true); addNodes(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp), true); addNode(expressions, temp, true); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 65f0fb14813..d9d456273b0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2873,7 +2873,8 @@ namespace ts { CapturesThis = 1 << 9, // The function captures a lexical `this` NoSourceMap = 1 << 10, // Do not emit a source map location for this node. NoNestedSourceMaps = 1 << 11, // Do not emit source map locations for children of this node. - PrefixExportedLocal = 1 << 12, + PrefixExportedLocal = 1 << 12, // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal). + Indented = 1 << 13, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter). } /** Additional context provided to `visitEachChild` */ diff --git a/tests/baselines/reference/classExpressionWithStaticProperties1.js b/tests/baselines/reference/classExpressionWithStaticProperties1.js index 1ede417f980..23d18d76766 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties1.js +++ b/tests/baselines/reference/classExpressionWithStaticProperties1.js @@ -3,10 +3,10 @@ var v = class C { static a = 1; static b = 2 }; //// [classExpressionWithStaticProperties1.js] var v = (_a = (function () { - function C() { - } - return C; -}()), + function C() { + } + return C; + }()), _a.a = 1, _a.b = 2, _a); diff --git a/tests/baselines/reference/classExpressionWithStaticProperties2.js b/tests/baselines/reference/classExpressionWithStaticProperties2.js index bdd6016d49d..b9fc0345591 100644 --- a/tests/baselines/reference/classExpressionWithStaticProperties2.js +++ b/tests/baselines/reference/classExpressionWithStaticProperties2.js @@ -3,10 +3,10 @@ var v = class C { static a = 1; static b }; //// [classExpressionWithStaticProperties2.js] var v = (_a = (function () { - function C() { - } - return C; -}()), + function C() { + } + return C; + }()), _a.a = 1, _a); var _a; diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js index f81e35fd822..43f5e7415c4 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES61.js @@ -3,7 +3,7 @@ var v = class C { static a = 1; static b = 2 }; //// [classExpressionWithStaticPropertiesES61.js] var v = (_a = class C { -}, + }, _a.a = 1, _a.b = 2, _a); diff --git a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js index 222d8af2521..1efa56ecaa2 100644 --- a/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js +++ b/tests/baselines/reference/classExpressionWithStaticPropertiesES62.js @@ -3,7 +3,7 @@ var v = class C { static a = 1; static b }; //// [classExpressionWithStaticPropertiesES62.js] var v = (_a = class C { -}, + }, _a.a = 1, _a); var _a;