From 48885acd20cc146bec97586b61facf443268c611 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sun, 22 Mar 2020 20:47:17 +0300 Subject: [PATCH 1/3] Fixed error during code heneration for object pattern single rest-element property --- escodegen.js | 5 ++++- test/harmony.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/escodegen.js b/escodegen.js index 26e75385..53034a13 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2215,7 +2215,10 @@ 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 { diff --git a/test/harmony.js b/test/harmony.js index 45e9d67a..88afccd2 100644 --- a/test/harmony.js +++ b/test/harmony.js @@ -2548,6 +2548,24 @@ data = { } }, + 'Harmony object pattern single rest-element property': { + '{...a}': { + generateFrom: { + type: 'ObjectPattern', + start: 8, + properties: [ + { + type: 'RestElement', + 'argument': { + type: 'Identifier', + name: 'a' + } + } + ] + } + } + }, + 'Harmony method property': { 'var obj = { test() { } }': { type: 'Program', From 5b635e6e15ef4664edd74dd01504f594cf65601a Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Tue, 2 Jun 2020 00:33:35 +0300 Subject: [PATCH 2/3] Updated harmony object pattern single rest-element property with more real-life AST --- test/harmony.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/test/harmony.js b/test/harmony.js index 88afccd2..2e5551f6 100644 --- a/test/harmony.js +++ b/test/harmony.js @@ -2549,19 +2549,37 @@ data = { }, 'Harmony object pattern single rest-element property': { - '{...a}': { + 'const {...foo} = {};': { generateFrom: { - type: 'ObjectPattern', - start: 8, - properties: [ + type: 'Program', + body: [ { - type: 'RestElement', - 'argument': { - type: 'Identifier', - name: 'a' - } + type: 'VariableDeclaration', + declarations: [ + { + type: 'VariableDeclarator', + id: { + type: 'ObjectPattern', + properties: [ + { + type: 'RestElement', + 'argument': { + type: 'Identifier', + name: 'foo' + } + } + ] + }, + init: { + type: 'ObjectExpression', + properties: [] + } + } + ], + kind: 'const' } - ] + ], + sourceType: "script" } } }, From 478df26e368c11f2373a4e82fc99247ceca1801d Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Tue, 2 Jun 2020 11:01:10 +0300 Subject: [PATCH 3/3] Fixed and added more test for determination of single/multi-line ObjectPattern --- escodegen.js | 5 +- test/harmony.js | 223 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 226 insertions(+), 2 deletions(-) diff --git a/escodegen.js b/escodegen.js index 53034a13..8c0744de 100644 --- a/escodegen.js +++ b/escodegen.js @@ -2224,7 +2224,10 @@ } 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 2e5551f6..2ccf0742 100644 --- a/test/harmony.js +++ b/test/harmony.js @@ -2548,7 +2548,7 @@ data = { } }, - 'Harmony object pattern single rest-element property': { + 'Harmony object pattern, singleline, 1 property: RestElement': { 'const {...foo} = {};': { generateFrom: { type: 'Program', @@ -2584,6 +2584,227 @@ data = { } }, + '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',