"\\)")
"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."
+;; TODO: Add ES6 module detection.
+(defun context-coloring-js2-top-level-local-p ()
+ "Guess whether top-level variables are local.
+For instance, the current file could be a Node.js program."
(or
;; A shebang is a pretty obvious giveaway.
(string-equal
;; 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."
+(defcustom context-coloring-javascript-detect-top-level-scope t
+ "If non-nil, detect when to use file-level scope."
:type 'boolean
:group 'context-coloring)
(defun context-coloring-js2-colorize ()
"Color the buffer using the `js2-mode'."
(cond
- ;; Increase the initial level if we can detect Node.js.
- ((and context-coloring-detect-node
- (context-coloring-node-program-p))
+ ;; Increase the initial level if we should.
+ ((and context-coloring-javascript-detect-top-level-scope
+ (context-coloring-js2-top-level-local-p))
(let ((context-coloring-initial-level 1))
(context-coloring-js2-colorize-ast)))
(t
:after (lambda ()
(setq context-coloring-initial-level 0)))
-(defun context-coloring-test-setup-detect-node (string)
+(defun context-coloring-test-setup-top-level-scope (string)
"Make STRING the first line and colorize again."
(goto-char (point-min))
(kill-whole-line 0)
;; Reparsing triggers recoloring.
(js2-reparse))
-(context-coloring-test-deftest-javascript detect-node
+(context-coloring-test-deftest-javascript top-level-scope
(lambda ()
(let ((positive-indicators
(list "#!/usr/bin/env node"
"exports"
"var require; require('a')")))
(dolist (indicator positive-indicators)
- (context-coloring-test-setup-detect-node indicator)
+ (context-coloring-test-setup-top-level-scope indicator)
(context-coloring-test-assert-javascript-elevated-level))
(dolist (indicator negative-indicators)
- (context-coloring-test-setup-detect-node indicator)
+ (context-coloring-test-setup-top-level-scope indicator)
(context-coloring-test-assert-javascript-global-level))))
:fixture "initial-level.js")