From 8988ea849bf8c031323679fb1a7546bc79551c99 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 5 Apr 2016 18:24:02 -0700 Subject: [PATCH] Fixes indentation of object literals with computed properties --- src/compiler/printer.ts | 9 +++++++++ src/compiler/transformers/es6.ts | 11 +++++++---- src/compiler/utilities.ts | 3 +++ .../reference/computedPropertyNames16_ES5.js | 10 ++++++++++ .../reference/computedPropertyNames36_ES5.js | 4 ++++ .../reference/computedPropertyNames37_ES5.js | 4 ++++ .../reference/computedPropertyNames43_ES5.js | 4 ++++ .../reference/computedPropertyNames49_ES5.js | 1 - .../reference/computedPropertyNames50_ES5.js | 1 - 9 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/compiler/printer.ts b/src/compiler/printer.ts index ab8c2fdccce3f..c8ddc1c81a7cf 100644 --- a/src/compiler/printer.ts +++ b/src/compiler/printer.ts @@ -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(); + } } } diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es6.ts index aba5b209e61dc..648ad7c8eac58 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es6.ts @@ -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 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index bbe38798df3d6..1498f9aa93c1d 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1637,6 +1637,9 @@ namespace ts { const rightHandSideName = (nameExpression).name.text; return getPropertyNameForKnownSymbolName(rightHandSideName); } + if (isStringOrNumericLiteral(nameExpression.kind)) { + return (nameExpression).text; + } } return undefined; diff --git a/tests/baselines/reference/computedPropertyNames16_ES5.js b/tests/baselines/reference/computedPropertyNames16_ES5.js index 5d8471e6560a4..92b98ecb1baba 100644 --- a/tests/baselines/reference/computedPropertyNames16_ES5.js +++ b/tests/baselines/reference/computedPropertyNames16_ES5.js @@ -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, diff --git a/tests/baselines/reference/computedPropertyNames36_ES5.js b/tests/baselines/reference/computedPropertyNames36_ES5.js index 884e425e25b20..ec5643db26a7f 100644 --- a/tests/baselines/reference/computedPropertyNames36_ES5.js +++ b/tests/baselines/reference/computedPropertyNames36_ES5.js @@ -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 diff --git a/tests/baselines/reference/computedPropertyNames37_ES5.js b/tests/baselines/reference/computedPropertyNames37_ES5.js index 8cdc0af30e729..914fd9a898e20 100644 --- a/tests/baselines/reference/computedPropertyNames37_ES5.js +++ b/tests/baselines/reference/computedPropertyNames37_ES5.js @@ -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 diff --git a/tests/baselines/reference/computedPropertyNames43_ES5.js b/tests/baselines/reference/computedPropertyNames43_ES5.js index 9690d0b8196bb..c79a4d8d6d10a 100644 --- a/tests/baselines/reference/computedPropertyNames43_ES5.js +++ b/tests/baselines/reference/computedPropertyNames43_ES5.js @@ -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 diff --git a/tests/baselines/reference/computedPropertyNames49_ES5.js b/tests/baselines/reference/computedPropertyNames49_ES5.js index 12c3430fbe94f..d4eb26a08a533 100644 --- a/tests/baselines/reference/computedPropertyNames49_ES5.js +++ b/tests/baselines/reference/computedPropertyNames49_ES5.js @@ -60,7 +60,6 @@ var x = (_a = { enumerable: true, configurable: true }), - , _a.p2 = 20, _a); var _a; diff --git a/tests/baselines/reference/computedPropertyNames50_ES5.js b/tests/baselines/reference/computedPropertyNames50_ES5.js index db9a03578dfa0..0c6f1798c933e 100644 --- a/tests/baselines/reference/computedPropertyNames50_ES5.js +++ b/tests/baselines/reference/computedPropertyNames50_ES5.js @@ -56,7 +56,6 @@ var x = (_a = { enumerable: true, configurable: true }), - , _a.p2 = 20, _a); var _a;