From: Dmitry Gutov Date: Wed, 6 Nov 2013 01:27:02 +0000 (+0200) Subject: Support Harmony spread operator, in arrays and calls X-Git-Url: https://code.delx.au/gnu-emacs-elpa/commitdiff_plain/65fb725443ac6aa875094e97095fb581d3ec1525 Support Harmony spread operator, in arrays and calls Closes #121 --- diff --git a/js2-mode.el b/js2-mode.el index 59c0cc8ee..9eef6de71 100644 --- a/js2-mode.el +++ b/js2-mode.el @@ -3200,6 +3200,7 @@ The type field inherited from `js2-node' holds the operator." (cons js2-BITNOT "~") (cons js2-POS "+") ; unary plus (cons js2-NEG "-") ; unary minus + (cons js2-TRIPLEDOT "...") (cons js2-SHEQ "===") ; shallow equality (cons js2-SHNE "!==") ; shallow inequality (cons js2-ASSIGN "=") @@ -3252,7 +3253,7 @@ The type field holds the actual assignment operator.") len operand))) "AST node type for unary operator nodes. The type field can be NOT, BITNOT, POS, NEG, INC, DEC, -TYPEOF, or DELPROP. For INC or DEC, a 'postfix node +TYPEOF, DELPROP or TRIPLEDOT. For INC or DEC, a 'postfix node property is added if the operator follows the operand." operand) ; a `js2-node' expression @@ -8833,9 +8834,14 @@ Returns the list in reverse order. Consumes the right-paren token." (let (result) (unless (js2-match-token js2-RP) (loop do - (if (= (js2-peek-token) js2-YIELD) - (js2-report-error "msg.yield.parenthesized")) - (push (js2-parse-assign-expr) result) + (let ((tt (js2-get-token))) + (if (= tt js2-YIELD) + (js2-report-error "msg.yield.parenthesized")) + (if (and (= tt js2-TRIPLEDOT) + (>= js2-language-version 200)) + (push (js2-make-unary tt 'js2-parse-assign-expr) result) + (js2-unget-token) + (push (js2-parse-assign-expr) result))) while (js2-match-token js2-COMMA)) (js2-must-match js2-RP "msg.no.paren.arg") @@ -9278,13 +9284,17 @@ array-literals, array comprehensions and regular expressions." (js2-unget-token) (setf continue nil pn (js2-parse-array-comprehension (car elems) pos))) - ;; another element (t (unless after-lb-or-comma (js2-report-error "msg.no.bracket.arg")) - (js2-unget-token) - (push (js2-parse-assign-expr) elems) + (if (and (= tt js2-TRIPLEDOT) + (>= js2-language-version 200)) + ;; spread operator + (push (js2-make-unary tt 'js2-parse-assign-expr) + elems) + (js2-unget-token) + (push (js2-parse-assign-expr) elems)) (setq after-lb-or-comma nil after-comma nil)))) (unless js2-is-in-destructuring diff --git a/tests/parser.el b/tests/parser.el index a9b717d71..fd8c4c4e5 100644 --- a/tests/parser.el +++ b/tests/parser.el @@ -161,6 +161,14 @@ the test." (js2-deftest-parse function-with-rest-after-default-parameter "function foo(a = 1, ...rest) {\n}") +;;; Spread operator + +(js2-deftest-parse spread-in-array-literal + "[1, ...[2, 3], 4, ...[5, 6]];") + +(js2-deftest-parse spread-in-function-call + "f(3, ...[t(2), t(3)], 42, ...[t(4)]);") + ;;; Arrow functions (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies