Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit bffd6cc

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Add more tests for destructuring and spread (fixes #306) (#308)
1 parent f7c9246 commit bffd6cc

File tree

65 files changed

+10977
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+10977
-4
lines changed

lib/convert.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ module.exports = function convert(config) {
570570
case SyntaxKind.ArrayLiteralExpression: {
571571

572572
const arrayAssignNode = nodeUtils.findAncestorOfKind(node, SyntaxKind.BinaryExpression);
573+
const arrayIsInForOf = node.parent && node.parent.kind === SyntaxKind.ForOfStatement;
574+
const arrayIsInForIn = node.parent && node.parent.kind === SyntaxKind.ForInStatement;
573575
let arrayIsInAssignment;
574576

575577
if (arrayAssignNode) {
@@ -581,7 +583,7 @@ module.exports = function convert(config) {
581583
}
582584

583585
// TypeScript uses ArrayLiteralExpression in destructuring assignment, too
584-
if (arrayIsInAssignment) {
586+
if (arrayIsInAssignment || arrayIsInForOf || arrayIsInForIn) {
585587
Object.assign(result, {
586588
type: AST_NODE_TYPES.ArrayPattern,
587589
elements: node.elements.map(convertChild)
@@ -925,6 +927,11 @@ module.exports = function convert(config) {
925927
left: arrayItem,
926928
right: convertChild(node.initializer)
927929
});
930+
} else if (node.dotDotDotToken) {
931+
Object.assign(result, {
932+
type: AST_NODE_TYPES.RestElement,
933+
argument: arrayItem
934+
});
928935
} else {
929936
return arrayItem;
930937
}
@@ -1058,16 +1065,31 @@ module.exports = function convert(config) {
10581065

10591066
// Patterns
10601067

1061-
case SyntaxKind.SpreadElement:
1068+
case SyntaxKind.SpreadElement: {
1069+
let type = AST_NODE_TYPES.SpreadElement;
1070+
1071+
if (node.parent &&
1072+
node.parent.parent &&
1073+
node.parent.parent.kind === SyntaxKind.BinaryExpression
1074+
) {
1075+
if (node.parent.parent.left === node.parent) {
1076+
type = AST_NODE_TYPES.RestElement;
1077+
} else if (node.parent.parent.right === node.parent) {
1078+
type = AST_NODE_TYPES.SpreadElement;
1079+
}
1080+
}
1081+
10621082
Object.assign(result, {
1063-
type: AST_NODE_TYPES.SpreadElement,
1083+
type,
10641084
argument: convertChild(node.expression)
10651085
});
10661086
break;
1087+
}
10671088
case SyntaxKind.SpreadAssignment: {
10681089
let type = AST_NODE_TYPES.ExperimentalSpreadProperty;
10691090

10701091
if (node.parent &&
1092+
node.parent.parent &&
10711093
node.parent.parent.kind === SyntaxKind.BinaryExpression
10721094
) {
10731095
if (node.parent.parent.right === node.parent) {

lib/node-utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ function findFirstMatchingChild(node, sourceFile, predicate) {
112112
if (child && predicate(child)) {
113113
return child;
114114
}
115+
116+
const grandChild = findFirstMatchingChild(child, sourceFile, predicate);
117+
if (grandChild) {
118+
return grandChild;
119+
}
115120
}
116121
return undefined;
117122
}
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
module.exports = {
2+
"type": "Program",
3+
"loc": {
4+
"start": {
5+
"line": 1,
6+
"column": 0
7+
},
8+
"end": {
9+
"line": 1,
10+
"column": 11
11+
}
12+
},
13+
"range": [
14+
0,
15+
11
16+
],
17+
"body": [
18+
{
19+
"type": "ExpressionStatement",
20+
"loc": {
21+
"start": {
22+
"line": 1,
23+
"column": 0
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 11
28+
}
29+
},
30+
"range": [
31+
0,
32+
11
33+
],
34+
"expression": {
35+
"type": "ArrowFunctionExpression",
36+
"loc": {
37+
"start": {
38+
"line": 1,
39+
"column": 0
40+
},
41+
"end": {
42+
"line": 1,
43+
"column": 10
44+
}
45+
},
46+
"range": [
47+
0,
48+
10
49+
],
50+
"id": null,
51+
"generator": false,
52+
"expression": true,
53+
"async": false,
54+
"params": [
55+
{
56+
"type": "ArrayPattern",
57+
"loc": {
58+
"start": {
59+
"line": 1,
60+
"column": 1
61+
},
62+
"end": {
63+
"line": 1,
64+
"column": 4
65+
}
66+
},
67+
"range": [
68+
1,
69+
4
70+
],
71+
"elements": [
72+
{
73+
"type": "Identifier",
74+
"loc": {
75+
"start": {
76+
"line": 1,
77+
"column": 2
78+
},
79+
"end": {
80+
"line": 1,
81+
"column": 3
82+
}
83+
},
84+
"range": [
85+
2,
86+
3
87+
],
88+
"name": "y"
89+
}
90+
]
91+
}
92+
],
93+
"body": {
94+
"type": "Identifier",
95+
"loc": {
96+
"start": {
97+
"line": 1,
98+
"column": 9
99+
},
100+
"end": {
101+
"line": 1,
102+
"column": 10
103+
}
104+
},
105+
"range": [
106+
9,
107+
10
108+
],
109+
"name": "x"
110+
}
111+
}
112+
}
113+
],
114+
"sourceType": "script",
115+
"tokens": [
116+
{
117+
"type": "Punctuator",
118+
"value": "(",
119+
"loc": {
120+
"start": {
121+
"line": 1,
122+
"column": 0
123+
},
124+
"end": {
125+
"line": 1,
126+
"column": 1
127+
}
128+
},
129+
"range": [
130+
0,
131+
1
132+
]
133+
},
134+
{
135+
"type": "Punctuator",
136+
"value": "[",
137+
"loc": {
138+
"start": {
139+
"line": 1,
140+
"column": 1
141+
},
142+
"end": {
143+
"line": 1,
144+
"column": 2
145+
}
146+
},
147+
"range": [
148+
1,
149+
2
150+
]
151+
},
152+
{
153+
"type": "Identifier",
154+
"value": "y",
155+
"loc": {
156+
"start": {
157+
"line": 1,
158+
"column": 2
159+
},
160+
"end": {
161+
"line": 1,
162+
"column": 3
163+
}
164+
},
165+
"range": [
166+
2,
167+
3
168+
]
169+
},
170+
{
171+
"type": "Punctuator",
172+
"value": "]",
173+
"loc": {
174+
"start": {
175+
"line": 1,
176+
"column": 3
177+
},
178+
"end": {
179+
"line": 1,
180+
"column": 4
181+
}
182+
},
183+
"range": [
184+
3,
185+
4
186+
]
187+
},
188+
{
189+
"type": "Punctuator",
190+
"value": ")",
191+
"loc": {
192+
"start": {
193+
"line": 1,
194+
"column": 4
195+
},
196+
"end": {
197+
"line": 1,
198+
"column": 5
199+
}
200+
},
201+
"range": [
202+
4,
203+
5
204+
]
205+
},
206+
{
207+
"type": "Punctuator",
208+
"value": "=>",
209+
"loc": {
210+
"start": {
211+
"line": 1,
212+
"column": 6
213+
},
214+
"end": {
215+
"line": 1,
216+
"column": 8
217+
}
218+
},
219+
"range": [
220+
6,
221+
8
222+
]
223+
},
224+
{
225+
"type": "Identifier",
226+
"value": "x",
227+
"loc": {
228+
"start": {
229+
"line": 1,
230+
"column": 9
231+
},
232+
"end": {
233+
"line": 1,
234+
"column": 10
235+
}
236+
},
237+
"range": [
238+
9,
239+
10
240+
]
241+
},
242+
{
243+
"type": "Punctuator",
244+
"value": ";",
245+
"loc": {
246+
"start": {
247+
"line": 1,
248+
"column": 10
249+
},
250+
"end": {
251+
"line": 1,
252+
"column": 11
253+
}
254+
},
255+
"range": [
256+
10,
257+
11
258+
]
259+
}
260+
]
261+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
([y]) => x;

0 commit comments

Comments
 (0)