Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
buble
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
95
Issues
95
List
Boards
Labels
Service Desk
Milestones
Merge Requests
6
Merge Requests
6
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Rich Harris
buble
Commits
d39b0780
Commit
d39b0780
authored
Nov 16, 2016
by
kzc
Committed by
Marijn Haverbeke
Dec 13, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix object destructuring with computed properties (
#146
)
parent
85ab44ec
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
5 deletions
+78
-5
AssignmentExpression.js
src/program/types/AssignmentExpression.js
+5
-2
destructure.js
src/utils/destructure.js
+5
-3
destructuring.js
test/samples/destructuring.js
+68
-0
No files found.
src/program/types/AssignmentExpression.js
View file @
d39b0780
...
...
@@ -113,7 +113,9 @@ export default class AssignmentExpression extends Node {
else
if
(
pattern
.
type
===
'ObjectPattern'
)
{
const
props
=
pattern
.
properties
;
if
(
props
.
length
==
1
)
{
destructure
(
props
[
0
].
value
,
`
${
ref
}
.
${
props
[
0
].
key
.
name
}
`
,
false
);
const
prop
=
props
[
0
];
const
value
=
prop
.
computed
||
prop
.
key
.
type
!==
'Identifier'
?
`
${
ref
}
[
${
code
.
original
.
substring
(
prop
.
key
.
start
,
prop
.
key
.
end
)}
]`
:
`
${
ref
}
.
${
prop
.
key
.
name
}
`
;
destructure
(
prop
.
value
,
value
,
false
);
}
else
{
if
(
!
mayDuplicate
)
{
...
...
@@ -123,7 +125,8 @@ export default class AssignmentExpression extends Node {
ref
=
temp
;
}
props
.
forEach
(
prop
=>
{
destructure
(
prop
.
value
,
`
${
ref
}
.
${
prop
.
key
.
name
}
`
,
false
);
const
value
=
prop
.
computed
||
prop
.
key
.
type
!==
'Identifier'
?
`
${
ref
}
[
${
code
.
original
.
substring
(
prop
.
key
.
start
,
prop
.
key
.
end
)}
]`
:
`
${
ref
}
.
${
prop
.
key
.
name
}
`
;
destructure
(
prop
.
value
,
value
,
false
);
}
);
}
}
...
...
src/utils/destructure.js
View file @
d39b0780
...
...
@@ -53,7 +53,7 @@ function destructureObjectPattern ( code, scope, node, ref, inline, statementGen
let
c
=
node
.
start
;
node
.
properties
.
forEach
(
prop
=>
{
let
value
=
prop
.
key
.
type
===
'Literal'
?
`
${
ref
}
[
${
prop
.
key
.
raw
}
]`
:
`
${
ref
}
.
${
prop
.
key
.
name
}
`
;
let
value
=
prop
.
computed
||
prop
.
key
.
type
!==
'Identifier'
?
`
${
ref
}
[
${
code
.
original
.
substring
(
prop
.
key
.
start
,
prop
.
key
.
end
)
}
]`
:
`
${
ref
}
.
${
prop
.
key
.
name
}
`
;
handleProperty
(
code
,
scope
,
c
,
prop
.
value
,
value
,
inline
,
statementGenerators
);
c
=
prop
.
end
;
});
...
...
@@ -122,12 +122,14 @@ function handleProperty ( code, scope, c, node, value, inline, statementGenerato
});
node
.
properties
.
forEach
(
prop
=>
{
handleProperty
(
code
,
scope
,
c
,
prop
.
value
,
`
${
ref
}
.
${
prop
.
key
.
name
}
`
,
inline
,
statementGenerators
);
const
value
=
prop
.
computed
||
prop
.
key
.
type
!==
'Identifier'
?
`
${
ref
}
[
${
code
.
original
.
substring
(
prop
.
key
.
start
,
prop
.
key
.
end
)}
]`
:
`
${
ref
}
.
${
prop
.
key
.
name
}
`
;
handleProperty
(
code
,
scope
,
c
,
prop
.
value
,
value
,
inline
,
statementGenerators
);
c
=
prop
.
end
;
});
}
else
{
const
prop
=
node
.
properties
[
0
];
handleProperty
(
code
,
scope
,
c
,
prop
.
value
,
`
${
value
}
.
${
prop
.
key
.
name
}
`
,
inline
,
statementGenerators
);
const
value_suffix
=
prop
.
computed
||
prop
.
key
.
type
!==
'Identifier'
?
`[
${
code
.
original
.
substring
(
prop
.
key
.
start
,
prop
.
key
.
end
)}
]`
:
`.
${
prop
.
key
.
name
}
`
;
handleProperty
(
code
,
scope
,
c
,
prop
.
value
,
`
${
value
}${
value_suffix
}
`
,
inline
,
statementGenerators
);
c
=
prop
.
end
;
}
...
...
test/samples/destructuring.js
View file @
d39b0780
...
...
@@ -332,6 +332,74 @@ module.exports = [
}`
},
{
description
:
'destructured object assignment with computed properties'
,
input
:
`
let one, two, three, four;
({ [FirstProp]: one, [SecondProp]: two = 'Too', 3: three, Fore: four } = x);
`
,
output
:
`
var one, two, three, four;
var assign;
((assign = x, one = assign[FirstProp], two = assign[SecondProp], two = two === void 0 ? 'Too' : two, three = assign[3], four = assign.Fore));
`
},
{
description
:
'destructured object declaration with computed properties'
,
input
:
`
var { [FirstProp]: one, [SecondProp]: two = 'Too', 3: three, Fore: four } = x;
`
,
output
:
`
var one = x[FirstProp];
var two = x[SecondProp]; if ( two === void 0 ) two = 'Too';
var three = x[3];
var four = x.Fore;
`
},
{
description
:
'destructured object with computed properties in parameters'
,
input
:
`
function foo({ [FirstProp]: one, [SecondProp]: two = 'Too', 3: three, Fore: four } = x) {
console.log(one, two, three, four);
}
`
,
output
:
`
function foo(ref) {
if ( ref === void 0 ) ref = x;
var one = ref[FirstProp];
var two = ref[SecondProp]; if ( two === void 0 ) two = 'Too';
var three = ref[3];
var four = ref.Fore;
console.log(one, two, three, four);
}
`
},
{
description
:
'deep matching in parameters with computed properties'
,
input
:
`
function foo ({ [a]: { [b]: c }, d: { 'e': f, [g]: h }, [i + j]: { [k + l]: m, n: o } }) {
console.log( c, f, h, m, o );
}`
,
output
:
`
function foo (ref) {
var c = ref[a][b];
var ref_d = ref.d;
var f = ref_d['e'];
var h = ref_d[g];
var ref_i_j = ref[i + j];
var m = ref_i_j[k + l];
var o = ref_i_j.n;
console.log( c, f, h, m, o );
}`
},
{
description
:
'transpiles destructuring assignment of an array'
,
input
:
`
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment