Skip to content

Commit 71c3bcc

Browse files
Produce better wrapping for object literal emit with computed property names.
1 parent 21fb559 commit 71c3bcc

File tree

50 files changed

+299
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+299
-236
lines changed

src/compiler/emitter.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,10 @@ module ts {
15231523
return diagnostics;
15241524
}
15251525

1526+
interface SynthesizedNode extends Node {
1527+
startsOnNewLine: boolean;
1528+
}
1529+
15261530
// @internal
15271531
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
15281532
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile): EmitResult {
@@ -2622,14 +2626,19 @@ module ts {
26222626
}
26232627
}
26242628

2625-
function createSynthesizedNode(kind: SyntaxKind): Node {
2626-
var node = createNode(kind);
2629+
function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
2630+
var node = <SynthesizedNode>createNode(kind);
26272631
node.pos = -1;
26282632
node.end = -1;
2633+
node.startsOnNewLine = startsOnNewLine;
26292634

26302635
return node;
26312636
}
26322637

2638+
function isSynthesized(node: Node) {
2639+
return node.pos === -1 && node.end === -1;
2640+
}
2641+
26332642
function emitDownlevelObjectLiteralWithComputedProperties(node: ObjectLiteralExpression, firstComputedPropertyIndex: number): void {
26342643
var parenthesizedObjectLiteral = createDownlevelObjectLiteralWithComputedProperties(node, firstComputedPropertyIndex);
26352644
return emit(parenthesizedObjectLiteral);
@@ -2663,7 +2672,7 @@ module ts {
26632672
});
26642673

26652674
// Finally, return the temp variable.
2666-
propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, tempVar);
2675+
propertyPatches = createBinaryExpression(propertyPatches, SyntaxKind.CommaToken, createIdentifier(tempVar.text, /*startsOnNewLine:*/ true));
26672676

26682677
var result = createParenthesizedExpression(propertyPatches);
26692678

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

2689-
return maybeRightHandSide && createBinaryExpression(leftHandSide, SyntaxKind.EqualsToken, maybeRightHandSide);
2698+
return maybeRightHandSide && createBinaryExpression(leftHandSide, SyntaxKind.EqualsToken, maybeRightHandSide, /*startsOnNewLine:*/ true);
26902699
}
26912700

26922701
function tryGetRightHandSideOfPatchingPropertyAssignment(objectLiteral: ObjectLiteralExpression, property: ObjectLiteralElement) {
@@ -2759,8 +2768,8 @@ module ts {
27592768
return result;
27602769
}
27612770

2762-
function createBinaryExpression(left: Expression, operator: SyntaxKind, right: Expression): BinaryExpression {
2763-
var result = <BinaryExpression>createSynthesizedNode(SyntaxKind.BinaryExpression);
2771+
function createBinaryExpression(left: Expression, operator: SyntaxKind, right: Expression, startsOnNewLine?: boolean): BinaryExpression {
2772+
var result = <BinaryExpression>createSynthesizedNode(SyntaxKind.BinaryExpression, startsOnNewLine);
27642773
result.operatorToken = createSynthesizedNode(operator);
27652774
result.left = left;
27662775
result.right = right;
@@ -2815,8 +2824,8 @@ module ts {
28152824
return result;
28162825
}
28172826

2818-
function createIdentifier(name: string) {
2819-
var result = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
2827+
function createIdentifier(name: string, startsOnNewLine?: boolean) {
2828+
var result = <Identifier>createSynthesizedNode(SyntaxKind.Identifier, startsOnNewLine);
28202829
result.text = name;
28212830

28222831
return result;
@@ -3160,9 +3169,11 @@ module ts {
31603169

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

3172+
var shouldPlaceOnNewLine = !isSynthesized(node) && !nodeEndIsOnSameLineAsNodeStart(node.operatorToken, node.right);
3173+
31633174
// Check if the right expression is on a different line versus the operator itself. If so,
31643175
// we'll emit newline.
3165-
if (!nodeEndIsOnSameLineAsNodeStart(node.operatorToken, node.right)) {
3176+
if (shouldPlaceOnNewLine || synthesizedNodeStartsOnNewLine(node.right)) {
31663177
increaseIndent();
31673178
writeLine();
31683179
emit(node.right);
@@ -3175,6 +3186,10 @@ module ts {
31753186
}
31763187
}
31773188

3189+
function synthesizedNodeStartsOnNewLine(node: Node) {
3190+
return isSynthesized(node) && (<SynthesizedNode>node).startsOnNewLine;
3191+
}
3192+
31783193
function emitConditionalExpression(node: ConditionalExpression) {
31793194
emit(node.condition);
31803195
write(" ? ");

tests/baselines/reference/ES5SymbolProperty1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ obj[Symbol.foo];
1212

1313
//// [ES5SymbolProperty1.js]
1414
var Symbol;
15-
var obj = (_a = {}, _a[Symbol.foo] =
16-
0,
15+
var obj = (_a = {},
16+
_a[Symbol.foo] = 0,
1717
_a);
1818
obj[Symbol.foo];
1919
var _a;

tests/baselines/reference/FunctionDeclaration8_es6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var v = { [yield]: foo }
33

44
//// [FunctionDeclaration8_es6.js]
5-
var v = (_a = {}, _a[yield] =
6-
foo,
5+
var v = (_a = {},
6+
_a[yield] = foo,
77
_a);
88
var _a;

tests/baselines/reference/FunctionDeclaration9_es6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function * foo() {
55

66
//// [FunctionDeclaration9_es6.js]
77
function foo() {
8-
var v = (_a = {}, _a[] =
9-
foo,
8+
var v = (_a = {},
9+
_a[] = foo,
1010
_a);
1111
var _a;
1212
}

tests/baselines/reference/FunctionPropertyAssignments5_es6.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var v = { *[foo()]() { } }
33

44
//// [FunctionPropertyAssignments5_es6.js]
5-
var v = (_a = {}, _a[foo()] = function () { },
5+
var v = (_a = {},
6+
_a[foo()] = function () { },
67
_a);
78
var _a;

tests/baselines/reference/computedPropertyNames10_ES5.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ var v = {
2020
var s;
2121
var n;
2222
var a;
23-
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 () { },
23+
var v = (_a = {},
24+
_a[s] = function () { },
25+
_a[n] = function () { },
26+
_a[s + s] = function () { },
27+
_a[s + n] = function () { },
28+
_a[+s] = function () { },
29+
_a[""] = function () { },
30+
_a[0] = function () { },
31+
_a[a] = function () { },
32+
_a[true] = function () { },
33+
_a["hello bye"] = function () { },
34+
_a["hello " + a + " bye"] = function () { },
2435
_a);
2536
var _a;

tests/baselines/reference/computedPropertyNames11_ES5.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ var v = {
2020
var s;
2121
var n;
2222
var a;
23-
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 }),
23+
var v = (_a = {},
24+
_a[s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
25+
_a[n] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
26+
_a[s + s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
27+
_a[s + n] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
28+
_a[+s] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
29+
_a[""] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
30+
_a[0] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
31+
_a[a] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
32+
_a[true] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
33+
_a["hello bye"] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
34+
_a["hello " + a + " bye"] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
2435
_a);
2536
var _a;

tests/baselines/reference/computedPropertyNames18_ES5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function foo() {
77

88
//// [computedPropertyNames18_ES5.js]
99
function foo() {
10-
var obj = (_a = {}, _a[this.bar] =
11-
0,
10+
var obj = (_a = {},
11+
_a[this.bar] = 0,
1212
_a);
1313
var _a;
1414
}

tests/baselines/reference/computedPropertyNames19_ES5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module M {
88
//// [computedPropertyNames19_ES5.js]
99
var M;
1010
(function (M) {
11-
var obj = (_a = {}, _a[this.bar] =
12-
0,
11+
var obj = (_a = {},
12+
_a[this.bar] = 0,
1313
_a);
1414
var _a;
1515
})(M || (M = {}));

tests/baselines/reference/computedPropertyNames1_ES5.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var v = {
55
}
66

77
//// [computedPropertyNames1_ES5.js]
8-
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 }),
8+
var v = (_a = {},
9+
_a[0 + 1] = Object.defineProperty({ get: function () { return 0; }, enumerable: true, configurable: true }),
10+
_a[0 + 1] = Object.defineProperty({ set: function (v) { }, enumerable: true, configurable: true }),
911
_a);
1012
var _a;

0 commit comments

Comments
 (0)