[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 6aba571 021/110: Merge pull request #276 from XeCycle/meth
From: |
Dmitry Gutov |
Subject: |
[elpa] master 6aba571 021/110: Merge pull request #276 from XeCycle/methods |
Date: |
Thu, 23 Jun 2016 01:12:53 +0000 (UTC) |
branch: master
commit 6aba571e22266137bc33cb73ffaa2a0140bf4922
Merge: cef7731 37867af
Author: Dmitry Gutov <address@hidden>
Commit: Dmitry Gutov <address@hidden>
Merge pull request #276 from XeCycle/methods
Rename getter-setter-node to method-node
---
NEWS.md | 4 ++
js2-mode.el | 143 ++++++++++++++++++++++++++++++-------------------------
tests/parser.el | 5 ++
3 files changed, 88 insertions(+), 64 deletions(-)
diff --git a/NEWS.md b/NEWS.md
index 015e8d6..aa04a42 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -4,6 +4,10 @@
* Object properties are highlighted using a different face:
`js2-object-property`, which has no color by default.
+* `js2-getter-setter-node` is renamed to `js2-method-node`, together
+ with its related functions. It already handles generator methods,
+ and will in the future add support for async methods, so the old
+ name will get more confusing.
## 20150909
diff --git a/js2-mode.el b/js2-mode.el
index ba119fa..c98917c 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -3275,7 +3275,7 @@ The `params' field is a Lisp list of nodes. Each node is
either a simple
(defun js2-print-function-node (n i)
(let* ((pad (js2-make-pad i))
- (getter (js2-node-get-prop n 'GETTER_SETTER))
+ (method (js2-node-get-prop n 'METHOD_TYPE))
(name (or (js2-function-node-name n)
(js2-function-node-member-expr n)))
(params (js2-function-node-params n))
@@ -3283,7 +3283,7 @@ The `params' field is a Lisp list of nodes. Each node is
either a simple
(rest-p (js2-function-node-rest-p n))
(body (js2-function-node-body n))
(expr (not (eq (js2-function-node-form n) 'FUNCTION_STATEMENT))))
- (unless (or getter arrow)
+ (unless (or method arrow)
(insert pad "function")
(when (eq (js2-function-node-generator-type n) 'STAR)
(insert "*")))
@@ -3884,6 +3884,30 @@ optional `js2-expr-node'"
(js2-print-list (js2-object-node-elems n))
(insert "}"))
+(cl-defstruct (js2-computed-prop-name-node
+ (:include js2-node)
+ (:constructor nil)
+ (:constructor make-js2-computed-prop-name-node
+ (&key
+ (type js2-LB)
+ expr
+ (pos (js2-current-token-beg))
+ (len (- js2-ts-cursor
+ (js2-current-token-beg))))))
+ "AST node for a `ComputedPropertyName'."
+ expr)
+
+(put 'cl-struct-js2-computed-prop-name-node 'js2-visitor
'js2-visit-computed-prop-name-node)
+(put 'cl-struct-js2-computed-prop-name-node 'js2-printer
'js2-print-computed-prop-name-node)
+
+(defun js2-visit-computed-prop-name-node (n v)
+ (js2-visit-ast (js2-computed-prop-name-node-expr n) v))
+
+(defun js2-print-computed-prop-name-node (n i)
+ (insert (js2-make-pad i) "[")
+ (js2-print-ast (js2-computed-prop-name-node-expr n) 0)
+ (insert "]"))
+
(cl-defstruct (js2-object-prop-node
(:include js2-infix-node)
(:constructor nil)
@@ -3903,53 +3927,38 @@ both fields have the same value.")
(defun js2-print-object-prop-node (n i)
(let* ((left (js2-object-prop-node-left n))
- (right (js2-object-prop-node-right n))
- (computed (not (or (js2-string-node-p left)
- (js2-number-node-p left)
- (js2-name-node-p left)))))
- (insert (js2-make-pad i))
- (if computed
- (insert "["))
- (js2-print-ast left 0)
- (if computed
- (insert "]"))
+ (right (js2-object-prop-node-right n)))
+ (js2-print-ast left i)
(if (not (js2-node-get-prop n 'SHORTHAND))
(progn
(insert ": ")
(js2-print-ast right 0)))))
-(cl-defstruct (js2-getter-setter-node
+(cl-defstruct (js2-method-node
(:include js2-infix-node)
(:constructor nil)
- (:constructor make-js2-getter-setter-node (&key type ; GET,
SET, or FUNCTION
- (pos
js2-ts-cursor)
- len left
right)))
- "AST node for a getter/setter property in an object literal.
-The `left' field is the `js2-name-node' naming the getter/setter prop.
+ (:constructor make-js2-method-node (&key type ; GET, SET, or
FUNCTION
+ (pos js2-ts-cursor)
+ len left right)))
+ "AST node for a method in an object literal or a class body.
+The `left' field is the `js2-name-node' naming the method.
The `right' field is always an anonymous `js2-function-node' with a node
-property `GETTER_SETTER' set to js2-GET, js2-SET, or js2-FUNCTION. ")
+property `METHOD_TYPE' set to js2-GET, js2-SET, or js2-FUNCTION. ")
-(put 'cl-struct-js2-getter-setter-node 'js2-visitor 'js2-visit-infix-node)
-(put 'cl-struct-js2-getter-setter-node 'js2-printer 'js2-print-getter-setter)
+(put 'cl-struct-js2-method-node 'js2-visitor 'js2-visit-infix-node)
+(put 'cl-struct-js2-method-node 'js2-printer 'js2-print-method)
-(defun js2-print-getter-setter (n i)
+(defun js2-print-method (n i)
(let* ((pad (js2-make-pad i))
- (left (js2-getter-setter-node-left n))
- (right (js2-getter-setter-node-right n))
- (computed (not (or (js2-string-node-p left)
- (js2-number-node-p left)
- (js2-name-node-p left)))))
+ (left (js2-method-node-left n))
+ (right (js2-method-node-right n)))
(insert pad)
(if (/= (js2-node-type n) js2-FUNCTION)
(insert (if (= (js2-node-type n) js2-GET) "get " "set ")))
(when (and (js2-function-node-p right)
(eq 'STAR (js2-function-node-generator-type right)))
(insert "*"))
- (when computed
- (insert "["))
(js2-print-ast left 0)
- (when computed
- (insert "]"))
(js2-print-ast right 0)))
(cl-defstruct (js2-prop-get-node
@@ -10577,10 +10586,12 @@ If ONLY-OF-P is non-nil, only the 'for (foo of bar)'
form is allowed."
(defun js2-property-key-string (property-node)
"Return the key of PROPERTY-NODE (a `js2-object-prop-node' or
-`js2-getter-setter-node') as a string, or nil if it can't be
+`js2-method-node') as a string, or nil if it can't be
represented as a string (e.g., the key is computed by an
expression)."
(let ((key (js2-infix-node-left property-node)))
+ (when (js2-computed-prop-name-node-p key)
+ (setq key (js2-computed-prop-name-node-expr key)))
(cond
((js2-name-node-p key)
(js2-name-node-name key))
@@ -10664,10 +10675,10 @@ expression)."
;; Check if the property is a duplicate.
(string= previous-elem-key-string elem-key-string)
;; But make an exception for getter / setter pairs.
- (not (and (js2-getter-setter-node-p elem)
- (js2-getter-setter-node-p previous-elem)
- (/= (js2-getter-setter-node-type elem)
- (js2-getter-setter-node-type
previous-elem))))))
+ (not (and (js2-method-node-p elem)
+ (js2-method-node-p previous-elem)
+ (/= (js2-method-node-type elem)
+ (js2-method-node-type
previous-elem))))))
elems))
(js2-report-error "msg.dup.obj.lit.prop.strict"
elem-key-string
@@ -10681,24 +10692,7 @@ expression)."
(defun js2-parse-named-prop (tt pos previous-token)
"Parse a name, string, or getter/setter object property.
When `js2-is-in-destructuring' is t, forms like {a, b, c} will be permitted."
- (let ((key (cond
- ;; Literal string keys: {'foo': 'bar'}
- ((= tt js2-STRING)
- (make-js2-string-node))
- ;; Handle computed keys: {[Symbol.iterator]: ...}, *[1+2]()
{...}},
- ;; {[foo + bar]() { ... }}, {[get ['x' + 1]() {...}}
- ((and (= tt js2-LB)
- (>= js2-language-version 200))
- (prog1 (js2-parse-expr)
- (js2-must-match js2-RB "msg.missing.computed.rb")))
- ;; Numeric keys: {12: 'foo'}, {10.7: 'bar'}
- ((= tt js2-NUMBER)
- (make-js2-number-node))
- ;; Unquoted names: {foo: 12}
- ((= tt js2-NAME)
- (js2-create-name-node))
- ;; Anything else is an error
- (t (js2-report-error "msg.bad.prop"))))
+ (let ((key (js2-parse-prop-name tt))
(prop (and previous-token (js2-token-string previous-token)))
(property-type (when previous-token
(if (= (js2-token-type previous-token) js2-MUL)
@@ -10715,7 +10709,7 @@ When `js2-is-in-destructuring' is t, forms like {a, b,
c} will be permitted."
(>= js2-language-version 200))
(when (js2-name-node-p key) ; highlight function name properties
(js2-record-face 'font-lock-function-name-face))
- (js2-parse-getter-setter-prop pos key property-type))
+ (js2-parse-method-prop pos key property-type))
;; regular prop
(t
(let ((beg (js2-current-token-beg))
@@ -10734,6 +10728,27 @@ When `js2-is-in-destructuring' is t, forms like {a, b,
c} will be permitted."
'record)
expr)))))
+(defun js2-parse-prop-name (tt)
+ (cond
+ ;; Literal string keys: {'foo': 'bar'}
+ ((= tt js2-STRING)
+ (make-js2-string-node))
+ ;; Handle computed keys: {[Symbol.iterator]: ...}, *[1+2]() {...}},
+ ;; {[foo + bar]() { ... }}, {[get ['x' + 1]() {...}}
+ ((and (= tt js2-LB)
+ (>= js2-language-version 200))
+ (make-js2-computed-prop-name-node
+ :expr (prog1 (js2-parse-assign-expr)
+ (js2-must-match js2-RB "msg.missing.computed.rb"))))
+ ;; Numeric keys: {12: 'foo'}, {10.7: 'bar'}
+ ((= tt js2-NUMBER)
+ (make-js2-number-node))
+ ;; Unquoted names: {foo: 12}
+ ((= tt js2-NAME)
+ (js2-create-name-node))
+ ;; Anything else is an error
+ (t (js2-report-error "msg.bad.prop"))))
+
(defun js2-parse-plain-property (prop)
"Parse a non-getter/setter property in an object literal.
PROP is the node representing the property: a number, name,
@@ -10775,11 +10790,11 @@ string or expression."
(js2-node-add-children result prop expr)
result))))
-(defun js2-parse-getter-setter-prop (pos prop type-string)
- "Parse getter or setter property in an object literal.
+(defun js2-parse-method-prop (pos prop type-string)
+ "Parse method property in an object literal or a class body.
JavaScript syntax is:
- { get foo() {...}, set foo(x) {...} }
+ { foo(...) {...}, get foo() {...}, set foo(x) {...}, *foo(...) {...} }
and expression closure style is also supported
@@ -10799,15 +10814,15 @@ TYPE-STRING is a string `get', `set', `*', or nil,
indicating a found keyword."
(js2-report-error "msg.bad.prop")
(if (cl-plusp (length (js2-function-name fn)))
(js2-report-error "msg.bad.prop")))
- (js2-node-set-prop fn 'GETTER_SETTER type) ; for codegen
+ (js2-node-set-prop fn 'METHOD_TYPE type) ; for codegen
(when (string= type-string "*")
(setf (js2-function-node-generator-type fn) 'STAR))
(setq end (js2-node-end fn)
- result (make-js2-getter-setter-node :type type
- :pos pos
- :len (- end pos)
- :left prop
- :right fn))
+ result (make-js2-method-node :type type
+ :pos pos
+ :len (- end pos)
+ :left prop
+ :right fn))
(js2-node-add-children result prop fn)
result))
diff --git a/tests/parser.el b/tests/parser.el
index 17519ca..650bac8 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -189,6 +189,11 @@ the test."
(js2-deftest-parse object-literal-method
"var x = {f(y) { return y;\n}};")
+(js2-deftest object-literal-method-own-name-in-scope "({f(){f();}});"
+ (js2-mode)
+ (should (equal '("msg.undeclared.variable" "f")
+ (caar js2-parsed-warnings))))
+
(js2-deftest-parse object-literal-getter-method
"var x = {get f() { return 42;\n}};")
- [elpa] master bfb83dd 017/110: Refactor: unify computed property name handling, (continued)
- [elpa] master bfb83dd 017/110: Refactor: unify computed property name handling, Dmitry Gutov, 2016/06/22
- [elpa] master c63c700 029/110: Update MELPA URL, Dmitry Gutov, 2016/06/22
- [elpa] master a28a648 007/110: Remove await-node for 'awayt' and use unary-node, Dmitry Gutov, 2016/06/22
- [elpa] master 2cf52e9 015/110: js2-parse-highlight-prop-get: Take care now to overwrite the js2-function-call face, Dmitry Gutov, 2016/06/22
- [elpa] master 3603bc4 018/110: Add test on shorthand method name and scoping, Dmitry Gutov, 2016/06/22
- [elpa] master 0555a8a 024/110: Support initializer in destructuring, Dmitry Gutov, 2016/06/22
- [elpa] master 1f95553 033/110: Add js2-jsx-mode usage instructions, Dmitry Gutov, 2016/06/22
- [elpa] master 47b215d 011/110: Define js2-object-property face, Dmitry Gutov, 2016/06/22
- [elpa] master c0e0309 022/110: Allow semicolons in a class body, Dmitry Gutov, 2016/06/22
- [elpa] master df3f97f 030/110: Add js2-jsx-mode, Dmitry Gutov, 2016/06/22
- [elpa] master 6aba571 021/110: Merge pull request #276 from XeCycle/methods,
Dmitry Gutov <=
- [elpa] master 221282a 028/110: Merge pull request #280 from XeCycle/binding-pattern-initializer, Dmitry Gutov, 2016/06/22
- [elpa] master 2e7fdb3 012/110: Also highlight property lookups with the same face, Dmitry Gutov, 2016/06/22
- [elpa] master b1234ca 013/110: Don't misindent generator methods, Dmitry Gutov, 2016/06/22
- [elpa] master d82a58d 019/110: Refactor: rename getter-setter-node to method-node, Dmitry Gutov, 2016/06/22
- [elpa] master 263852b 026/110: Add melpa badges, Dmitry Gutov, 2016/06/22
- [elpa] master 98433fb 036/110: `js2-print-json-path' added, Dmitry Gutov, 2016/06/22
- [elpa] master f9d28d3 052/110: Adjust js2-print-export-node, Dmitry Gutov, 2016/06/22
- [elpa] master d307285 039/110: Move method type handling to property METHOD_TYPE, Dmitry Gutov, 2016/06/22
- [elpa] master f652d17 044/110: Add setImmediate and clearImmediate to js2-node-externs, Dmitry Gutov, 2016/06/22
- [elpa] master e0972fb 048/110: s/will/would, Dmitry Gutov, 2016/06/22