Skip to content

Commit

Permalink
Fix position of async functions (expressions and statements)
Browse files Browse the repository at this point in the history
Fixes #471
  • Loading branch information
dgutov committed Dec 19, 2023
1 parent 1f94b09 commit 1a5aeb2
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions js2-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -8032,9 +8032,10 @@ string is NAME. Returns nil and keeps current token otherwise."
(defun js2-match-async-function ()
(when (and (js2-contextual-kwd-p (js2-current-token) "async")
(= (js2-peek-token) js2-FUNCTION))
(js2-record-face 'font-lock-keyword-face)
(js2-get-token)
t))
(let ((async-pos (js2-current-token-beg)))
(js2-record-face 'font-lock-keyword-face)
(js2-get-token)
async-pos)))

(defun js2-match-async-arrow-function ()
(and (js2-contextual-kwd-p (js2-current-token) "async")
Expand Down Expand Up @@ -8503,37 +8504,34 @@ Last token scanned is the close-curly for the function body."
(js2-name-node-name name) pos end)
(js2-add-strict-warning "msg.anon.no.return.value" nil pos end)))))

(defun js2-parse-function-stmt (&optional async-p)
(let ((pos (js2-current-token-beg))
(defun js2-parse-function-stmt (&optional async-pos)
(let ((pos (or async-pos (js2-current-token-beg)))
(star-p (js2-match-token js2-MUL)))
(js2-must-match-name "msg.unnamed.function.stmt")
(let ((name (js2-create-name-node t))
pn member-expr)
(cond
((js2-match-token js2-LP)
(js2-parse-function 'FUNCTION_STATEMENT pos star-p async-p name))
(js2-parse-function 'FUNCTION_STATEMENT pos star-p async-pos name))
(js2-allow-member-expr-as-function-name
(setq member-expr (js2-parse-member-expr-tail nil name))
(js2-parse-highlight-member-expr-fn-name member-expr)
(js2-must-match js2-LP "msg.no.paren.parms")
(setf pn (js2-parse-function 'FUNCTION_STATEMENT pos star-p async-p)
(setf pn (js2-parse-function 'FUNCTION_STATEMENT pos star-p async-pos)
(js2-function-node-member-expr pn) member-expr)
pn)
(t
(js2-report-error "msg.no.paren.parms")
(make-js2-error-node))))))

(defun js2-parse-async-function-stmt ()
(js2-parse-function-stmt t))

(defun js2-parse-function-expr (&optional async-p)
(let ((pos (js2-current-token-beg))
(defun js2-parse-function-expr (&optional async-pos)
(let ((pos (or async-pos (js2-current-token-beg)))
(star-p (js2-match-token js2-MUL))
name)
(when (js2-match-token js2-NAME)
(setq name (js2-create-name-node t)))
(js2-must-match js2-LP "msg.no.paren.parms")
(js2-parse-function 'FUNCTION_EXPRESSION pos star-p async-p name)))
(js2-parse-function 'FUNCTION_EXPRESSION pos star-p async-pos name)))

(defun js2-parse-function-internal (function-type pos star-p &optional async-p name)
(let (fn-node lp)
Expand Down Expand Up @@ -8725,11 +8723,11 @@ node are given relative start positions and correct lengths."
(defun js2-statement-helper ()
(let* ((tt (js2-get-token))
(first-tt tt)
(async-stmt (js2-match-async-function))
(async-pos (js2-match-async-function))
(parser (if (= tt js2-ERROR)
#'js2-parse-semi
(if async-stmt
#'js2-parse-async-function-stmt
(if async-pos
(apply-partially #'js2-parse-function-stmt async-pos)
(aref js2-parsers tt))))
pn)
;; If the statement is set, then it's been told its label by now.
Expand All @@ -8740,7 +8738,7 @@ node are given relative start positions and correct lengths."
;; Don't do auto semi insertion for certain statement types.
(unless (or (memq first-tt js2-no-semi-insertion)
(js2-labeled-stmt-node-p pn)
async-stmt)
async-pos)
(js2-auto-insert-semicolon pn))
pn))

Expand Down Expand Up @@ -9140,7 +9138,8 @@ invalid export statements."
(js2-report-error "msg.mod.export.decl.at.top.level"))
(let ((beg (js2-current-token-beg))
(children (list))
exports-list from-clause declaration default)
exports-list from-clause declaration default
async-pos)
(cond
((js2-match-token js2-MUL)
(setq from-clause (js2-parse-from-clause))
Expand All @@ -9160,10 +9159,10 @@ invalid export statements."
(js2-parse-class-stmt)
(js2-parse-class-expr)))
((js2-match-token js2-NAME)
(if (js2-match-async-function)
(if (setq async-pos (js2-match-async-function))
(if (eq (js2-peek-token) js2-NAME)
(js2-parse-async-function-stmt)
(js2-parse-function-expr t))
(js2-parse-function-stmt async-pos)
(js2-parse-function-expr async-pos))
(js2-unget-token)
(js2-parse-expr)))
((js2-match-token js2-FUNCTION)
Expand All @@ -9177,8 +9176,8 @@ invalid export statements."
(setq declaration (js2-parse-class-stmt)))
((js2-match-token js2-NAME)
(setq declaration
(if (js2-match-async-function)
(js2-parse-async-function-stmt)
(if (setq async-pos (js2-match-async-function))
(js2-parse-function-stmt async-pos)
(js2-unget-token)
(js2-parse-expr))))
((js2-match-token js2-FUNCTION)
Expand Down Expand Up @@ -10698,15 +10697,15 @@ For instance, @[expr], @*::[expr], or ns::[expr]."
"Parse a literal (leaf) expression of some sort.
Includes complex literals such as functions, object-literals,
array-literals, array comprehensions and regular expressions."
(let (tt node)
(let (tt node async-pos)
(setq tt (js2-current-token-type))
(cond
((= tt js2-CLASS)
(js2-parse-class-expr))
((= tt js2-FUNCTION)
(js2-parse-function-expr))
((js2-match-async-function)
(js2-parse-function-expr t))
((setq async-pos (js2-match-async-function))
(js2-parse-function-expr async-pos))
((= tt js2-LB)
(js2-parse-array-comp-or-literal))
((= tt js2-LC)
Expand Down

0 comments on commit 1a5aeb2

Please sign in to comment.