Preserve newlines appropriately between elements of an object literal or array literal when emitting.

This commit is contained in:
Cyrus Najmabadi
2015-02-06 20:16:35 -08:00
parent 0fbbbffde6
commit f44144a77c
26 changed files with 168 additions and 120 deletions
+69 -24
View File
@@ -1742,8 +1742,8 @@ module ts {
lastRecordedSourceMapSpan.emittedLine != emittedLine ||
lastRecordedSourceMapSpan.emittedColumn != emittedColumn ||
(lastRecordedSourceMapSpan.sourceIndex === sourceMapSourceIndex &&
(lastRecordedSourceMapSpan.sourceLine > sourceLinePos.line ||
(lastRecordedSourceMapSpan.sourceLine === sourceLinePos.line && lastRecordedSourceMapSpan.sourceColumn > sourceLinePos.character)))) {
(lastRecordedSourceMapSpan.sourceLine > sourceLinePos.line ||
(lastRecordedSourceMapSpan.sourceLine === sourceLinePos.line && lastRecordedSourceMapSpan.sourceColumn > sourceLinePos.character)))) {
// Encode the last recordedSpan before assigning new
encodeLastRecordedSourceMapSpan();
@@ -1994,7 +1994,7 @@ module ts {
break;
}
// _a .. _h, _j ... _z, _0, _1, ...
name = "_" + (tempCount < 25 ? String.fromCharCode(tempCount + (tempCount < 8 ? 0: 1) + CharacterCodes.a) : tempCount - 25);
name = "_" + (tempCount < 25 ? String.fromCharCode(tempCount + (tempCount < 8 ? 0 : 1) + CharacterCodes.a) : tempCount - 25);
tempCount++;
}
var result = <Identifier>createNode(SyntaxKind.Identifier);
@@ -2057,6 +2057,49 @@ module ts {
}
}
function emitLinePreservingList(parent: Node, nodes: NodeArray<Node>, allowTrailingComma: boolean, spacesBetweenBraces: boolean) {
Debug.assert(nodes.length > 0);
increaseIndent();
if (nodeStartPositionsAreOnSameLine(parent, nodes[0])) {
if (spacesBetweenBraces) {
write(" ");
}
}
else {
writeLine();
}
for (var i = 0, n = nodes.length; i < n; i++) {
if (i) {
if (nodeEndIsOnSameLineAsNodeStart(nodes[i - 1], nodes[i])) {
write(", ");
}
else {
write(",");
writeLine();
}
}
emit(nodes[i]);
}
var closeTokenIsOnSameLineAsLastElement = nodeEndPositionsAreOnSameLine(parent, lastOrUndefined(nodes));
if (nodes.hasTrailingComma && allowTrailingComma) {
write(",");
}
decreaseIndent();
if (closeTokenIsOnSameLineAsLastElement) {
if (spacesBetweenBraces) {
write(" ");
}
}
else {
writeLine();
}
}
function emitList(nodes: Node[], start: number, count: number, multiLine: boolean, trailingComma: boolean) {
if (multiLine) {
increaseIndent();
@@ -2122,7 +2165,7 @@ module ts {
function emitLiteral(node: LiteralExpression) {
var text = languageVersion < ScriptTarget.ES6 && isTemplateLiteralKind(node.kind) ? getTemplateLiteralAsStringLiteral(node) :
node.parent ? getSourceTextOfNodeFromSourceFile(currentSourceFile, node) :
node.text;
node.text;
if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writer.writeLiteral(text);
}
@@ -2423,6 +2466,10 @@ module ts {
return true;
}
function isSpreadElementExpression(node: Node) {
return node.kind === SyntaxKind.SpreadElementExpression;
}
function emitArrayLiteral(node: ArrayLiteralExpression) {
var elements = node.elements;
var length = elements.length;
@@ -2430,13 +2477,14 @@ module ts {
write("[]");
return;
}
if (languageVersion >= ScriptTarget.ES6) {
if (languageVersion >= ScriptTarget.ES6 || !forEach(elements, isSpreadElementExpression)) {
write("[");
emitList(elements, 0, elements.length, /*multiLine*/(node.flags & NodeFlags.MultiLine) !== 0,
/*trailingComma*/ elements.hasTrailingComma);
emitLinePreservingList(node, elements, /*allowTrailingComma:*/ true, /*spacesBetweenBraces:*/ false);
write("]");
return;
}
var pos = 0;
var group = 0;
while (pos < length) {
@@ -2459,7 +2507,7 @@ module ts {
i++;
}
write("[");
emitList(elements, pos, i - pos, /*multiLine*/ (node.flags & NodeFlags.MultiLine) !== 0,
emitList(elements, pos, i - pos, /*multiLine*/(node.flags & NodeFlags.MultiLine) !== 0,
/*trailingComma*/ elements.hasTrailingComma);
write("]");
pos = i;
@@ -2475,15 +2523,7 @@ module ts {
write("{");
var properties = node.properties;
if (properties.length) {
var multiLine = (node.flags & NodeFlags.MultiLine) !== 0;
if (!multiLine) {
write(" ");
}
emitList(properties, 0, properties.length, /*multiLine*/ multiLine,
/*trailingComma*/ properties.hasTrailingComma && languageVersion >= ScriptTarget.ES5);
if (!multiLine) {
write(" ");
}
emitLinePreservingList(node, properties, /*allowTrailingComma:*/ languageVersion >= ScriptTarget.ES5, /*spacesBetweenBraces:*/ true);
}
write("}");
}
@@ -2713,7 +2753,7 @@ module ts {
emit(node.whenFalse);
}
function isSingleLineBlock(node: Node) {
function isSingleLineEmptyBlock(node: Node) {
if (node && node.kind === SyntaxKind.Block) {
var block = <Block>node;
return block.statements.length === 0 && nodeEndIsOnSameLineAsNodeStart(block, block);
@@ -2721,7 +2761,7 @@ module ts {
}
function emitBlock(node: Block) {
if (isSingleLineBlock(node)) {
if (isSingleLineEmptyBlock(node)) {
emitToken(SyntaxKind.OpenBraceToken, node.pos);
write(" ");
emitToken(SyntaxKind.CloseBraceToken, node.statements.end);
@@ -2895,9 +2935,14 @@ module ts {
emitToken(SyntaxKind.CloseBraceToken, node.clauses.end);
}
function isOnSameLine(node1: Node, node2: Node) {
function nodeStartPositionsAreOnSameLine(node1: Node, node2: Node) {
return getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node1.pos)) ===
getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos));
getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos));
}
function nodeEndPositionsAreOnSameLine(node1: Node, node2: Node) {
return getLineOfLocalPosition(currentSourceFile, node1.end) ===
getLineOfLocalPosition(currentSourceFile, node2.end);
}
function nodeEndIsOnSameLineAsNodeStart(node1: Node, node2: Node) {
@@ -2914,7 +2959,7 @@ module ts {
else {
write("default:");
}
if (node.statements.length === 1 && isOnSameLine(node, node.statements[0])) {
if (node.statements.length === 1 && nodeStartPositionsAreOnSameLine(node, node.statements[0])) {
write(" ");
emit(node.statements[0]);
}
@@ -3404,7 +3449,7 @@ module ts {
emitSignatureParameters(node);
}
if (isSingleLineBlock(node.body) || !node.body) {
if (isSingleLineEmptyBlock(node.body) || !node.body) {
write(" { }");
}
else if (node.body.kind === SyntaxKind.Block) {
@@ -3449,7 +3494,7 @@ module ts {
// 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)) {
if (!preambleEmitted && nodeStartPositionsAreOnSameLine(node, body)) {
write(" ");
emitStart(body);
write("return ");
@@ -1930,8 +1930,6 @@ function compile(fileNames, options) {
}
exports.compile = compile;
compile(process.argv.slice(2), {
noEmitOnError: true,
noImplicitAny: true,
target: 1 /* ES5 */,
module: 1 /* CommonJS */
noEmitOnError: true, noImplicitAny: true,
target: 1 /* ES5 */, module: 1 /* CommonJS */
});
@@ -7,5 +7,6 @@ var v = { * foo() {
//// [YieldExpression10_es6.js]
var v = { foo: function () {
;
} };
;
}
};
+1 -3
View File
@@ -20,9 +20,7 @@ var C = (function () {
return C;
})();
var y = {
foo: ,
class: C2
}, _a = void 0;
foo: , class: C2 }, _a = void 0;
var M;
(function (M) {
var z = ;
@@ -150,26 +150,27 @@ var i1Ori2 = {
propertyOnlyInI2: "Hello"
};
var arrayI1OrI2 = [i1, i2, {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello"
}, {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
}, {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello",
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
}];
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello"
},
{
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
}, {
commonPropertyType: "hello",
commonMethodType: function (a) { return a; },
commonMethodWithTypeParameter: function (a) { return a; },
methodOnlyInI1: function (a) { return a; },
propertyOnlyInI1: "Hello",
methodOnlyInI2: function (a) { return a; },
propertyOnlyInI2: "Hello"
}];
var i11;
var i21;
var i11Ori21 = i11;
@@ -191,17 +192,17 @@ var i11Ori21 = {
commonPropertyDifferentType: 10
};
var arrayOrI11OrI21 = [i11, i21, i11 || i21, {
// Like i1
commonMethodDifferentReturnType: function (a, b) {
var z = a.charAt(b);
return z;
},
commonPropertyDifferentType: "hello"
}, {
// Like i2
commonMethodDifferentReturnType: function (a, b) {
var z = a.charCodeAt(b);
return z;
},
commonPropertyDifferentType: 10
}];
// Like i1
commonMethodDifferentReturnType: function (a, b) {
var z = a.charAt(b);
return z;
},
commonPropertyDifferentType: "hello"
}, {
// Like i2
commonMethodDifferentReturnType: function (a, b) {
var z = a.charCodeAt(b);
return z;
},
commonPropertyDifferentType: 10
}];
+2 -14
View File
@@ -150,21 +150,9 @@ var E9;
// (refer to .js to validate)
// Enum constant members are propagated
var doNotPropagate = [
E8.B,
E7.A,
E4.Z,
E3.X,
E3.Y,
E3.Z
E8.B, E7.A, E4.Z, E3.X, E3.Y, E3.Z
];
// Enum computed members are not propagated
var doPropagate = [
0 /* A */,
E9.B,
0 /* B */,
1 /* C */,
0 /* A */,
0 /* A */,
3 /* B */,
4 /* C */
0 /* A */, E9.B, 0 /* B */, 1 /* C */, 0 /* A */, 0 /* A */, 3 /* B */, 4 /* C */
];
@@ -61,5 +61,22 @@ function Foo<
//// [genericsManyTypeParameters.js]
function Foo(x1, y1, z1, a1, b1, c1, x2, y2, z2, a2, b2, c2, x3, y3, z3, a3, b3, c3, x4, y4, z4, a4, b4, c4, x5, y5, z5, a5, b5, c5, x6, y6, z6, a6, b6, c6, x7, y7, z7, a7, b7, c7, x8, y8, z8, a8, b8, c8, x9, y9, z9, a9, b9, c9, x10, y12, z10, a10, b10, c10, x11, y13, z11, a11, b11, c11, x12, y14, z12, a12, b12, c12, x13, y15, z13, a13, b13, c13, x14, y16, z14, a14, b14, c14, x15, y17, z15, a15, b15, c15, x16, y18, z16, a16, b16, c16, x17, y19, z17, a17, b17, c17, x18, y10, z18, a18, b18, c18) {
return [x1, y1, z1, a1, b1, c1, x2, y2, z2, a2, b2, c2, x3, y3, z3, a3, b3, c3, x4, y4, z4, a4, b4, c4, x5, y5, z5, a5, b5, c5, x6, y6, z6, a6, b6, c6, x7, y7, z7, a7, b7, c7, x8, y8, z8, a8, b8, c8, x9, y9, z9, a9, b9, c9, x10, y12, z10, a10, b10, c10, x11, y13, z11, a11, b11, c11, x12, y14, z12, a12, b12, c12, x13, y15, z13, a13, b13, c13, x14, y16, z14, a14, b14, c14, x15, y17, z15, a15, b15, c15, x16, y18, z16, a16, b16, c16, x17, y19, z17, a17, b17, c17, x18, y10, z18, a18, b18, c18];
return [x1, y1, z1, a1, b1, c1,
x2, y2, z2, a2, b2, c2,
x3, y3, z3, a3, b3, c3,
x4, y4, z4, a4, b4, c4,
x5, y5, z5, a5, b5, c5,
x6, y6, z6, a6, b6, c6,
x7, y7, z7, a7, b7, c7,
x8, y8, z8, a8, b8, c8,
x9, y9, z9, a9, b9, c9,
x10, y12, z10, a10, b10, c10,
x11, y13, z11, a11, b11, c11,
x12, y14, z12, a12, b12, c12,
x13, y15, z13, a13, b13, c13,
x14, y16, z14, a14, b14, c14,
x15, y17, z15, a15, b15, c15,
x16, y18, z16, a16, b16, c16,
x17, y19, z17, a17, b17, c17,
x18, y10, z18, a18, b18, c18];
}
@@ -37,5 +37,4 @@ var C3 = (function () {
return C3;
})();
var x = {
class: C4
}, _a = void 0;
class: C4 }, _a = void 0;
@@ -5,5 +5,4 @@ var v = {
//// [objectLiteralWithSemicolons4.js]
var v = {
a:
};
a: };
@@ -42,6 +42,5 @@ var C2 = (function () {
return C2;
})();
var b = {
x: function () { },
1: // error
x: function () { }, 1: // error
};
@@ -3,4 +3,5 @@ var v = [1, 2, 3
4, 5, 6, 7];
//// [parserErrorRecoveryArrayLiteralExpression1.js]
var v = [1, 2, 3, 4, 5, 6, 7];
var v = [1, 2, 3,
4, 5, 6, 7];
@@ -5,4 +5,5 @@ var points = [-0.6961439251899719, 1.207661509513855, 0.19374050199985504, -0
//// [parserErrorRecoveryArrayLiteralExpression2.js]
var points = [-0.6961439251899719, 1.207661509513855, 0.19374050199985504, -0, .7042760848999023, 1.1955541372299194, 0.19600726664066315, -0.7120069861412048];
var points = [-0.6961439251899719, 1.207661509513855, 0.19374050199985504, -0,
.7042760848999023, 1.1955541372299194, 0.19600726664066315, -0.7120069861412048];
@@ -3,4 +3,5 @@ var v = { a
return;
//// [parserErrorRecovery_ObjectLiteral2.js]
var v = { a: , return: };
var v = { a: ,
return: };
@@ -3,4 +3,5 @@ var v = { a:
return;
//// [parserErrorRecovery_ObjectLiteral3.js]
var v = { a: , return: };
var v = { a: ,
return: };
@@ -3,4 +3,5 @@ var v = { a: 1
return;
//// [parserErrorRecovery_ObjectLiteral4.js]
var v = { a: 1, return: };
var v = { a: 1,
return: };
@@ -3,4 +3,5 @@ var v = { a: 1,
return;
//// [parserErrorRecovery_ObjectLiteral5.js]
var v = { a: 1, return: };
var v = { a: 1,
return: };
+1 -3
View File
@@ -2627,9 +2627,7 @@ var Harness;
var description = b.description + (prop ? ": " + prop : '');
emitLog('testStart', { desc: description });
emitLog('pass', {
desc: description,
pass: true,
perfResults: {
desc: description, pass: true, perfResults: {
mean: b.results[prop].mean(),
min: b.results[prop].min(),
max: b.results[prop].max(),
+1 -2
View File
@@ -12,7 +12,6 @@ var y: {
//// [privateIndexer2.js]
// private indexers not allowed
var x = {
[x]: string,
string:
[x]: string, string:
};
var y;
+6 -3
View File
@@ -108,6 +108,9 @@ function C(a, b) {
this.a = a;
this.b = b;
}
C.prototype = { a: 0, b: 0, C1M1: function (c, d) {
return (this.a + c) + (this.b + d);
} };
C.prototype = { a: 0,
b: 0,
C1M1: function (c, d) {
return (this.a + c) + (this.b + d);
}
};
@@ -6,6 +6,5 @@ var x = {
//// [templateStringInObjectLiteral.js]
var x = {
a: "abc" + 123 + "def"
} "b";
a: "abc" + 123 + "def" } "b";
321;
@@ -6,6 +6,5 @@ var x = {
//// [templateStringInObjectLiteralES6.js]
var x = {
a: `abc${123}def`,
} `b`;
a: `abc${123}def`, } `b`;
321;
@@ -29,8 +29,7 @@ var menuData = [
"type": "image",
"link": "",
"icon": "modules/menu/logo.svg"
},
{
}, {
"id": "productName",
"type": "default",
"link": "",
@@ -9,8 +9,8 @@ var functions = [function () {
//// [typeCheckingInsideFunctionExpressionInArray.js]
var functions = [function () {
var k = 10;
k = new Object();
[1, 2, 3].NonexistantMethod();
derp();
}];
var k = 10;
k = new Object();
[1, 2, 3].NonexistantMethod();
derp();
}];
@@ -11,6 +11,6 @@ var functions = [function() {
//// [undefinedSymbolReferencedInArrayLiteral1.js]
var tokens = [{ startIndex: deltaOffset }];
var functions = [function () {
[1, 2, 3].NonexistantMethod();
anotherNonExistingMethod();
}];
[1, 2, 3].NonexistantMethod();
anotherNonExistingMethod();
}];
@@ -30,6 +30,6 @@ var C = (function () {
})();
var c = new C({ length: 2 });
var r = c.foo({ length: 3, charAt: function (x) {
'';
} });
'';
} });
var r2 = r('');
@@ -28,5 +28,5 @@ var C = (function () {
var c = new C({ length: 2 });
var r = c.foo('');
var r2 = r({ length: 3, charAt: function (x) {
'';
} }); // error
'';
} }); // error