]> code.delx.au - gnu-emacs-elpa/commitdiff
Support Harmony spread operator, in arrays and calls
authorDmitry Gutov <dgutov@yandex.ru>
Wed, 6 Nov 2013 01:27:02 +0000 (03:27 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Wed, 6 Nov 2013 01:27:02 +0000 (03:27 +0200)
Closes #121

js2-mode.el
tests/parser.el

index 59c0cc8eecc6f5b5f77ee533c6d4684b2f9fff9f..9eef6de71bf1a658e20947be741c4c520c950cbf 100644 (file)
@@ -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
index a9b717d71450681120337c04da995ceaf14956e7..fd8c4c4e5de38ceb3fd7f85c825b3643399bdcad 100644 (file)
@@ -161,6 +161,14 @@ the test."
 (js2-deftest-parse function-with-rest-after-default-parameter\r
   "function foo(a = 1, ...rest) {\n}")\r
 \r
+;;; Spread operator\r
+\r
+(js2-deftest-parse spread-in-array-literal\r
+  "[1, ...[2, 3], 4, ...[5, 6]];")\r
+\r
+(js2-deftest-parse spread-in-function-call\r
+  "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")\r
+\r
 ;;; Arrow functions\r
 \r
 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies\r