Permalink
Browse files

Added support for spread/rest operator

Closes GH-979.
Closes GH-953.
Closes GH-954.
  • Loading branch information...
1 parent 8c1cf12 commit 6ab6645f09062fe4d1efb1e846b37353e53aaeb4 @guyzmo guyzmo committed with valueof Apr 9, 2013
Showing with 89 additions and 19 deletions.
  1. +25 −0 src/stable/jshint.js
  2. +6 −1 src/stable/lex.js
  3. +58 −18 tests/stable/unit/parser.js
View
@@ -2147,6 +2147,17 @@ var JSHINT = (function () {
return this;
});
+ prefix("...", function () {
+ if (!state.option.inESNext()) {
+ warning("W104", this, "spread/rest operator");
+ }
+ if (!state.tokens.next.identifier) {
+ error("E030", state.tokens.next, state.tokens.next.value);
+ }
+ expression(150);
+ return this;
+ });
+
prefix("!", function () {
this.right = expression(150);
this.arity = "unary";
@@ -2527,6 +2538,11 @@ var JSHINT = (function () {
addlabel(t.id, "unused", t.token);
}
}
+ } else if (curr.value === "...") {
+ if (!state.option.inESNext()) {
+ warning("W104", curr, "spread/rest operator");
+ }
+ continue;
} else {
addlabel(curr.value, "unused", curr);
}
@@ -2560,6 +2576,15 @@ var JSHINT = (function () {
addlabel(t.id, "unused", t.token);
}
}
+ } else if (state.tokens.next.value === "...") {
+ if (!state.option.inESNext()) {
+ warning("W104", state.tokens.next, "spread/rest operator");
+ }
+ advance("...");
+ nospace();
+ ident = identifier(true);
+ params.push(ident);
+ addlabel(ident, "unused", state.tokens.curr);
} else {
ident = identifier(true);
params.push(ident);
View
@@ -317,7 +317,12 @@ Lexer.prototype = {
if ((/^[0-9]$/).test(this.peek(1))) {
return null;
}
-
+ if (this.peek(1) === "." && this.peek(2) === ".") {
+ return {
+ type: Token.Punctuator,
+ value: "..."
+ };
+ }
/* falls through */
case "(":
case ")":
@@ -2924,30 +2924,31 @@ exports["fat arrows support"] = function (test) {
run.test(code, {moz: true});
run = TestRun(test)
- .addError(1, "'let' is only available in JavaScript 1.7.")
- .addError(1, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(2, "'let' is only available in JavaScript 1.7.")
- .addError(2, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(3, "'let' is only available in JavaScript 1.7.")
- .addError(3, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(4, "'let' is only available in JavaScript 1.7.")
- .addError(4, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(5, "'let' is only available in JavaScript 1.7.")
- .addError(5, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(6, "'let' is only available in JavaScript 1.7.")
- .addError(6, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(7, "'let' is only available in JavaScript 1.7.")
- .addError(7, "'destructuring expression' is only available in JavaScript 1.7.")
- .addError(7, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
- .addError(11, "'const' is only available in JavaScript 1.7.")
- .addError(13, "'arrow function syntax (=>)' is only available in JavaScript 1.7.");
+ .addError(1, "'let' is only available in JavaScript 1.7.")
+ .addError(1, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(2, "'let' is only available in JavaScript 1.7.")
+ .addError(2, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(3, "'let' is only available in JavaScript 1.7.")
+ .addError(3, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(4, "'let' is only available in JavaScript 1.7.")
+ .addError(4, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(5, "'let' is only available in JavaScript 1.7.")
+ .addError(5, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(6, "'let' is only available in JavaScript 1.7.")
+ .addError(6, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(7, "'let' is only available in JavaScript 1.7.")
+ .addError(7, "'destructuring expression' is only available in JavaScript 1.7.")
+ .addError(7, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(11, "'const' is only available in JavaScript 1.7.")
+ .addError(13, "'arrow function syntax (=>)' is only available in JavaScript 1.7.");
+
run.test(code); // es5
run.test(code, {es3: true});
test.done();
};
-exports["consise methods support"] = function (test) {
+exports["concise methods support"] = function (test) {
var code = [
"var foobar = {",
" foo () {",
@@ -2973,6 +2974,45 @@ exports["consise methods support"] = function (test) {
run.test(code, {es3: true});
test.done();
+};
+
+exports["spread rest operator support"] = function (test) {
+ var code = [
+ // spread operator
+ "function foo(a, b, c) {",
+ " console.log(a, b, c); ",
+ "}",
+ "var args = [ 0, 1, 2 ];",
+ "foo(...args);",
+ // spread operator
+ "let initial = [ 1, 2, 3, 4, 5 ];",
+ "let extended = [ ...initial, 6, 7, 8, 9 ];",
+
+ // rest operator
+ "(function foo(i, j, ...args) {",
+ " return args;",
+ "}());",
+
+ // rest operator on a fat arrow function
+ "let bar = (...args) => args;"
+ ];
+
+ var run = TestRun(test);
+ run.test(code, {esnext: true});
+ run.test(code, {moz: true});
+
+ run = TestRun(test)
+ .addError(5, "'spread/rest operator' is only available in JavaScript 1.7.")
+ .addError(6, "'let' is only available in JavaScript 1.7.")
+ .addError(7, "'let' is only available in JavaScript 1.7.")
+ .addError(7, "'spread/rest operator' is only available in JavaScript 1.7.")
+ .addError(8, "'spread/rest operator' is only available in JavaScript 1.7.")
+ .addError(11, "'let' is only available in JavaScript 1.7.")
+ .addError(11, "'spread/rest operator' is only available in JavaScript 1.7.")
+ .addError(11, "'arrow function syntax (=>)' is only available in JavaScript 1.7.")
+ .addError(11, "'spread/rest operator' is only available in JavaScript 1.7.");
+
+ test.done();
};

0 comments on commit 6ab6645

Please sign in to comment.