diff --git a/escodegen.js b/escodegen.js index 6fe6646d..3e1a207e 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2223,13 +2223,19 @@ multiline = false; if (expr.properties.length === 1) { property = expr.properties[0]; - if (property.value.type !== Syntax.Identifier) { + if ( + property.type === Syntax.Property + && property.value.type !== Syntax.Identifier + ) { multiline = true; } } else { for (i = 0, iz = expr.properties.length; i < iz; ++i) { property = expr.properties[i]; - if (!property.shorthand) { + if ( + property.type === Syntax.Property + && !property.shorthand + ) { multiline = true; break; } diff --git a/test/harmony.js b/test/harmony.js index f790c01e..833a0fbd 100644 --- a/test/harmony.js +++ b/test/harmony.js @@ -2548,6 +2548,263 @@ data = { } }, + 'Harmony object pattern, singleline, 1 property: RestElement': { + 'const {...foo} = {};': { + generateFrom: { + type: 'Program', + body: [ + { + type: 'VariableDeclaration', + declarations: [ + { + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: [ + { + type: 'RestElement', + 'argument': { + type: 'Identifier', + name: 'foo' + } + } + ] + }, + init: { + type: 'ObjectExpression', + properties: [] + } + } + ], + kind: 'const' + } + ], + sourceType: "script" + } + } + }, + + 'Harmony object pattern, singleline, 2 properties: Property, Property': { + 'const {foo, bar} = {};': { + generateFrom: { + type: 'Program', + body: [ + { + type: 'VariableDeclaration', + declarations: [ + { + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: [ + { + type: 'Property', + method: false, + shorthand: true, + computed: false, + key: { + type: 'Identifier', + name: 'foo' + }, + kind: 'init', + value: { + type: 'Identifier', + name: 'foo' + } + }, + { + type: 'Property', + method: false, + shorthand: true, + computed: false, + key: { + type: 'Identifier', + name: 'bar' + }, + kind: 'init', + value: { + type: 'Identifier', + name: 'bar' + } + }, + ] + }, + init: { + type: 'ObjectExpression', + properties: [] + } + } + ], + kind: 'const' + } + ], + sourceType: "script" + } + } + }, + + 'Harmony object pattern, singleline, 2 properties: Property, RestElement': { + 'const {foo, ...bar} = {};': { + generateFrom: { + type: 'Program', + body: [ + { + type: 'VariableDeclaration', + declarations: [ + { + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: [ + { + type: 'Property', + method: false, + shorthand: true, + computed: false, + key: { + type: 'Identifier', + name: 'foo' + }, + kind: 'init', + value: { + type: 'Identifier', + name: 'foo' + } + }, + { + type: 'RestElement', + 'argument': { + type: 'Identifier', + name: 'bar' + } + } + ] + }, + init: { + type: 'ObjectExpression', + properties: [] + } + } + ], + kind: 'const' + } + ], + sourceType: "script" + } + } + }, + + 'Harmony object pattern, multiline, 1 property: Property': { + 'const {\n foo = 1\n} = {};': { + generateFrom: { + type: 'Program', + body: [ + { + type: 'VariableDeclaration', + declarations: [ + { + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: [ + { + type: 'Property', + method: false, + shorthand: true, + computed: false, + key: { + type: 'Identifier', + name: 'foo' + }, + kind: 'init', + value: { + type: 'AssignmentPattern', + left: { + type: 'Identifier', + name: 'foo' + }, + right: { + type: 'Literal', + value: 1, + raw: '1' + } + } + } + ] + }, + init: { + type: 'ObjectExpression', + properties: [] + } + } + ], + kind: 'const' + } + ], + sourceType: "script" + } + } + }, + + 'Harmony object pattern, multiline, 2 properties: Property, Property': { + 'const {\n foo: bar,\n baz\n} = {};': { + generateFrom: { + type: 'Program', + body: [ + { + type: 'VariableDeclaration', + declarations: [ + { + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: [ + { + type: 'Property', + method: false, + shorthand: false, + computed: false, + key: { + type: 'Identifier', + name: 'foo' + }, + kind: 'init', + value: { + type: 'Identifier', + name: 'bar' + } + }, + { + type: 'Property', + method: false, + shorthand: true, + computed: false, + key: { + type: 'Identifier', + name: 'baz' + }, + kind: 'init', + value: { + type: 'Identifier', + name: 'baz' + } + }, + ] + }, + init: { + type: 'ObjectExpression', + properties: [] + } + } + ], + kind: 'const' + } + ], + sourceType: "script" + } + } + }, + 'Harmony method property': { 'var obj = { test() { } }': { type: 'Program',