[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master f450de0 34/49: Handle destructuring assignments when class
From: |
Dmitry Gutov |
Subject: |
[elpa] master f450de0 34/49: Handle destructuring assignments when classifying variables |
Date: |
Mon, 16 Jan 2017 15:35:49 +0000 (UTC) |
branch: master
commit f450de0a2139369bc7a2283884a3c7fe177d4045
Author: Lele Gaifax <address@hidden>
Commit: Lele Gaifax <address@hidden>
Handle destructuring assignments when classifying variables
This should fix #393.
---
js2-mode.el | 24 ++++++++++++++++++++----
tests/parser.el | 8 ++++++++
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index 12a5c6a..e500aef 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -7136,6 +7136,22 @@ in the cdr of the entry.
(setq used nil))
(puthash sym (cons inition (if used (list symbol))) vars))))))
+(defun js2--add-or-update-symbols (targets inition used vars)
+ "Determine the state of each symbol in TARGETS.
+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)))))
+
(defun js2--classify-variables ()
"Collect and classify variables declared or used within js2-mode-ast.
Traverse the whole ast tree returning a summary of the variables
@@ -7168,19 +7184,19 @@ are ignored."
(mapcar #'js2-var-init-node-target
(js2-var-decl-node-kids
(js2-for-in-node-iterator
grandparent)))))))
- (js2--add-or-update-symbol target inited nil vars)))))
+ (js2--add-or-update-symbols target inited nil vars)))))
((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-symbol left t nil vars))))
+ (js2--add-or-update-symbols left t nil vars))))
((js2-prop-get-node-p node)
;; handle x.y.z nodes, considering only x
(let ((left (js2-prop-get-node-left node)))
(when (js2-name-node-p left)
- (js2--add-or-update-symbol left nil t vars))))
+ (js2--add-or-update-symbols left nil t vars))))
((js2-name-node-p node)
;; take note about used variables
@@ -7210,7 +7226,7 @@ are ignored."
(when grandparent
(setq used (js2-return-node-p grandparent)))))
- (js2--add-or-update-symbol node inited used vars))))))))
+ (js2--add-or-update-symbols node inited used vars))))))))
t))
vars))
diff --git a/tests/parser.el b/tests/parser.el
index f6345e1..1e7fa3e 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -1218,3 +1218,11 @@ the test."
(js2-deftest-classify-variables named-wrapper-function
"function foo() { var a; (function bar() { a=42; })(); return a; }"
'("address@hidden:U" "address@hidden:I" 62 "address@hidden:I" 35))
+
+(js2-deftest-classify-variables destructure-array
+ "function foo(x,y) { let [u,v] = [x,y]; }"
+ '("address@hidden:U" "address@hidden:P" 34 "address@hidden:P" 36
"address@hidden:I" 26 "address@hidden:I" 28))
+
+(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))
- [elpa] master bea6942 13/49: Unbind js2-mode-show-node, (continued)
- [elpa] master bea6942 13/49: Unbind js2-mode-show-node, Dmitry Gutov, 2017/01/16
- [elpa] master c0801b2 12/49: Propertize regexp literals like js-mode does, Dmitry Gutov, 2017/01/16
- [elpa] master 9a8aaa9 28/49: Fix length of name in namespace-import nodes, Dmitry Gutov, 2017/01/16
- [elpa] master 44c289f 24/49: Copy edits, Dmitry Gutov, 2017/01/16
- [elpa] master b6d9301 17/49: Merge pull request #377 from XeCycle/memfn-delete, Dmitry Gutov, 2017/01/16
- [elpa] master 64ec0a2 14/49: Allow newline in globals declaration, Dmitry Gutov, 2017/01/16
- [elpa] master 2801681 26/49: Fix "pos" argument in template node constructors, Dmitry Gutov, 2017/01/16
- [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 <=
- [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, 2017/01/16
- [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