Skip to content

Commit

Permalink
New: spread operator (refs eslint#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamund Ferguson committed Feb 12, 2015
1 parent 7db96b2 commit 0798001
Show file tree
Hide file tree
Showing 52 changed files with 1,811 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ var ast = espree.parse(code, {
// enable parsing of generators/yield
generators: true,

// support the spread operator
spread: true,

// enable React JSX parsing
jsx: true,

Expand Down
35 changes: 24 additions & 11 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,11 @@ function scanPunctuator() {
};
}

// The ... operator only valid in JSX mode for now
if (extra.ecmaFeatures.restParams || (extra.ecmaFeatures.jsx && state.inJSXSpreadAttribute)) {
// The ... operator (spread, restParams, JSX, etc.)
if (extra.ecmaFeatures.spread ||
extra.ecmaFeatures.restParams ||
(extra.ecmaFeatures.jsx && state.inJSXSpreadAttribute)
) {
if (ch1 === "." && ch2 === "." && ch3 === ".") {
index += 3;
return {
Expand Down Expand Up @@ -2338,24 +2341,29 @@ function isLeftHandSide(expr) {

function parseArrayInitialiser() {
var elements = [],
marker = markerCreate();
marker = markerCreate(),
tmp;

expect("[");

while (!match("]")) {
if (match(",")) {
lex();
lex(); // only get here when you have [a,,] or similar
elements.push(null);
} else {
elements.push(parseAssignmentExpression());

if (!match("]")) {
expect(",");
tmp = parseSpreadOrAssignmentExpression();
elements.push(tmp);
if (tmp && tmp.type === astNodeTypes.SpreadElement) {
if (!match("]")) {
throwError({}, Messages.ElementAfterSpreadElement);
}
} else if (!(match("]"))) {
expect(","); // handles the common case of comma-separated values
}
}
}

lex();
expect("]");

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

function parseArguments() {
var args = [];
var args = [], arg;

expect("(");

if (!match(")")) {
while (index < length) {
args.push(parseAssignmentExpression());
arg = parseSpreadOrAssignmentExpression();
args.push(arg);

if (match(")")) {
break;
} else if (arg.type === astNodeTypes.SpreadElement) {
throwError({}, Messages.ElementAfterSpreadElement);
}

expect(",");
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ module.exports = {
// enable parsing of generators/yield
generators: false,

// support the spread operator
spread: false,

// enable super in functions
superInFunctions: false,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
spread: true,
destructuring: true
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
module.exports = {
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "ArrayPattern",
"elements": [
{
"type": "ObjectPattern",
"properties": [
{
"type": "Property",
"key": {
"type": "Identifier",
"name": "a",
"range": [
3,
4
],
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 4
}
}
},
"value": {
"type": "Identifier",
"name": "a",
"range": [
3,
4
],
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 4
}
}
},
"kind": "init",
"method": false,
"shorthand": true,
"computed": false,
"range": [
3,
4
],
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 4
}
}
},
{
"type": "Property",
"key": {
"type": "Identifier",
"name": "b",
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
}
},
"value": {
"type": "Identifier",
"name": "b",
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
}
},
"kind": "init",
"method": false,
"shorthand": true,
"computed": false,
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
}
}
],
"range": [
1,
9
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 9
}
}
},
{
"type": "SpreadElement",
"argument": {
"type": "Identifier",
"name": "c",
"range": [
14,
15
],
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 15
}
}
},
"range": [
11,
15
],
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 15
}
}
}
],
"range": [
0,
16
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
}
}
},
"right": {
"type": "Identifier",
"name": "d",
"range": [
19,
20
],
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 20
}
}
},
"range": [
0,
20
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 20
}
}
},
"range": [
0,
21
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 21
}
}
}
],
"range": [
0,
21
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 21
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{ a, b }, ...c] = d;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
spread: true,
destructuring: true
};
Loading

0 comments on commit 0798001

Please sign in to comment.