Skip to content

Commit 692b9b1

Browse files
committed
Merge pull request #60 from xjamundx/spread
New: spread operator (refs #10)
2 parents 7db96b2 + 0798001 commit 692b9b1

File tree

52 files changed

+1811
-11
lines changed

Some content is hidden

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

52 files changed

+1811
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ var ast = espree.parse(code, {
110110
// enable parsing of generators/yield
111111
generators: true,
112112

113+
// support the spread operator
114+
spread: true,
115+
113116
// enable React JSX parsing
114117
jsx: true,
115118

espree.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,11 @@ function scanPunctuator() {
552552
};
553553
}
554554

555-
// The ... operator only valid in JSX mode for now
556-
if (extra.ecmaFeatures.restParams || (extra.ecmaFeatures.jsx && state.inJSXSpreadAttribute)) {
555+
// The ... operator (spread, restParams, JSX, etc.)
556+
if (extra.ecmaFeatures.spread ||
557+
extra.ecmaFeatures.restParams ||
558+
(extra.ecmaFeatures.jsx && state.inJSXSpreadAttribute)
559+
) {
557560
if (ch1 === "." && ch2 === "." && ch3 === ".") {
558561
index += 3;
559562
return {
@@ -2338,24 +2341,29 @@ function isLeftHandSide(expr) {
23382341

23392342
function parseArrayInitialiser() {
23402343
var elements = [],
2341-
marker = markerCreate();
2344+
marker = markerCreate(),
2345+
tmp;
23422346

23432347
expect("[");
23442348

23452349
while (!match("]")) {
23462350
if (match(",")) {
2347-
lex();
2351+
lex(); // only get here when you have [a,,] or similar
23482352
elements.push(null);
23492353
} else {
2350-
elements.push(parseAssignmentExpression());
2351-
2352-
if (!match("]")) {
2353-
expect(",");
2354+
tmp = parseSpreadOrAssignmentExpression();
2355+
elements.push(tmp);
2356+
if (tmp && tmp.type === astNodeTypes.SpreadElement) {
2357+
if (!match("]")) {
2358+
throwError({}, Messages.ElementAfterSpreadElement);
2359+
}
2360+
} else if (!(match("]"))) {
2361+
expect(","); // handles the common case of comma-separated values
23542362
}
23552363
}
23562364
}
23572365

2358-
lex();
2366+
expect("]");
23592367

23602368
return markerApply(marker, astNodeFactory.createArrayExpression(elements));
23612369
}
@@ -2841,16 +2849,21 @@ function parsePrimaryExpression() {
28412849
// 11.2 Left-Hand-Side Expressions
28422850

28432851
function parseArguments() {
2844-
var args = [];
2852+
var args = [], arg;
28452853

28462854
expect("(");
28472855

28482856
if (!match(")")) {
28492857
while (index < length) {
2850-
args.push(parseAssignmentExpression());
2858+
arg = parseSpreadOrAssignmentExpression();
2859+
args.push(arg);
2860+
28512861
if (match(")")) {
28522862
break;
2863+
} else if (arg.type === astNodeTypes.SpreadElement) {
2864+
throwError({}, Messages.ElementAfterSpreadElement);
28532865
}
2866+
28542867
expect(",");
28552868
}
28562869
}

lib/features.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ module.exports = {
9191
// enable parsing of generators/yield
9292
generators: false,
9393

94+
// support the spread operator
95+
spread: false,
96+
9497
// enable super in functions
9598
superInFunctions: false,
9699

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

0 commit comments

Comments
 (0)