Skip to content
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
33 changes: 24 additions & 9 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,10 @@ module ts {
return diagnostics;
}

interface SynthesizedNode extends Node {
startsOnNewLine: boolean;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be optional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's optional then it, as a structural type, has no meaning.

}

// @internal
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile): EmitResult {
Expand Down Expand Up @@ -2622,14 +2626,19 @@ module ts {
}
}

function createSynthesizedNode(kind: SyntaxKind): Node {
var node = createNode(kind);
function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
var node = <SynthesizedNode>createNode(kind);
node.pos = -1;
node.end = -1;
node.startsOnNewLine = startsOnNewLine;

return node;
}

function isSynthesized(node: Node) {
return node.pos === -1 && node.end === -1;
}

function emitDownlevelObjectLiteralWithComputedProperties(node: ObjectLiteralExpression, firstComputedPropertyIndex: number): void {
var parenthesizedObjectLiteral = createDownlevelObjectLiteralWithComputedProperties(node, firstComputedPropertyIndex);
return emit(parenthesizedObjectLiteral);
Expand Down Expand Up @@ -2663,7 +2672,7 @@ module ts {
});

// Finally, return the temp variable.
propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, tempVar);
propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, createIdentifier(tempVar.text, /*startsOnNewLine:*/ true));

var result = createParenthesizedExpression(propertyPatches);

Expand All @@ -2686,7 +2695,7 @@ module ts {
var leftHandSide = createMemberAccessForPropertyName(tempVar, property.name);
var maybeRightHandSide = tryGetRightHandSideOfPatchingPropertyAssignment(objectLiteral, property);

return maybeRightHandSide && createBinaryExpression(leftHandSide, SyntaxKind.EqualsToken, maybeRightHandSide);
return maybeRightHandSide && createBinaryExpression(leftHandSide, SyntaxKind.EqualsToken, maybeRightHandSide, /*startsOnNewLine:*/ true);
}

function tryGetRightHandSideOfPatchingPropertyAssignment(objectLiteral: ObjectLiteralExpression, property: ObjectLiteralElement) {
Expand Down Expand Up @@ -2759,8 +2768,8 @@ module ts {
return result;
}

function createBinaryExpression(left: Expression, operator: SyntaxKind, right: Expression): BinaryExpression {
var result = <BinaryExpression>createSynthesizedNode(SyntaxKind.BinaryExpression);
function createBinaryExpression(left: Expression, operator: SyntaxKind, right: Expression, startsOnNewLine?: boolean): BinaryExpression {
var result = <BinaryExpression>createSynthesizedNode(SyntaxKind.BinaryExpression, startsOnNewLine);
result.operatorToken = createSynthesizedNode(operator);
result.left = left;
result.right = right;
Expand Down Expand Up @@ -2815,8 +2824,8 @@ module ts {
return result;
}

function createIdentifier(name: string) {
var result = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
function createIdentifier(name: string, startsOnNewLine?: boolean) {
var result = <Identifier>createSynthesizedNode(SyntaxKind.Identifier, startsOnNewLine);
result.text = name;

return result;
Expand Down Expand Up @@ -3160,9 +3169,11 @@ module ts {

write(tokenToString(node.operatorToken.kind));

var shouldPlaceOnNewLine = !isSynthesized(node) && !nodeEndIsOnSameLineAsNodeStart(node.operatorToken, node.right);

// Check if the right expression is on a different line versus the operator itself. If so,
// we'll emit newline.
if (!nodeEndIsOnSameLineAsNodeStart(node.operatorToken, node.right)) {
if (shouldPlaceOnNewLine || synthesizedNodeStartsOnNewLine(node.right)) {
increaseIndent();
writeLine();
emit(node.right);
Expand All @@ -3175,6 +3186,10 @@ module ts {
}
}

function synthesizedNodeStartsOnNewLine(node: Node) {
return isSynthesized(node) && (<SynthesizedNode>node).startsOnNewLine;
}

function emitConditionalExpression(node: ConditionalExpression) {
emit(node.condition);
write(" ? ");
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5SymbolProperty1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ obj[Symbol.foo];

//// [ES5SymbolProperty1.js]
var Symbol;
var obj = (_a = {}, _a[Symbol.foo] =
0,
var obj = (_a = {},
_a[Symbol.foo] = 0,
_a);
obj[Symbol.foo];
var _a;
4 changes: 2 additions & 2 deletions tests/baselines/reference/FunctionDeclaration8_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var v = { [yield]: foo }

//// [FunctionDeclaration8_es6.js]
var v = (_a = {}, _a[yield] =
foo,
var v = (_a = {},
_a[yield] = foo,
_a);
var _a;
4 changes: 2 additions & 2 deletions tests/baselines/reference/FunctionDeclaration9_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ function * foo() {

//// [FunctionDeclaration9_es6.js]
function foo() {
var v = (_a = {}, _a[] =
foo,
var v = (_a = {},
_a[] = foo,
_a);
var _a;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var v = { *[foo()]() { } }

//// [FunctionPropertyAssignments5_es6.js]
var v = (_a = {}, _a[foo()] = function () { },
var v = (_a = {},
_a[foo()] = function () { },
_a);
var _a;
13 changes: 12 additions & 1 deletion tests/baselines/reference/computedPropertyNames10_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ var v = {
var s;
var n;
var a;
var v = (_a = {}, _a[s] = function () { }, _a[n] = function () { }, _a[s + s] = function () { }, _a[s + n] = function () { }, _a[+s] = function () { }, _a[""] = function () { }, _a[0] = function () { }, _a[a] = function () { }, _a[true] = function () { }, _a["hello bye"] = function () { }, _a["hello " + a + " bye"] = function () { },
var v = (_a = {},
_a[s] = function () { },
_a[n] = function () { },
_a[s + s] = function () { },
_a[s + n] = function () { },
_a[+s] = function () { },
_a[""] = function () { },
_a[0] = function () { },
_a[a] = function () { },
_a[true] = function () { },
_a["hello bye"] = function () { },
_a["hello " + a + " bye"] = function () { },
_a);
var _a;
13 changes: 12 additions & 1 deletion tests/baselines/reference/computedPropertyNames11_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ var v = {
var s;
var n;
var a;
var v = (_a = {}, _a[s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }), _a[n] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }), _a[s + s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }), _a[s + n] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }), _a[+s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }), _a[""] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }), _a[0] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }), _a[a] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }), _a[true] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }), _a["hello bye"] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }), _a["hello " + a + " bye"] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
var v = (_a = {},
_a[s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a[n] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
_a[s + s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a[s + n] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
_a[+s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a[""] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
_a[0] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a[a] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
_a[true] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a["hello bye"] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
_a["hello " + a + " bye"] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a);
var _a;
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames18_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function foo() {

//// [computedPropertyNames18_ES5.js]
function foo() {
var obj = (_a = {}, _a[this.bar] =
0,
var obj = (_a = {},
_a[this.bar] = 0,
_a);
var _a;
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames19_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module M {
//// [computedPropertyNames19_ES5.js]
var M;
(function (M) {
var obj = (_a = {}, _a[this.bar] =
0,
var obj = (_a = {},
_a[this.bar] = 0,
_a);
var _a;
})(M || (M = {}));
4 changes: 3 additions & 1 deletion tests/baselines/reference/computedPropertyNames1_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var v = {
}

//// [computedPropertyNames1_ES5.js]
var v = (_a = {}, _a[0 + 1] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }), _a[0 + 1] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
var v = (_a = {},
_a[0 + 1] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
_a[0 + 1] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
_a);
var _a;
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames20_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var obj = {
}

//// [computedPropertyNames20_ES5.js]
var obj = (_a = {}, _a[this.bar] =
0,
var obj = (_a = {},
_a[this.bar] = 0,
_a);
var _a;
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames22_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var C = (function () {
function C() {
}
C.prototype.bar = function () {
var obj = (_a = {}, _a[this.bar()] = function () { },
var obj = (_a = {},
_a[this.bar()] = function () { },
_a);
return 0;
var _a;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames23_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var C = (function () {
C.prototype.bar = function () {
return 0;
};
C.prototype[(_a = {}, _a[this.bar()] =
1,
C.prototype[(_a = {},
_a[this.bar()] = 1,
_a)[0]] = function () { };
return C;
})();
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames25_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var C = (function (_super) {
_super.apply(this, arguments);
}
C.prototype.foo = function () {
var obj = (_a = {}, _a[_super.prototype.bar.call(this)] = function () { },
var obj = (_a = {},
_a[_super.prototype.bar.call(this)] = function () { },
_a);
return 0;
var _a;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames26_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var C = (function (_super) {
}
// Gets emitted as super, not _super, which is consistent with
// use of super in static properties initializers.
C.prototype[(_a = {}, _a[super.bar.call(this)] =
1,
C.prototype[(_a = {},
_a[super.bar.call(this)] = 1,
_a)[0]] = function () { };
return C;
})(Base);
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames28_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var C = (function (_super) {
__extends(C, _super);
function C() {
_super.call(this);
var obj = (_a = {}, _a[(_super.call(this), "prop")] = function () { },
var obj = (_a = {},
_a[(_super.call(this), "prop")] = function () { },
_a);
var _a;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames29_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var C = (function () {
C.prototype.bar = function () {
var _this = this;
(function () {
var obj = (_a = {}, _a[_this.bar()] = function () { },
var obj = (_a = {},
_a[_this.bar()] = function () { },
_a);
var _a;
});
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames30_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var C = (function (_super) {
function C() {
_super.call(this);
(function () {
var obj = (_a = {}, _a[(_super.call(this), "prop")] = function () { },
var obj = (_a = {},
_a[(_super.call(this), "prop")] = function () { },
_a);
var _a;
});
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames31_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ var C = (function (_super) {
C.prototype.foo = function () {
var _this = this;
(function () {
var obj = (_a = {}, _a[_super.prototype.bar.call(_this)] = function () { },
var obj = (_a = {},
_a[_super.prototype.bar.call(_this)] = function () { },
_a);
var _a;
});
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames33_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var C = (function () {
function C() {
}
C.prototype.bar = function () {
var obj = (_a = {}, _a[foo()] = function () { },
var obj = (_a = {},
_a[foo()] = function () { },
_a);
return 0;
var _a;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/computedPropertyNames34_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var C = (function () {
function C() {
}
C.bar = function () {
var obj = (_a = {}, _a[foo()] = function () { },
var obj = (_a = {},
_a[foo()] = function () { },
_a);
return 0;
var _a;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames46_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var o = {
};

//// [computedPropertyNames46_ES5.js]
var o = (_a = {}, _a["" || 0] =
0,
var o = (_a = {},
_a["" || 0] = 0,
_a);
var _a;
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames47_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var E2;
(function (E2) {
E2[E2["x"] = 0] = "x";
})(E2 || (E2 = {}));
var o = (_a = {}, _a[0 /* x */ || 0 /* x */] =
0,
var o = (_a = {},
_a[0 /* x */ || 0 /* x */] = 0,
_a);
var _a;
12 changes: 6 additions & 6 deletions tests/baselines/reference/computedPropertyNames48_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ var E;
E[E["x"] = 0] = "x";
})(E || (E = {}));
var a;
extractIndexer((_a = {}, _a[a] =
"",
extractIndexer((_a = {},
_a[a] = "",
_a)); // Should return string
extractIndexer((_b = {}, _b[0 /* x */] =
"",
extractIndexer((_b = {},
_b[0 /* x */] = "",
_b)); // Should return string
extractIndexer((_c = {}, _c["" || 0] =
"",
extractIndexer((_c = {},
_c["" || 0] = "",
_c)); // Should return any (widened form of undefined)
var _a, _b, _c;
30 changes: 17 additions & 13 deletions tests/baselines/reference/computedPropertyNames49_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ var x = {
//// [computedPropertyNames49_ES5.js]
var x = (_a = {
p1: 10
}, _a.p1 =
10, _a[1 + 1] = Object.defineProperty({ get: function () {
throw 10;
}, enumerable: true, configurable: true }), _a[1 + 1] = Object.defineProperty({ get: function () {
return 10;
}, enumerable: true, configurable: true }), _a[1 + 1] = Object.defineProperty({ set: function () {
// just throw
throw 10;
}, enumerable: true, configurable: true }), _a.foo = Object.defineProperty({ get: function () {
if (1 == 1) {
},
_a.p1 = 10,
_a[1 + 1] = Object.defineProperty({ get: function () {
throw 10;
}, enumerable: true, configurable: true }),
_a[1 + 1] = Object.defineProperty({ get: function () {
return 10;
}
}, enumerable: true, configurable: true }), _a.p2 =
20,
}, enumerable: true, configurable: true }),
_a[1 + 1] = Object.defineProperty({ set: function () {
// just throw
throw 10;
}, enumerable: true, configurable: true }),
_a.foo = Object.defineProperty({ get: function () {
if (1 == 1) {
return 10;
}
}, enumerable: true, configurable: true }),
_a.p2 = 20,
_a);
var _a;
Loading