[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 3725fcf 35/49: Handle other cases of destructuring assignm
From: |
Dmitry Gutov |
Subject: |
[elpa] master 3725fcf 35/49: Handle other cases of destructuring assignments/initializations |
Date: |
Mon, 16 Jan 2017 15:35:49 +0000 (UTC) |
branch: master
commit 3725fcfe3fef6a5d9776dcb399f949f79de9d3a2
Author: Lele Gaifax <address@hidden>
Commit: Lele Gaifax <address@hidden>
Handle other cases of destructuring assignments/initializations
While this fixes the cases suggested by Dmitry, it basically duplicates the
‘js2-define-destruct-symbols’: most probably that function may be refactored
on top of the new ‘js2--collect-target-name-nodes’.
Also, there is still an (harmless) bug in the way variables are summarized:
with destructuring initializations (some of the) variables appear to used
at the same location.
---
js2-mode.el | 63 +++++++++++++++++++++++++++++++++++++++++--------------
tests/parser.el | 12 +++++++++++
2 files changed, 59 insertions(+), 16 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index e500aef..d6dc70c 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -7129,7 +7129,7 @@ in the cdr of the entry.
(progn
(when (and inition (not (equal (car var) ?P)))
(setcar var inition))
- (when used
+ (when (and used (not (memq symbol (cdr var))))
(push symbol (cdr var))))
;; do not consider the declaration of catch parameter as an usage
(when (and err-var-p used)
@@ -7141,16 +7141,47 @@ in the cdr of the entry.
TARGETS may be either a single js2-name-node, a js2-array-node or a
js2-object-node.
In the first case simply call `js2--add-or-update-symbol' forwarding the same
arguments.
The latter two cases happen in destructuring assignments: recursively update
the symbols."
- (cond
- ((js2-name-node-p targets)
- (js2--add-or-update-symbol targets inition used vars))
- ((js2-array-node-p targets)
- (dolist (elt (js2-array-node-elems targets))
- (when elt
- (js2--add-or-update-symbols elt inition used vars))))
- ((js2-object-node-p targets)
- (dolist (elt (js2-object-node-elems targets))
- (js2--add-or-update-symbols (js2-object-prop-node-right elt) inition
used vars)))))
+ (dolist (elt (js2--collect-target-name-nodes targets))
+ (js2--add-or-update-symbol elt inition used vars)))
+
+(defun js2--collect-target-name-nodes (target)
+ "Collect the target js-name-nodes in a list and return that."
+ ;; TODO: refactor js2-define-destruct-symbols on top of this?
+ (let (targets)
+ (cond
+ ((js2-name-node-p target)
+ (push target targets))
+ ((js2-array-node-p target)
+ (dolist (elt (js2-array-node-elems target))
+ (when elt
+ (setq elt (cond ((js2-infix-node-p elt) ;; default (=)
+ (js2-infix-node-left elt))
+ ((js2-unary-node-p elt) ;; rest (...)
+ (js2-unary-node-operand elt))
+ (t elt)))
+ (setq targets (append (js2--collect-target-name-nodes elt)
targets)))))
+ ((js2-object-node-p target)
+ (dolist (elt (js2-object-node-elems target))
+ (let ((subexpr (cond
+ ((and (js2-infix-node-p elt)
+ (= js2-ASSIGN (js2-infix-node-type elt)))
+ ;; Destructuring with default argument.
+ (js2-infix-node-left elt))
+ ((and (js2-infix-node-p elt)
+ (= js2-COLON (js2-infix-node-type elt)))
+ ;; In regular destructuring {a: aa, b: bb},
+ ;; the var is on the right. In abbreviated
+ ;; destructuring {a, b}, right == left.
+ (js2-infix-node-right elt))
+ ((and (js2-unary-node-p elt)
+ (= js2-TRIPLEDOT (js2-unary-node-type elt)))
+ ;; Destructuring with spread.
+ (js2-unary-node-operand elt)))))
+ (when subexpr
+ (setq targets (append
+ (js2--collect-target-name-nodes subexpr)
+ targets)))))))
+ targets))
(defun js2--classify-variables ()
"Collect and classify variables declared or used within js2-mode-ast.
@@ -7188,9 +7219,7 @@ are ignored."
((js2-assign-node-p node)
;; take note about assignments
- (let ((left (js2-assign-node-left node)))
- (when (js2-name-node-p left)
- (js2--add-or-update-symbols left t nil vars))))
+ (js2--add-or-update-symbols (js2-assign-node-left node) t nil vars))
((js2-prop-get-node-p node)
;; handle x.y.z nodes, considering only x
@@ -7203,9 +7232,11 @@ are ignored."
(let ((parent (js2-node-parent node)))
(when parent
(unless (or (and (js2-var-init-node-p parent) ; handled above
- (eq node (js2-var-init-node-target parent)))
+ (memq node (js2--collect-target-name-nodes
+ (js2-var-init-node-target
parent))))
(and (js2-assign-node-p parent)
- (eq node (js2-assign-node-left parent)))
+ (memq node (js2--collect-target-name-nodes
+ (js2-assign-node-left parent))))
(js2-prop-get-node-p parent))
(let ((used t) inited)
(cond
diff --git a/tests/parser.el b/tests/parser.el
index 1e7fa3e..ac44d99 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -1226,3 +1226,15 @@ the test."
(js2-deftest-classify-variables destructure-object
"function foo(x,y) { var {p: [, w], q: z} = {p: [x, 2, 3], q: y}; }"
'("address@hidden:U" "address@hidden:P" 49 "address@hidden:P" 62
"address@hidden:I" 32 "address@hidden:I" 39))
+
+(js2-deftest-classify-variables destructure-object-shorthand
+ "function foo(x,y) { var {p, q} = {p: x, q: y}; }"
+ ;; FIXME: this is imprecise although harmless: it should really be
+ ;; ("address@hidden:U" "address@hidden:P" 38 "address@hidden:P" 44
"address@hidden:I" 35 "address@hidden:I" 41)
+ '("address@hidden:U" "address@hidden:P" 38 "address@hidden:P" 44
"address@hidden:I" 26 35 "address@hidden:I" 29 41))
+
+(js2-deftest-classify-variables destructure-object-mixed
+ "function foo() { let {a, b, c = 3} = {a: 1, b: 2}; }"
+ ;; FIXME: this is imprecise although harmless: it should really be
+ ;; ("address@hidden:U" "address@hidden:I" 23 39 "address@hidden:I" 45
"address@hidden:I"))
+ '("address@hidden:U" "address@hidden:I" 23 39 "address@hidden:I" 26 45
"address@hidden:I" 29))
- [elpa] master 712dfb0 29/49: Merge pull request #384 from mishoo/master, (continued)
- [elpa] master 712dfb0 29/49: Merge pull request #384 from mishoo/master, Dmitry Gutov, 2017/01/16
- [elpa] master df06798 23/49: Merge pull request #378 from JulianKniephoff/include-externs-locally, Dmitry Gutov, 2017/01/16
- [elpa] master 2a79649 33/49: Fix indentation after regexp in js2-old-indent.el, Dmitry Gutov, 2017/01/16
- [elpa] master f450de0 34/49: Handle destructuring assignments when classifying variables, Dmitry Gutov, 2017/01/16
- [elpa] master 90e37cd 45/49: Merge pull request #397 from lelit/issue393, Dmitry Gutov, 2017/01/16
- [elpa] master 9b63a13 16/49: Special methods should allow keywords as names, Dmitry Gutov, 2017/01/16
- [elpa] master 672e2da 07/49: js2-parse-mul-expr: Parse the right operand as exponentiation too, Dmitry Gutov, 2017/01/16
- [elpa] master a91e357 40/49: Create a separate scope for switch statements, Dmitry Gutov, 2017/01/16
- [elpa] master 4d35da5 30/49: Fix the fix (error detection), Dmitry Gutov, 2017/01/16
- [elpa] master 31a2399 44/49: Mostly cosmetic, split js2--classify-variables in three simpler functions, Dmitry Gutov, 2017/01/16
- [elpa] master 3725fcf 35/49: Handle other cases of destructuring assignments/initializations,
Dmitry Gutov <=
- [elpa] master dad7d09 32/49: feat: add js2-comments-between func, Dmitry Gutov, 2017/01/16
- [elpa] master 523b3cb 38/49: Add a STRICT parameter to js2--collect-declared-symbols, Dmitry Gutov, 2017/01/16
- [elpa] master 1f12517 10/49: Merge pull request #353 from kaushalmodi/js2-jsx-mode-docstring-fix, Dmitry Gutov, 2017/01/16
- [elpa] master 454b242 20/49: Update NEWS.md, Dmitry Gutov, 2017/01/16
- [elpa] master 2d0dc5e 43/49: Cosmetic, reformat to stay within the preferred 80 columns limit, Dmitry Gutov, 2017/01/16
- [elpa] master ff072bf 49/49: Merge commit '03c679eb9914d58d7d9b7afc2036c482a9a01236' from js2-mode, Dmitry Gutov, 2017/01/16
- [elpa] master fbe58dc 18/49: Compute externs on demand, Dmitry Gutov, 2017/01/16
- [elpa] master d3dd406 46/49: fix: copyright year of tests/comsume.el, Dmitry Gutov, 2017/01/16
- [elpa] master cdbdca4 39/49: Rewrite js2--classify-variables, focusing only on name nodes, Dmitry Gutov, 2017/01/16
- [elpa] master 82bbd97 25/49: Move it up, Dmitry Gutov, 2017/01/16