[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 31a2399 44/49: Mostly cosmetic, split js2--classify-variab
From: |
Dmitry Gutov |
Subject: |
[elpa] master 31a2399 44/49: Mostly cosmetic, split js2--classify-variables in three simpler functions |
Date: |
Mon, 16 Jan 2017 15:35:50 +0000 (UTC) |
branch: master
commit 31a239931932a180ad3eb5d5f1270aeb19310d1a
Author: Lele Gaifax <address@hidden>
Commit: Lele Gaifax <address@hidden>
Mostly cosmetic, split js2--classify-variables in three simpler functions
---
js2-mode.el | 191 +++++++++++++++++++++++++++++++----------------------------
1 file changed, 101 insertions(+), 90 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index 50409ac..a3fd76a 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -7181,6 +7181,105 @@ When STRICT, signal an error if NODE is not one of the
expected types."
nil))
targets))
+(defun js2--examine-variable (parent node var-init-node)
+ "Examine the usage of the variable NODE, a js2-name-node.
+PARENT is its direct ancestor and VAR-INIT-NODE is the node to be
+examined: return a list of three values, respectively if the
+variable is declared and/or assigned or whether it is simply a
+key of a literal object."
+ (let ((target (js2-var-init-node-target var-init-node))
+ declared assigned object-key)
+ (setq declared (memq node (js2--collect-target-symbols target nil)))
+ ;; Is there an initializer for the declared variable?
+ (when (js2-var-init-node-initializer var-init-node)
+ (setq assigned declared)
+ ;; Determine if the name is actually a literal object key that we shall
+ ;; ignore later
+ (when (and (not declared)
+ (js2-object-prop-node-p parent)
+ (eq node (js2-object-prop-node-left parent)))
+ (setq object-key t)))
+ ;; Maybe this is a for loop and the variable is one of its iterators?
+ (unless assigned
+ (let* ((gp (js2-node-parent parent))
+ (ggp (if gp (js2-node-parent gp))))
+ (when (and ggp (js2-for-in-node-p ggp))
+ (setq assigned (memq node
+ (cl-loop
+ for kid in (js2-var-decl-node-kids
+ (js2-for-in-node-iterator ggp))
+ with syms = '()
+ do
+ (setq syms (append syms
+ (js2--collect-target-symbols
+ (js2-var-init-node-target
kid)
+ nil)))
+ finally return syms))))))
+ (list declared assigned object-key)))
+
+(defun js2--classify-variable (parent node)
+ "Classify the single variable NODE, a js2-name-node."
+ (let ((function-param (and (js2-function-node-p parent)
+ (memq node (js2-function-node-params parent)))))
+ (if (js2-prop-get-node-p parent)
+ ;; If we are within a prop-get, e.g. the "bar" in "foo.bar",
+ ;; just mark "foo" as used
+ (let ((left (js2-prop-get-node-left parent)))
+ (when (js2-name-node-p left)
+ (js2--add-or-update-symbol left nil t vars)))
+ (let ((granparent parent)
+ var-init-node
+ assign-node
+ object-key ; is name actually an object prop key?
+ declared ; is it declared in narrowest scope?
+ assigned ; does it get assigned or initialized?
+ (used (null function-param)))
+ ;; Determine the closest var-init-node and assign-node: this
+ ;; is needed because the name may be within a "destructured"
+ ;; declaration/assignment, so we cannot just take its parent
+ (while (and granparent (not (js2-scope-p granparent)))
+ (cond
+ ((js2-var-init-node-p granparent)
+ (when (null var-init-node)
+ (setq var-init-node granparent)))
+ ((js2-assign-node-p granparent)
+ (when (null assign-node)
+ (setq assign-node granparent))))
+ (setq granparent (js2-node-parent granparent)))
+
+ ;; If we are within a var-init-node, determine if the name is
+ ;; declared and initialized
+ (when var-init-node
+ (let ((result (js2--examine-variable parent node var-init-node)))
+ (setq declared (car result)
+ assigned (cadr result)
+ object-key (car (cddr result)))))
+
+ ;; Ignore literal object keys, which are not really variables
+ (unless object-key
+ (when function-param
+ (setq assigned ?P))
+
+ (when (null assigned)
+ (cond
+ ((js2-for-in-node-p parent)
+ (setq assigned (eq node (js2-for-in-node-iterator parent))
+ used (not assigned)))
+ ((js2-function-node-p parent)
+ (setq assigned t
+ used (js2-wrapper-function-p parent)))
+ (assign-node
+ (setq assigned (memq node
+ (js2--collect-target-symbols
+ (js2-assign-node-left assign-node)
+ nil))
+ used (not assigned)))))
+
+ (when declared
+ (setq used nil))
+
+ (js2--add-or-update-symbol node assigned 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
@@ -7196,97 +7295,9 @@ are ignored."
js2-mode-ast
(lambda (node end-p)
(when (and (null end-p) (js2-name-node-p node))
- (let* ((parent (js2-node-parent node))
- (function-param
- (and (js2-function-node-p parent)
- (memq node (js2-function-node-params parent)))))
+ (let ((parent (js2-node-parent node)))
(when parent
- (if (js2-prop-get-node-p parent)
- ;; If we are within a prop-get, e.g. the "bar" in "foo.bar",
- ;; just mark "foo" as used
- (let ((left (js2-prop-get-node-left parent)))
- (when (js2-name-node-p left)
- (js2--add-or-update-symbol left nil t vars)))
- (let ((granparent parent)
- var-init-node
- assign-node
- object-key ; is name actually an object prop key?
- declared ; is it declared in narrowest scope?
- assigned
- (used (null function-param)))
- ;; Determine the closest var-init-node and assign-node: this
- ;; is needed because the name may be within a "destructured"
- ;; declaration/assignment, so we cannot just take its parent
- (while (and granparent (not (js2-scope-p granparent)))
- (cond
- ((js2-var-init-node-p granparent)
- (when (null var-init-node)
- (setq var-init-node granparent)))
- ((js2-assign-node-p granparent)
- (when (null assign-node)
- (setq assign-node granparent))))
- (setq granparent (js2-node-parent granparent)))
-
- ;; If we are within a var-init-node, determine if the name is
- ;; declared and initialized
- (when var-init-node
- (let ((target (js2-var-init-node-target var-init-node)))
- (setq declared
- (memq node (js2--collect-target-symbols target
nil)))
- ;; Is there an initializer for the declared variable?
- (when (js2-var-init-node-initializer var-init-node)
- (setq assigned declared)
- ;; Determine if the name is actually a literal object
key
- ;; that we shall ignore later
- (when (and (not declared)
- (js2-object-prop-node-p parent)
- (eq node (js2-object-prop-node-left parent)))
- (setq object-key t)))
- ;; Maybe this is a for loop and the variable is one of
- ;; its iterators?
- (unless assigned
- (let* ((gp (js2-node-parent parent))
- (ggp (if gp (js2-node-parent gp))))
- (when (and ggp (js2-for-in-node-p ggp))
- (setq assigned
- (memq
- node
- (cl-loop
- for kid in (js2-var-decl-node-kids
- (js2-for-in-node-iterator ggp))
- with syms = '()
- do (setq
- syms
- (append syms
- (js2--collect-target-symbols
- (js2-var-init-node-target kid)
- nil)))
- finally return syms))))))))
-
- ;; Ignore literal object keys, which are not really variables
- (unless object-key
- (when function-param
- (setq assigned ?P))
-
- (when (null assigned)
- (cond
- ((js2-for-in-node-p parent)
- (setq assigned (eq node (js2-for-in-node-iterator
parent))
- used (not assigned)))
- ((js2-function-node-p parent)
- (setq assigned t
- used (js2-wrapper-function-p parent)))
- (assign-node
- (setq assigned (memq node
- (js2--collect-target-symbols
- (js2-assign-node-left assign-node)
- nil))
- used (not assigned)))))
-
- (when declared
- (setq used nil))
-
- (js2--add-or-update-symbol node assigned used vars)))))))
+ (js2--classify-variable parent node))))
t))
vars))
- [elpa] master 2801681 26/49: Fix "pos" argument in template node constructors, (continued)
- [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, 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 <=
- [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
- [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