Skip to content

Commit 5e9515d

Browse files
committed
Support trailing comma in arrow function params
Fixes #480
1 parent b891ede commit 5e9515d

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

js2-mode.el

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9892,15 +9892,20 @@ If NODE is non-nil, it is the AST node associated with the symbol."
98929892
(while (and (not oneshot)
98939893
(js2-match-token js2-COMMA))
98949894
(setq op-pos (- (js2-current-token-beg) pos)) ; relative
9895-
(setq right (js2-parse-assign-expr)
9896-
left pn
9897-
pn (make-js2-infix-node :type js2-COMMA
9898-
:pos pos
9899-
:len (- js2-ts-cursor pos)
9900-
:op-pos op-pos
9901-
:left left
9902-
:right right))
9903-
(js2-node-add-children pn left right))
9895+
(if (eq (js2-peek-token) js2-RP)
9896+
;; Stop the parser from scanning too far: it's actually
9897+
;; valid syntax in arrow fun arguments, and we don't want
9898+
;; the RP token to get consumed.
9899+
(js2-report-error "msg.syntax")
9900+
(setq right (js2-parse-assign-expr)
9901+
left pn
9902+
pn (make-js2-infix-node :type js2-COMMA
9903+
:pos pos
9904+
:len (- js2-ts-cursor pos)
9905+
:op-pos op-pos
9906+
:left left
9907+
:right right))
9908+
(js2-node-add-children pn left right)))
99049909
pn))
99059910

99069911
(defun js2-parse-assign-expr ()

tests/parser.el

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ the test."
449449
(js2-deftest-parse arrow-function-recovers-from-error
450450
"[(,foo) => 1];" :syntax-error "," :errors-count 6)
451451

452+
(js2-deftest-parse arrow-function-with-trailing-comma-in-arguments
453+
"(a, b = 1,) => { c;\n};"
454+
:reference "(a, b = 1) => { c;\n};")
455+
452456
;;; Automatic semicolon insertion
453457

454458
(js2-deftest-parse no-auto-semi-insertion-after-if

0 commit comments

Comments
 (0)