Skip to content

Fixes indentation of class expression bodies #7811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/compiler/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1396,13 +1396,23 @@ 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();
emitSignatureHead(node);
write(" {");
emitBlockFunctionBody(node, body);
write("}");

if (indentedFlag) {
decreaseIndent();
}

tempFlags = savedTempFlags;
}
else {
Expand Down Expand Up @@ -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);

Expand All @@ -1507,6 +1523,10 @@ const _super = (function (geti, seti) {
emitList(node, node.members, ListFormat.ClassMembers);
write("}");

if (indentedFlag) {
decreaseIndent();
}

tempFlags = savedTempFlags;
}

Expand Down
21 changes: 15 additions & 6 deletions src/compiler/transformers/es6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
: []
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;