Skip to content

Commit

Permalink
Merge pull request #7845 from Microsoft/transforms-fixObjectLiteralBody
Browse files Browse the repository at this point in the history
Fixes indentation of object literals with computed properties
  • Loading branch information
rbuckton committed Apr 6, 2016
2 parents e3e45c5 + 8988ea8 commit 7b86dd5
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/compiler/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,18 @@ const _super = (function (geti, seti) {
write("{}");
}
else {
const indentedFlag = getNodeEmitFlags(node) & NodeEmitFlags.Indented;
if (indentedFlag) {
increaseIndent();
}

const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
const allowTrailingComma = languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None;
emitList(node, properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine);

if (indentedFlag) {
decreaseIndent();
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/compiler/transformers/es6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1688,10 +1688,13 @@ namespace ts {
addNode(expressions,
createAssignment(
temp,
createObjectLiteral(
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialNonComputedProperties),
/*location*/ undefined,
node.multiLine
setNodeEmitFlags(
createObjectLiteral(
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialNonComputedProperties),
/*location*/ undefined,
node.multiLine
),
NodeEmitFlags.Indented
)
),
node.multiLine
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,9 @@ namespace ts {
const rightHandSideName = (<PropertyAccessExpression>nameExpression).name.text;
return getPropertyNameForKnownSymbolName(rightHandSideName);
}
if (isStringOrNumericLiteral(nameExpression.kind)) {
return (<LiteralExpression>nameExpression).text;
}
}

return undefined;
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/computedPropertyNames16_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ var C = (function () {
enumerable: true,
configurable: true
});
Object.defineProperty(C, "", {
set: function (v) { },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, 0, {
get: function () { return 0; },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, a, {
set: function (v) { },
enumerable: true,
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/computedPropertyNames36_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var C = (function () {
Object.defineProperty(C.prototype, "get1", {
// Computed properties
get: function () { return new Foo; },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, "set1", {
set: function (p) { },
enumerable: true,
configurable: true
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/computedPropertyNames37_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var C = (function () {
Object.defineProperty(C.prototype, "get1", {
// Computed properties
get: function () { return new Foo; },
enumerable: true,
configurable: true
});
Object.defineProperty(C.prototype, "set1", {
set: function (p) { },
enumerable: true,
configurable: true
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/computedPropertyNames43_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ var D = (function (_super) {
Object.defineProperty(D.prototype, "get1", {
// Computed properties
get: function () { return new Foo; },
enumerable: true,
configurable: true
});
Object.defineProperty(D.prototype, "set1", {
set: function (p) { },
enumerable: true,
configurable: true
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/computedPropertyNames49_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ var x = (_a = {
enumerable: true,
configurable: true
}),
,
_a.p2 = 20,
_a);
var _a;
1 change: 0 additions & 1 deletion tests/baselines/reference/computedPropertyNames50_ES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var x = (_a = {
enumerable: true,
configurable: true
}),
,
_a.p2 = 20,
_a);
var _a;

0 comments on commit 7b86dd5

Please sign in to comment.