[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 4125b49a 04/22: Merge pull request #487 from lelit/classif
From: |
Dmitry Gutov |
Subject: |
[elpa] master 4125b49a 04/22: Merge pull request #487 from lelit/classify-imported |
Date: |
Tue, 19 Feb 2019 06:06:45 -0500 (EST) |
branch: master
commit 4125b49a6982a3eb36674962160118250daf665e
Merge: 8bb1907 47dee19
Author: Dmitry Gutov <address@hidden>
Commit: GitHub <address@hidden>
Merge pull request #487 from lelit/classify-imported
Classify imported variables
---
js2-mode.el | 115 +++++++++++++++++++++++++++++++-------------------------
tests/parser.el | 30 ++++++++++++++-
2 files changed, 93 insertions(+), 52 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index bd691c5..5dcb2a6 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -7192,58 +7192,71 @@ key of a literal object."
(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)
+ ;; If the node is the external name of an export-binding-node, and
+ ;; it is different from the local name, ignore it
+ (when (or (not (js2-export-binding-node-p parent))
+ (not (and (eq (js2-export-binding-node-extern-name parent)
node)
+ (not (eq (js2-export-binding-node-local-name parent)
node)))))
+ (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-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-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)))
+ ((js2-export-binding-node-p parent)
+ (if (js2-import-clause-node-p (js2-node-parent parent))
+ (setq declared t
+ assigned t)
+ (setq used t)))
+ ((js2-namespace-import-node-p parent)
+ (setq assigned t
+ used nil))
+ (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.
diff --git a/tests/parser.el b/tests/parser.el
index 1853f03..9137940 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -635,7 +635,7 @@ the test."
(should name)
(should (equal "default" (js2-name-node-name name))))))
-(js2-deftest parse-namepsace-import "* as lib;"
+(js2-deftest parse-namespace-import "* as lib;"
(js2-init-scanner)
(should (js2-match-token js2-MUL))
(let ((namespace-import (js2-parse-namespace-import)))
@@ -1358,6 +1358,34 @@ the test."
"function foo() { let {foo: missing = 10} = {}; }"
'("address@hidden:U" "address@hidden:U"))
+(js2-deftest-classify-variables import-unused
+ "import foo from 'module';"
+ '("address@hidden:U"))
+
+(js2-deftest-classify-variables named-import-unused
+ "import foo as bar from 'module';"
+ '("address@hidden:U"))
+
+(js2-deftest-classify-variables import-unused-many
+ "import {a,b} from 'module';"
+ '("address@hidden:U" "address@hidden:U"))
+
+(js2-deftest-classify-variables named-import-unused-many
+ "import {a as b, c as d} from 'module';"
+ '("address@hidden:U" "address@hidden:U"))
+
+(js2-deftest-classify-variables import-export
+ "import foo from 'module'; export {foo}"
+ '("address@hidden:I" 35))
+
+(js2-deftest-classify-variables import-namespace-unused
+ "import * as foo from 'module';"
+ '("address@hidden:U"))
+
+(js2-deftest-classify-variables import-namespace-used
+ "import * as foo from 'module'; function bar() { return foo.x; }"
+ '("address@hidden:I" 56 "address@hidden:U"))
+
;; Side effects
(js2-deftest no-side-effects-at-top-level
- [elpa] master 3bea9ab 14/22: Merge pull request #498 from wyuenho/fix-short-name-for-emacs26, (continued)
- [elpa] master 3bea9ab 14/22: Merge pull request #498 from wyuenho/fix-short-name-for-emacs26, Dmitry Gutov, 2019/02/19
- [elpa] master bf61e6f 10/22: Fix #495 Aggressively reparse during imenu-create-index when possible, Dmitry Gutov, 2019/02/19
- [elpa] master 47dee19 03/22: Properly classify imported symbols, Dmitry Gutov, 2019/02/19
- [elpa] master dee4c47 07/22: Merge pull request #491 from felipeochoa/forward-sexp-in-str, Dmitry Gutov, 2019/02/19
- [elpa] master 40a43b1 13/22: Fix broken js2-node-short-name for Emacs 26, Dmitry Gutov, 2019/02/19
- [elpa] master 8bb1907 01/22: Silence "variable ‘_’ not left unused", Dmitry Gutov, 2019/02/19
- [elpa] master 2ed3cc0 17/22: Merge pull request #501 from thorrr/obj-literal-fix, Dmitry Gutov, 2019/02/19
- [elpa] master 58006df 20/22: Release new version, Dmitry Gutov, 2019/02/19
- [elpa] master 381545b 08/22: Make js2-additional-externs safe as a local variable, Dmitry Gutov, 2019/02/19
- [elpa] master ed955e7 21/22: Update NEWS, Dmitry Gutov, 2019/02/19
- [elpa] master 4125b49a 04/22: Merge pull request #487 from lelit/classify-imported,
Dmitry Gutov <=
- [elpa] master 1b0e174 12/22: Don't record js2-xml-ref-node's in js2-recorder-identifiers, Dmitry Gutov, 2019/02/19
- [elpa] master 5165f4d 19/22: Merge pull request #507 from piotr-yuxuan/patch-1, Dmitry Gutov, 2019/02/19
- [elpa] master 4de0e2d 05/22: Don't warn about trailing commas by default, Dmitry Gutov, 2019/02/19
- [elpa] master f863e08 18/22: Update README.md, Dmitry Gutov, 2019/02/19
- [elpa] master d76b4df 09/22: Merge pull request #492 from felipeochoa/additional-externs-safe-local, Dmitry Gutov, 2019/02/19
- [elpa] master b92b3ae 15/22: allow testing for specific errors and add test for object property, Dmitry Gutov, 2019/02/19
- [elpa] master 76b7f45 16/22: fix bug jumping to function def in shorthand property:value literal, Dmitry Gutov, 2019/02/19
- [elpa] master 19be337 11/22: Merge pull request #496 from wyuenho/bug495, Dmitry Gutov, 2019/02/19
- [elpa] master c5cc951 22/22: Merge commit 'ed955e7f1608cfd2d2713129d65f5fd734842ae4' from js2-mode, Dmitry Gutov, 2019/02/19