mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Provide specialized functions for emitting the body of a function depending on if that body is an expression or a block.
This commit is contained in:
parent
ff31b96533
commit
11aa4d362b
@ -3404,77 +3404,14 @@ module ts {
|
||||
emitSignatureParameters(node);
|
||||
}
|
||||
|
||||
if (isSingleLineBlock(node.body)) {
|
||||
if (isSingleLineBlock(node.body) || !node.body) {
|
||||
write(" { }");
|
||||
}
|
||||
else if (node.body.kind === SyntaxKind.Block) {
|
||||
emitBlockFunctionBody(node, <Block>node.body);
|
||||
}
|
||||
else {
|
||||
write(" {");
|
||||
scopeEmitStart(node);
|
||||
|
||||
if (!node.body) {
|
||||
writeLine();
|
||||
write("}");
|
||||
}
|
||||
else {
|
||||
increaseIndent();
|
||||
|
||||
emitDetachedComments(node.body.kind === SyntaxKind.Block ? (<Block>node.body).statements : node.body);
|
||||
|
||||
var startIndex = 0;
|
||||
if (node.body.kind === SyntaxKind.Block) {
|
||||
startIndex = emitDirectivePrologues((<Block>node.body).statements, /*startWithNewLine*/ true);
|
||||
}
|
||||
var outPos = writer.getTextPos();
|
||||
|
||||
emitCaptureThisForNodeIfNecessary(node);
|
||||
emitDefaultValueAssignments(node);
|
||||
emitRestParameter(node);
|
||||
if (node.body.kind !== SyntaxKind.Block && outPos === writer.getTextPos()) {
|
||||
decreaseIndent();
|
||||
write(" ");
|
||||
emitStart(node.body);
|
||||
write("return ");
|
||||
|
||||
// Don't emit comments on this body. We'll have already taken care of it above
|
||||
// when we called emitDetachedComments.
|
||||
emitNode(node.body, /*disableComments:*/ true);
|
||||
emitEnd(node.body);
|
||||
write(";");
|
||||
emitTempDeclarations(/*newLine*/ false);
|
||||
write(" ");
|
||||
emitStart(node.body);
|
||||
write("}");
|
||||
emitEnd(node.body);
|
||||
}
|
||||
else {
|
||||
if (node.body.kind === SyntaxKind.Block) {
|
||||
emitLinesStartingAt((<Block>node.body).statements, startIndex);
|
||||
}
|
||||
else {
|
||||
writeLine();
|
||||
emitLeadingComments(node.body);
|
||||
write("return ");
|
||||
emit(node.body, /*disableComments:*/ true);
|
||||
write(";");
|
||||
emitTrailingComments(node.body);
|
||||
}
|
||||
emitTempDeclarations(/*newLine*/ true);
|
||||
writeLine();
|
||||
if (node.body.kind === SyntaxKind.Block) {
|
||||
emitLeadingCommentsOfPosition((<Block>node.body).statements.end);
|
||||
decreaseIndent();
|
||||
emitToken(SyntaxKind.CloseBraceToken, (<Block>node.body).statements.end);
|
||||
}
|
||||
else {
|
||||
decreaseIndent();
|
||||
emitStart(node.body);
|
||||
write("}");
|
||||
emitEnd(node.body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scopeEmitEnd();
|
||||
emitExpressionFunctionBody(node, <Expression>node.body);
|
||||
}
|
||||
|
||||
if (node.flags & NodeFlags.Export) {
|
||||
@ -3486,11 +3423,86 @@ module ts {
|
||||
emitEnd(node);
|
||||
write(";");
|
||||
}
|
||||
|
||||
tempCount = saveTempCount;
|
||||
tempVariables = saveTempVariables;
|
||||
tempParameters = saveTempParameters;
|
||||
}
|
||||
|
||||
// Returns true if any preamble code was emitted.
|
||||
function emitFunctionBodyPreamble(node: FunctionLikeDeclaration): void {
|
||||
emitCaptureThisForNodeIfNecessary(node);
|
||||
emitDefaultValueAssignments(node);
|
||||
emitRestParameter(node);
|
||||
}
|
||||
|
||||
function emitExpressionFunctionBody(node: FunctionLikeDeclaration, body: Expression) {
|
||||
write(" {");
|
||||
scopeEmitStart(node);
|
||||
|
||||
increaseIndent();
|
||||
var outPos = writer.getTextPos();
|
||||
emitDetachedComments(node.body);
|
||||
emitFunctionBodyPreamble(node);
|
||||
var preambleEmitted = writer.getTextPos() !== outPos;
|
||||
decreaseIndent();
|
||||
|
||||
// If we didn't have to emit any preamble code, then attempt to keep the arrow
|
||||
// function on one line.
|
||||
if (!preambleEmitted && isOnSameLine(node, body)) {
|
||||
write(" ");
|
||||
emitStart(body);
|
||||
write("return ");
|
||||
|
||||
// Don't emit comments on this body. We'll have already taken care of it above
|
||||
// when we called emitDetachedComments.
|
||||
emitNode(body, /*disableComments:*/ true);
|
||||
emitEnd(body);
|
||||
write(";");
|
||||
emitTempDeclarations(/*newLine*/ false);
|
||||
write(" ");
|
||||
}
|
||||
else {
|
||||
increaseIndent();
|
||||
writeLine();
|
||||
emitLeadingComments(node.body);
|
||||
write("return ");
|
||||
emit(node.body, /*disableComments:*/ true);
|
||||
write(";");
|
||||
emitTrailingComments(node.body);
|
||||
|
||||
emitTempDeclarations(/*newLine*/ true);
|
||||
decreaseIndent();
|
||||
writeLine();
|
||||
}
|
||||
|
||||
emitStart(node.body);
|
||||
write("}");
|
||||
emitEnd(node.body);
|
||||
|
||||
scopeEmitEnd();
|
||||
}
|
||||
|
||||
function emitBlockFunctionBody(node: FunctionLikeDeclaration, body: Block) {
|
||||
write(" {");
|
||||
scopeEmitStart(node);
|
||||
|
||||
increaseIndent();
|
||||
emitDetachedComments(body.statements);
|
||||
var startIndex = emitDirectivePrologues(body.statements, /*startWithNewLine*/ true);
|
||||
|
||||
emitFunctionBodyPreamble(node);
|
||||
emitLinesStartingAt(body.statements, startIndex);
|
||||
emitTempDeclarations(/*newLine*/ true);
|
||||
|
||||
writeLine();
|
||||
emitLeadingCommentsOfPosition(body.statements.end);
|
||||
decreaseIndent();
|
||||
emitToken(SyntaxKind.CloseBraceToken, body.statements.end);
|
||||
|
||||
scopeEmitEnd();
|
||||
}
|
||||
|
||||
function findInitialSuperCall(ctor: ConstructorDeclaration): ExpressionStatement {
|
||||
if (ctor.body) {
|
||||
var statement = (<Block>ctor.body).statements[0];
|
||||
|
||||
@ -2,5 +2,4 @@
|
||||
var v = { * }
|
||||
|
||||
//// [FunctionPropertyAssignments4_es6.js]
|
||||
var v = { : function () {
|
||||
} };
|
||||
var v = { : function () { } };
|
||||
|
||||
@ -2,5 +2,4 @@
|
||||
var v = { get foo() }
|
||||
|
||||
//// [accessorWithoutBody1.js]
|
||||
var v = { get foo() {
|
||||
} };
|
||||
var v = { get foo() { } };
|
||||
|
||||
@ -2,5 +2,4 @@
|
||||
var v = { set foo(a) }
|
||||
|
||||
//// [accessorWithoutBody2.js]
|
||||
var v = { set foo(a) {
|
||||
} };
|
||||
var v = { set foo(a) { } };
|
||||
|
||||
@ -11,4 +11,7 @@ Foo(() =>
|
||||
//// [commentOnSimpleArrowFunctionBody1.js]
|
||||
function Foo(x) {
|
||||
}
|
||||
Foo(function () { return 127; });
|
||||
Foo(function () {
|
||||
// do something
|
||||
return 127;
|
||||
});
|
||||
|
||||
@ -19,5 +19,13 @@ var foo: <K, N>(g: (x: K) => N) =>
|
||||
|
||||
//// [genericsAndHigherOrderFunctions.js]
|
||||
// no errors expected
|
||||
var combine = function (f) { return function (g) { return function (x) { return f(g(x)); }; }; };
|
||||
var foo = function (g) { return function (h) { return function (f) { return h(combine(f)(g)); }; }; };
|
||||
var combine = function (f) {
|
||||
return function (g) {
|
||||
return function (x) { return f(g(x)); };
|
||||
};
|
||||
};
|
||||
var foo = function (g) {
|
||||
return function (h) {
|
||||
return function (f) { return h(combine(f)(g)); };
|
||||
};
|
||||
};
|
||||
|
||||
@ -706,44 +706,38 @@ define(["require", "exports"], function (require, exports) {
|
||||
C.prototype.rF = function () { };
|
||||
C.prototype.pgF = function () { };
|
||||
Object.defineProperty(C.prototype, "pgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.psF = function (param) { };
|
||||
Object.defineProperty(C.prototype, "psF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.rgF = function () { };
|
||||
Object.defineProperty(C.prototype, "rgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.rsF = function (param) { };
|
||||
Object.defineProperty(C.prototype, "rsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.tF = function () { };
|
||||
C.tsF = function (param) { };
|
||||
Object.defineProperty(C, "tsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.tgF = function () { };
|
||||
Object.defineProperty(C, "tgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
@ -761,44 +755,38 @@ define(["require", "exports"], function (require, exports) {
|
||||
C.prototype.rF = function () { };
|
||||
C.prototype.pgF = function () { };
|
||||
Object.defineProperty(C.prototype, "pgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.psF = function (param) { };
|
||||
Object.defineProperty(C.prototype, "psF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.rgF = function () { };
|
||||
Object.defineProperty(C.prototype, "rgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.rsF = function (param) { };
|
||||
Object.defineProperty(C.prototype, "rsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.tF = function () { };
|
||||
C.tsF = function (param) { };
|
||||
Object.defineProperty(C, "tsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.tgF = function () { };
|
||||
Object.defineProperty(C, "tgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
@ -845,44 +833,38 @@ define(["require", "exports"], function (require, exports) {
|
||||
eC.prototype.rF = function () { };
|
||||
eC.prototype.pgF = function () { };
|
||||
Object.defineProperty(eC.prototype, "pgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.psF = function (param) { };
|
||||
Object.defineProperty(eC.prototype, "psF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.rgF = function () { };
|
||||
Object.defineProperty(eC.prototype, "rgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.rsF = function (param) { };
|
||||
Object.defineProperty(eC.prototype, "rsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.tF = function () { };
|
||||
eC.tsF = function (param) { };
|
||||
Object.defineProperty(eC, "tsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.tgF = function () { };
|
||||
Object.defineProperty(eC, "tgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
@ -932,44 +914,38 @@ define(["require", "exports"], function (require, exports) {
|
||||
eC.prototype.rF = function () { };
|
||||
eC.prototype.pgF = function () { };
|
||||
Object.defineProperty(eC.prototype, "pgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.psF = function (param) { };
|
||||
Object.defineProperty(eC.prototype, "psF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.rgF = function () { };
|
||||
Object.defineProperty(eC.prototype, "rgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.rsF = function (param) { };
|
||||
Object.defineProperty(eC.prototype, "rsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.tF = function () { };
|
||||
eC.tsF = function (param) { };
|
||||
Object.defineProperty(eC, "tsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.tgF = function () { };
|
||||
Object.defineProperty(eC, "tgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
@ -988,44 +964,38 @@ define(["require", "exports"], function (require, exports) {
|
||||
C.prototype.rF = function () { };
|
||||
C.prototype.pgF = function () { };
|
||||
Object.defineProperty(C.prototype, "pgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.psF = function (param) { };
|
||||
Object.defineProperty(C.prototype, "psF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.rgF = function () { };
|
||||
Object.defineProperty(C.prototype, "rgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.prototype.rsF = function (param) { };
|
||||
Object.defineProperty(C.prototype, "rsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.tF = function () { };
|
||||
C.tsF = function (param) { };
|
||||
Object.defineProperty(C, "tsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
C.tgF = function () { };
|
||||
Object.defineProperty(C, "tgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
@ -1072,44 +1042,38 @@ define(["require", "exports"], function (require, exports) {
|
||||
eC.prototype.rF = function () { };
|
||||
eC.prototype.pgF = function () { };
|
||||
Object.defineProperty(eC.prototype, "pgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.psF = function (param) { };
|
||||
Object.defineProperty(eC.prototype, "psF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.rgF = function () { };
|
||||
Object.defineProperty(eC.prototype, "rgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.prototype.rsF = function (param) { };
|
||||
Object.defineProperty(eC.prototype, "rsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.tF = function () { };
|
||||
eC.tsF = function (param) { };
|
||||
Object.defineProperty(eC, "tsF", {
|
||||
set: function (param) {
|
||||
},
|
||||
set: function (param) { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
eC.tgF = function () { };
|
||||
Object.defineProperty(eC, "tgF", {
|
||||
get: function () {
|
||||
},
|
||||
get: function () { },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
@ -12,4 +12,7 @@ Foo(() =>
|
||||
//// [lambdaASIEmit.js]
|
||||
function Foo(x) {
|
||||
}
|
||||
Foo(function () { return 127; });
|
||||
Foo(function () {
|
||||
// do something
|
||||
return 127;
|
||||
});
|
||||
|
||||
@ -2,5 +2,4 @@
|
||||
var v = { foo(); }
|
||||
|
||||
//// [objectLiteralMemberWithoutBlock1.js]
|
||||
var v = { foo: function () {
|
||||
} };
|
||||
var v = { foo: function () { } };
|
||||
|
||||
@ -10,7 +10,9 @@ function test()
|
||||
//// [thisReferencedInFunctionInsideArrowFunction1.js]
|
||||
var foo = function (dummy) { };
|
||||
function test() {
|
||||
foo(function () { return function () {
|
||||
return this;
|
||||
}; });
|
||||
foo(function () {
|
||||
return function () {
|
||||
return this;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user