(when type
(insert (cdr (assoc type '((GET . "get ")
(SET . "set ")
+ (ASYNC . "async ")
(FUNCTION . ""))))))
(when (and (js2-function-node-p right)
(eq 'STAR (js2-function-node-generator-type right)))
(= js2-MUL tt))
(setq previous-token (js2-current-token)
tt (js2-get-prop-name-token)))
- ;; Handle 'get' or 'set' keywords
+ ;; Handle getter, setter and async methods
(let ((prop (js2-current-token-string)))
(when (and (>= js2-language-version 200)
(= js2-NAME tt)
- (or (string= prop "get")
- (string= prop "set"))
+ (member prop '("get" "set" "async"))
(member (js2-peek-token)
(list js2-NAME js2-STRING js2-NUMBER js2-LB)))
(setq previous-token (js2-current-token)
(if (= (js2-token-type previous-token) js2-MUL)
"*"
(js2-token-string previous-token)))))
- (when (or (string= prop "get")
- (string= prop "set"))
+ (when (member prop '("get" "set" "async"))
(js2-set-face (js2-token-beg previous-token)
(js2-token-end previous-token)
- 'font-lock-keyword-face 'record)) ; get/set
+ 'font-lock-keyword-face 'record)) ; get/set/async
(cond
;; method definition: {f() {...}}
((and (= (js2-peek-token) js2-LP)
"Parse method property in an object literal or a class body.
JavaScript syntax is:
- { foo(...) {...}, get foo() {...}, set foo(x) {...}, *foo(...) {...} }
+ { foo(...) {...}, get foo() {...}, set foo(x) {...}, *foo(...) {...},
+ async foo(...) {...} }
and expression closure style is also supported
POS is the start position of the `get' or `set' keyword.
PROP is the `js2-name-node' representing the property name.
TYPE-STRING is a string `get', `set', `*', or nil, indicating a found keyword."
- (let ((type (or (cdr (assoc type-string '(("get" . GET)
- ("set" . SET))))
- 'FUNCTION))
- result end
- (fn (js2-parse-function-expr)))
+ (let* ((type (or (cdr (assoc type-string '(("get" . GET)
+ ("set" . SET)
+ ("async" . ASYNC))))
+ 'FUNCTION))
+ result end
+ (fn (js2-parse-function-expr (eq type 'ASYNC))))
;; it has to be an anonymous function, as we already parsed the name
(if (/= (js2-node-type fn) js2-FUNCTION)
(js2-report-error "msg.bad.prop")
(js2-deftest-parse async-arrow-function-expression
"a = async (b) => { b;\n};")
+(js2-deftest-parse async-method-in-object-literal
+ "({async f() {}});")
+
+(js2-deftest-parse async-method-in-class-body
+ "class C {\n async foo() {}\n}")
+
+(js2-deftest-parse static-async-method-in-class-body
+ "class C {\n static async foo() {}\n}")
+
+(js2-deftest-parse async-method-allow-await
+ "({async f() { await x;\n}});")
+
;;; Await
(js2-deftest-parse await-is-ok "async function foo() {\n await bar();\n}")