[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 2c62db1 10/16: Ensure Node.js variable is global.
From: |
Jackson Ray Hamilton |
Subject: |
[elpa] master 2c62db1 10/16: Ensure Node.js variable is global. |
Date: |
Sun, 18 Oct 2015 11:21:48 +0000 |
branch: master
commit 2c62db146349231954951a7c0b034a9696822d2a
Author: Jackson Hamilton <address@hidden>
Commit: Jackson Hamilton <address@hidden>
Ensure Node.js variable is global.
---
context-coloring.el | 149 +++++++++++++++++++++--------------------
test/context-coloring-test.el | 3 +-
2 files changed, 79 insertions(+), 73 deletions(-)
diff --git a/context-coloring.el b/context-coloring.el
index a2cd88f..1f617b9 100644
--- a/context-coloring.el
+++ b/context-coloring.el
@@ -298,78 +298,6 @@ are scoped to a file (as in Node.js), set this to `1'."
:group 'context-coloring)
-;;; Node.js colorization
-
-(defconst context-coloring-node-comment-regexp
- (concat
- ;; Ensure the "//" or "/*" comment starts with the directive.
- "\\(//[[:space:]]*\\|/\\*[[:space:]]*\\)"
- ;; Support multiple directive formats.
- "\\("
- ;; JSLint and JSHint support a JSON-like format.
- "\\(jslint\\|jshint\\)[[:space:]].*?node:[[:space:]]*true"
- "\\|"
- ;; ESLint just specifies the option name.
- "eslint-env[[:space:]].*?node"
- "\\)")
- "Match a comment body hinting at a Node.js program.")
-
-(defun context-coloring-node-program-p ()
- "Guess whether the current file is a Node.js program."
- (or
- ;; A shebang is a pretty obvious giveaway.
- (string-equal
- "node"
- (save-excursion
- (goto-char (point-min))
- (when (looking-at auto-mode-interpreter-regexp)
- (match-string 2))))
- ;; Otherwise, perform static analysis.
- (catch 'node-program-p
- (js2-visit-ast
- js2-mode-ast
- (lambda (node end-p)
- (when (null end-p)
- (when
- (cond
- ;; Infer based on inline linter configuration.
- ((js2-comment-node-p node)
- (string-match-p
- context-coloring-node-comment-regexp
- (js2-node-string node)))
- ;; Infer based on the prescence of certain variables.
- ((and (js2-name-node-p node)
- (let ((parent (js2-node-parent node)))
- (not (and (js2-object-prop-node-p parent)
- (eq node (js2-object-prop-node-left
parent))))))
- (let ((name (js2-name-node-name node))
- (parent (js2-node-parent node)))
- (cond
- ;; Check whether this is "exports.something" or
- ;; "module.exports".
- ((js2-prop-get-node-p parent)
- (and
- (eq node (js2-prop-get-node-left parent))
- (or (string-equal name "exports")
- (let* ((property (js2-prop-get-node-right parent))
- (property-name (js2-name-node-name property)))
- (or (and (string-equal name "module")
- (string-equal property-name
"exports")))))))
- ;; Check whether it's a "require('module')" call.
- ((js2-call-node-p parent)
- (or (string-equal name "require")))))))
- (throw 'node-program-p t))
- ;; The `t' indicates to search children.
- t)))
- ;; Default to returning nil from the catch body.
- nil)))
-
-(defcustom context-coloring-detect-node t
- "If non-nil, use file-level scope for variables in Node.js."
- :type 'boolean
- :group 'context-coloring)
-
-
;;; JavaScript colorization
(defvar-local context-coloring-js2-scope-level-hash-table nil
@@ -472,6 +400,83 @@ this for ES6 code; disable it elsewhere."
t)))
(context-coloring-colorize-comments-and-strings)))
+(defconst context-coloring-node-comment-regexp
+ (concat
+ ;; Ensure the "//" or "/*" comment starts with the directive.
+ "\\(//[[:space:]]*\\|/\\*[[:space:]]*\\)"
+ ;; Support multiple directive formats.
+ "\\("
+ ;; JSLint and JSHint support a JSON-like format.
+ "\\(jslint\\|jshint\\)[[:space:]].*?node:[[:space:]]*true"
+ "\\|"
+ ;; ESLint just specifies the option name.
+ "eslint-env[[:space:]].*?node"
+ "\\)")
+ "Match a comment body hinting at a Node.js program.")
+
+(defun context-coloring-node-program-p ()
+ "Guess whether the current file is a Node.js program."
+ (or
+ ;; A shebang is a pretty obvious giveaway.
+ (string-equal
+ "node"
+ (save-excursion
+ (goto-char (point-min))
+ (when (looking-at auto-mode-interpreter-regexp)
+ (match-string 2))))
+ ;; Otherwise, perform static analysis.
+ (progn
+ (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test
#'eq))
+ (catch 'node-program-p
+ (js2-visit-ast
+ js2-mode-ast
+ (lambda (node end-p)
+ (when (null end-p)
+ (when
+ (cond
+ ;; Infer based on inline linter configuration.
+ ((js2-comment-node-p node)
+ (string-match-p
+ context-coloring-node-comment-regexp
+ (js2-node-string node)))
+ ;; Infer based on the prescence of certain variables.
+ ((and (js2-name-node-p node)
+ (let ((parent (js2-node-parent node)))
+ (not (and (js2-object-prop-node-p parent)
+ (eq node (js2-object-prop-node-left
parent))))))
+ (let ((name (js2-name-node-name node))
+ (parent (js2-node-parent node)))
+ (and
+ (cond
+ ;; Check whether this is "exports.something" or
+ ;; "module.exports".
+ ((js2-prop-get-node-p parent)
+ (and
+ (eq node (js2-prop-get-node-left parent))
+ (or (string-equal name "exports")
+ (let* ((property (js2-prop-get-node-right parent))
+ (property-name (js2-name-node-name
property)))
+ (and (string-equal name "module")
+ (string-equal property-name "exports"))))))
+ ;; Check whether it's a "require('module')" call.
+ ((js2-call-node-p parent)
+ (or (string-equal name "require"))))
+ (let* ((enclosing-scope (js2-node-get-enclosing-scope
node))
+ (defining-scope (js2-get-defining-scope
+ enclosing-scope name)))
+ ;; The variable also must be global.
+ (null defining-scope))))))
+ (throw 'node-program-p t))
+ ;; The `t' indicates to search children.
+ t)))
+ ;; Default to returning nil from the catch body.
+ nil))))
+
+(defcustom context-coloring-detect-node t
+ "If non-nil, use file-level scope for variables in Node.js."
+ :type 'boolean
+ :group 'context-coloring)
+
(defun context-coloring-js2-colorize ()
"Color the buffer using the `js2-mode'."
(cond
diff --git a/test/context-coloring-test.el b/test/context-coloring-test.el
index abd4df1..6cf78e4 100644
--- a/test/context-coloring-test.el
+++ b/test/context-coloring-test.el
@@ -645,7 +645,8 @@ ssssssssssss0"))
(negative-indicators
(list "// Blah blah jshint blah."
"module"
- "exports")))
+ "exports"
+ "var require; require('a')")))
(dolist (indicator positive-indicators)
(context-coloring-test-setup-detect-node indicator)
(context-coloring-test-assert-javascript-elevated-level))
- [elpa] master 53279b1 01/16: Fix tests on Windows., (continued)
- [elpa] master 53279b1 01/16: Fix tests on Windows., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 85397da 02/16: Add more sophisticated block scope test., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 683a522 04/16: Suggest eval-expression-minibuffer-setup-hook., Jackson Ray Hamilton, 2015/10/18
- [elpa] master df914b8 03/16: Bump minimum js2-mode version., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 7c16f46 05/16: Add initial level setting., Jackson Ray Hamilton, 2015/10/18
- [elpa] master fa99117 06/16: Add defcustom types., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 614de4c 07/16: Don't make the initial level buffer-local., Jackson Ray Hamilton, 2015/10/18
- [elpa] master a193f4e 08/16: Refactor position level assertion., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 6fe9e58 09/16: Add Node.js detection., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 6531b86 11/16: Use more agnostic names., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 2c62db1 10/16: Ensure Node.js variable is global.,
Jackson Ray Hamilton <=
- [elpa] master 6ac3b43 12/16: Merge branch 'detect-node', Jackson Ray Hamilton, 2015/10/18
- [elpa] master ff9643c 14/16: Update Zenburn paragraph., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 765d4c4 13/16: Document new scope detection., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 0c3a48f 15/16: Version 7.1.0., Jackson Ray Hamilton, 2015/10/18
- [elpa] master 6934762 16/16: Merge commit '0c3a48f05d17f486a0c522f0f9ec316dce24d86d' from context-coloring, Jackson Ray Hamilton, 2015/10/18