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 11, 2015
1 parent 7db96b2 commit 8d6172c
Show file tree
Hide file tree
Showing 40 changed files with 1,759 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ var ast = espree.parse(code, {
// enable React JSX parsing
jsx: true,

// support the spread operator
spread: true,

// enable return in global scope
globalReturn: true
}
Expand Down
24 changes: 16 additions & 8 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,8 @@ function isLeftHandSide(expr) {

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

expect("[");

Expand All @@ -2347,15 +2348,17 @@ function parseArrayInitialiser() {
lex();
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);
}
}
}
}

lex();
expect("]");

return markerApply(marker, astNodeFactory.createArrayExpression(elements));
}
Expand Down Expand Up @@ -2841,16 +2844,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 @@ -97,6 +97,9 @@ module.exports = {
// React JSX parsing
jsx: false,

// support the spread operator
spread: false,

// allow return statement in global scope
globalReturn: false
};
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 8d6172c

Please sign in to comment.