[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 30249fa 07/45: Support parsing the ES6 'static' keyword.
From: |
Dmitry Gutov |
Subject: |
[elpa] master 30249fa 07/45: Support parsing the ES6 'static' keyword. |
Date: |
Mon, 02 Feb 2015 03:18:34 +0000 |
branch: master
commit 30249fa3a8e3d7c8cb72f132985214d6cd52df7d
Author: Stephen Hicks <address@hidden>
Commit: Stephen Hicks <address@hidden>
Support parsing the ES6 'static' keyword.
This is defined in the ClassElement production in ยง14.5 of the draft ES6
spec:
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-class-definitions, and
usage examples are available at e.g.
https://github.com/lukehoban/es6features#classes.
---
js2-mode.el | 51 +++++++++++++++++++++++++++++++++------------------
tests/parser.el | 3 +++
2 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index 64b20f8..769abed 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -663,8 +663,9 @@ which doesn't seem particularly useful, but Rhino permits
it."
(defvar js2-ARROW 163) ; function arrow (=>)
(defvar js2-CLASS 164)
(defvar js2-EXTENDS 165)
+(defvar js2-STATIC 166)
-(defconst js2-num-tokens (1+ js2-EXTENDS))
+(defconst js2-num-tokens (1+ js2-STATIC))
(defconst js2-debug-print-trees nil)
@@ -1964,6 +1965,9 @@ the correct number of ARGS must be provided."
(js2-msg "msg.class.unexpected.comma" ; added by js2-mode
"unexpected ',' between class properties")
+(js2-msg "msg.unexpected.static" ; added by js2-mode
+ "unexpected 'static'")
+
(js2-msg "msg.missing.extends" ; added by js2-mode
"name is required after extends")
@@ -3548,7 +3552,10 @@ optional `js2-expr-node'"
(insert " {")
(dolist (elem elems)
(insert "\n")
- (js2-print-ast elem (1+ i)))
+ (if (js2-node-get-prop elem 'STATIC)
+ (progn (insert (js2-make-pad (1+ i)) "static ")
+ (js2-print-ast elem 0)) ;; TODO(sdh): indentation isn't quite
right
+ (js2-print-ast elem (1+ i))))
(insert "\n" pad "}")))
(defstruct (js2-object-node
@@ -5349,7 +5356,7 @@ into temp buffers."
let
new null
return
- switch
+ static switch
this throw true try typeof
var void
while with
@@ -5369,7 +5376,7 @@ into temp buffers."
js2-LET
js2-NEW js2-NULL
js2-RETURN
- js2-SWITCH
+ js2-STATIC js2-SWITCH
js2-THIS js2-THROW js2-TRUE js2-TRY js2-TYPEOF
js2-VAR
js2-WHILE js2-WITH
@@ -9650,10 +9657,13 @@ If ONLY-OF-P is non-nil, only the 'for (foo of bar)'
form is allowed."
(defun js2-parse-object-literal-elems (&optional class-p)
(let ((pos (js2-current-token-beg))
- tt elems result after-comma
- (continue t))
+ (static nil)
+ (continue t)
+ tt elems elem after-comma)
(while continue
- (setq tt (js2-get-token))
+ (setq static (and class-p (js2-match-token js2-STATIC))
+ tt (js2-get-token)
+ elem nil)
(cond
;; {foo: ...}, {'foo': ...}, {foo, bar, ...},
;; {get foo() {...}}, {set foo(x) {...}}, or {foo(x) {...}}
@@ -9661,23 +9671,22 @@ If ONLY-OF-P is non-nil, only the 'for (foo of bar)'
form is allowed."
((or (js2-valid-prop-name-token tt)
(= tt js2-STRING))
(setq after-comma nil
- result (js2-parse-named-prop tt))
- (if (and (null result)
+ elem (js2-parse-named-prop tt))
+ (if (and (null elem)
(not js2-recover-from-parse-errors))
- (setq continue nil)
- (push result elems)))
+ (setq continue nil)))
;; {[Symbol.iterator]: ...}
((and (= tt js2-LB)
(>= js2-language-version 200))
(let ((expr (js2-parse-expr)))
(js2-must-match js2-RB "msg.missing.computed.rb")
- (setq after-comma nil)
- (push (js2-parse-plain-property expr) elems)))
+ (setq after-comma nil
+ elem (js2-parse-plain-property expr))))
;; {12: x} or {10.7: x}
((= tt js2-NUMBER)
- (setq after-comma nil)
- (push (js2-parse-plain-property (make-js2-number-node)) elems))
- ;; break out of loop, trailing comma
+ (setq after-comma nil
+ elem (js2-parse-plain-property (make-js2-number-node))))
+ ;; Break out of loop, and handle trailing commas.
((or (= tt js2-RC)
(= tt js2-EOF))
(js2-unget-token)
@@ -9689,14 +9698,20 @@ If ONLY-OF-P is non-nil, only the 'for (foo of bar)'
form is allowed."
(js2-report-error "msg.bad.prop")
(unless js2-recover-from-parse-errors
(setq continue nil)))) ; end switch
- ;; handle commas, depending on class-p
+ ;; Handle static for classes' codegen.
+ (if static
+ (if elem (js2-node-set-prop elem 'STATIC t)
+ (js2-report-error "msg.unexpected.static")))
+ ;; Handle commas, depending on class-p.
(let ((comma (js2-match-token js2-COMMA)))
(if class-p
(if comma
(js2-report-error "msg.class.unexpected.comma"))
(if comma
(setq after-comma (js2-current-token-end))
- (setq continue nil))))) ; end loop
+ (setq continue nil))))
+ ;; Append any parsed element.
+ (if elem (push elem elems))) ; end loop
(js2-must-match js2-RC "msg.no.brace.prop")
(nreverse elems)))
diff --git a/tests/parser.el b/tests/parser.el
index 65f3ae4..10b0c13 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -360,6 +360,9 @@ the test."
"class Foo extends {\n}"
:syntax-error "extends")
+(js2-deftest-parse parse-harmony-class-static-method
+ "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
+
(js2-deftest-parse parse-unterminated-class-is-not-okay
"class Foo {\n get bar() { return 42;\n}"
:syntax-error "}")
- [elpa] master updated (d47c872 -> a1ac1e0), Dmitry Gutov, 2015/02/01
- [elpa] master 6037c0f 04/45: Add support for 'u' and 'y' RegExp flags in ES6 mode., Dmitry Gutov, 2015/02/01
- [elpa] master dad2f12 03/45: Support method definitions in object literals., Dmitry Gutov, 2015/02/01
- [elpa] master 926cb45 05/45: Support ES6 computed property names., Dmitry Gutov, 2015/02/01
- [elpa] master 30249fa 07/45: Support parsing the ES6 'static' keyword.,
Dmitry Gutov <=
- [elpa] master c79d426 06/45: Support ES6 class statements/expressions., Dmitry Gutov, 2015/02/01
- [elpa] master 2b0f5ba 08/45: Support parsing the ES6 'super' keyword., Dmitry Gutov, 2015/02/01
- [elpa] master dd344ff 01/45: Support short-hand object literals {foo, bar} everywhere., Dmitry Gutov, 2015/02/01
- [elpa] master b1f7e6c 11/45: Simplify parsing of destructuring literals, Dmitry Gutov, 2015/02/01
- [elpa] master 68b4abd 02/45: Add ES6 class externs, Dmitry Gutov, 2015/02/01
- [elpa] master ee17084 13/45: js2-parse-plain-property: Disallow numbers as abbreviated props, Dmitry Gutov, 2015/02/01
- [elpa] master b19ea52 12/45: js2-parse-named-prop: Highlight externals in abbreviated props, Dmitry Gutov, 2015/02/01
- [elpa] master 31c49e0 14/45: js2-imenu-extension-styles: Turn into defvar, Dmitry Gutov, 2015/02/01
- [elpa] master 9b77d64 15/45: Implement template strings, ignoring substitutions, Dmitry Gutov, 2015/02/01
- [elpa] master 7b3d590 17/45: No need to explicitly apply face to string nodes, Dmitry Gutov, 2015/02/01