Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: spread operator (refs #10) #60

Merged
merged 1 commit into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move above JSX, trying to keep ES6 features grouped together


// 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