Skip to content

Commit

Permalink
fixed multiline decision for object patterns with rest elements (#408)
Browse files Browse the repository at this point in the history
Co-authored-by: Timofey Kachalov <sanex3339@users.noreply.github.com>
Co-authored-by: sanex3339 <yarabotayuvyandex3339>
  • Loading branch information
sanex3339 and sanex3339 committed Jun 3, 2020
1 parent a3b6718 commit c62cbe2
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 2 deletions.
10 changes: 8 additions & 2 deletions escodegen.js
Expand Up @@ -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;
}
Expand Down
257 changes: 257 additions & 0 deletions test/harmony.js
Expand Up @@ -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',
Expand Down

0 comments on commit c62cbe2

Please sign in to comment.