Skip to content

Commit

Permalink
Make 'of' contextual keyword
Browse files Browse the repository at this point in the history
Refs #53
  • Loading branch information
dgutov committed Jun 14, 2012
1 parent d75365c commit be9054d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
21 changes: 15 additions & 6 deletions js2-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,8 @@ which doesn't seem particularly useful, but Rhino permits it."

(defvar js2-COMMENT 160)
(defvar js2-ENUM 161) ; for "enum" reserved word
(defvar js2-OF 162) ; for "for of" iterators

(defconst js2-num-tokens (1+ js2-OF))
(defconst js2-num-tokens (1+ js2-ENUM))

(defconst js2-debug-print-trees nil)

Expand Down Expand Up @@ -5412,7 +5411,7 @@ into temp buffers."
debugger default delete do
else enum
false finally for function
if in of instanceof import
if in instanceof import
let
new null
return
Expand All @@ -5432,7 +5431,7 @@ into temp buffers."
js2-DEBUGGER js2-DEFAULT js2-DELPROP js2-DO
js2-ELSE
js2-FALSE js2-FINALLY js2-FOR js2-FUNCTION
js2-IF js2-IN js2-OF js2-INSTANCEOF js2-IMPORT
js2-IF js2-IN js2-INSTANCEOF js2-IMPORT
js2-LET
js2-NEW js2-NULL
js2-RETURN
Expand Down Expand Up @@ -7185,6 +7184,16 @@ Returns nil and consumes nothing if MATCH is not the next token."
(js2-consume-token)
t))

(defun js2-match-contextual-kwd (name)
"Consume and return t if next token is `js2-NAME', and its
string is NAME. Returns nil and does nothing otherwise."
(if (or (/= (js2-peek-token) js2-NAME)
(not (string= js2-ts-string name)))
nil
(js2-consume-token)
(js2-record-face 'font-lock-keyword-face)
t))

(defsubst js2-valid-prop-name-token (tt)
(or (= tt js2-NAME)
(when (and js2-allow-keywords-as-property-names
Expand Down Expand Up @@ -7952,7 +7961,7 @@ Parses for, for-in, and for each-in statements."
(setq init (js2-parse-expr)))))
(if (or (js2-match-token js2-IN)
(and (>= js2-language-version 200)
(js2-match-token js2-OF)
(js2-match-contextual-kwd "of")
(setq is-for-of t)))
(setq is-for-in-or-of t
in-pos (- js2-token-beg for-pos)
Expand Down Expand Up @@ -9551,7 +9560,7 @@ Last token peeked should be the initial FOR."
(js2-define-symbol js2-LET (js2-name-node-name iter) pn t))
(if (or (js2-match-token js2-IN)
(and (>= js2-language-version 200)
(js2-match-token js2-OF)
(js2-match-contextual-kwd "of")
(setq forof-p t)))
(setq in-pos (- js2-token-beg pos))
(js2-report-error "msg.in.after.for.name"))
Expand Down
16 changes: 15 additions & 1 deletion tests/ast.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ When BINDINGS are specified, apply them around the test."
"a = {in:1};"
((js2-allow-keywords-as-property-names t)))

;;; Other tests.
;;; Misc.

(js2-deftest-ast parse-array-comp-loop
"[a for (a in [])];")

;;; 'of' contextual keyword.

(js2-deftest-ast parse-array-comp-loop-with-of
"[a for (a of [])];")

(js2-deftest-ast parse-for-of
"for (var a of []) {\n}")

(js2-deftest-ast of-can-be-var-name
"var of = 3;")

(js2-deftest-ast of-can-be-function-name
"function of() {\n}")

0 comments on commit be9054d

Please sign in to comment.