branch: externals/js2-mode commit 5e9515dff26ed2fa382c97b0b67cdd370f7859f4 Author: Dmitry Gutov <dgu...@yandex.ru> Commit: Dmitry Gutov <dgu...@yandex.ru>
Support trailing comma in arrow function params Fixes #480 --- js2-mode.el | 23 ++++++++++++++--------- tests/parser.el | 4 ++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/js2-mode.el b/js2-mode.el index 685cdf9..8327ca3 100644 --- a/js2-mode.el +++ b/js2-mode.el @@ -9892,15 +9892,20 @@ If NODE is non-nil, it is the AST node associated with the symbol." (while (and (not oneshot) (js2-match-token js2-COMMA)) (setq op-pos (- (js2-current-token-beg) pos)) ; relative - (setq right (js2-parse-assign-expr) - left pn - pn (make-js2-infix-node :type js2-COMMA - :pos pos - :len (- js2-ts-cursor pos) - :op-pos op-pos - :left left - :right right)) - (js2-node-add-children pn left right)) + (if (eq (js2-peek-token) js2-RP) + ;; Stop the parser from scanning too far: it's actually + ;; valid syntax in arrow fun arguments, and we don't want + ;; the RP token to get consumed. + (js2-report-error "msg.syntax") + (setq right (js2-parse-assign-expr) + left pn + pn (make-js2-infix-node :type js2-COMMA + :pos pos + :len (- js2-ts-cursor pos) + :op-pos op-pos + :left left + :right right)) + (js2-node-add-children pn left right))) pn)) (defun js2-parse-assign-expr () diff --git a/tests/parser.el b/tests/parser.el index 23c6ebe..78115f3 100644 --- a/tests/parser.el +++ b/tests/parser.el @@ -449,6 +449,10 @@ the test." (js2-deftest-parse arrow-function-recovers-from-error "[(,foo) => 1];" :syntax-error "," :errors-count 6) +(js2-deftest-parse arrow-function-with-trailing-comma-in-arguments + "(a, b = 1,) => { c;\n};" + :reference "(a, b = 1) => { c;\n};") + ;;; Automatic semicolon insertion (js2-deftest-parse no-auto-semi-insertion-after-if