mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-16 05:58:32 -06:00
Merge pull request #7811 from Microsoft/transforms-fixClassExpressionIndentation
Fixes indentation of class expression bodies
This commit is contained in:
commit
0f4a3dbc63
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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)]
|
||||
: []
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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` */
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user