;;; js2-mode.el --- Improved JavaScript editing mode
-;; Copyright (C) 2009, 2011-2015 Free Software Foundation, Inc.
+;; Copyright (C) 2009, 2011-2016 Free Software Foundation, Inc.
;; Author: Steve Yegge <steve.yegge@gmail.com>
;; mooz <stillpedant@gmail.com>
;; Dmitry Gutov <dgutov@yandex.ru>
;; URL: https://github.com/mooz/js2-mode/
;; http://code.google.com/p/js2-mode/
-;; Version: 20150909
+;; Version: 20160623
;; Keywords: languages, javascript
;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
:type 'boolean
:group 'js2-mode)
+(defcustom js2-mode-assume-strict nil
+ "Non-nil to start files in strict mode automatically."
+ :type 'boolean
+ :group 'js2-mode)
+
(defcustom js2-mode-show-strict-warnings t
"Non-nil to emit Ecma strict-mode warnings.
Some of the warnings can be individually disabled by other flags,
"Compilation produced %s syntax errors.")
(js2-msg "msg.var.redecl"
- "TypeError: redeclaration of var %s.")
+ "Redeclaration of var %s.")
(js2-msg "msg.const.redecl"
"TypeError: redeclaration of const %s.")
when (and (eq 'block (js2-comment-node-format node))
(save-excursion
(goto-char (js2-node-abs-pos node))
- (looking-at "/\\*global ")))
+ (looking-at "/\\* *global ")))
append (js2-get-jslint-globals-in
(match-end 0)
(js2-node-abs-end node))))
we append the property name to QNAME, then call `js2-record-imenu-entry'."
(let (right)
(dolist (e (js2-object-node-elems node)) ; e is a `js2-object-prop-node'
- (let ((left (js2-infix-node-left e))
- ;; Element positions are relative to the parent position.
- (pos (+ pos (js2-node-pos e))))
- (cond
- ;; foo: function() {...}
- ((js2-function-node-p (setq right (js2-infix-node-right e)))
- (when (js2-prop-node-name left)
- ;; As a policy decision, we record the position of the property,
- ;; not the position of the `function' keyword, since the property
- ;; is effectively the name of the function.
- (js2-record-imenu-entry right (append qname (list left)) pos)))
- ;; foo: {object-literal} -- add foo to qname, offset position, and recurse
- ((js2-object-node-p right)
- (js2-record-object-literal right
- (append qname (list (js2-infix-node-left e)))
- (+ pos (js2-node-pos right)))))))))
+ (when (js2-infix-node-p e)
+ (let ((left (js2-infix-node-left e))
+ ;; Element positions are relative to the parent position.
+ (pos (+ pos (js2-node-pos e))))
+ (cond
+ ;; foo: function() {...}
+ ((js2-function-node-p (setq right (js2-infix-node-right e)))
+ (when (js2-prop-node-name left)
+ ;; As a policy decision, we record the position of the property,
+ ;; not the position of the `function' keyword, since the property
+ ;; is effectively the name of the function.
+ (js2-record-imenu-entry right (append qname (list left)) pos)))
+ ;; foo: {object-literal} -- add foo to qname, offset position, and recurse
+ ((js2-object-node-p right)
+ (js2-record-object-literal right
+ (append qname (list (js2-infix-node-left e)))
+ (+ pos (js2-node-pos right))))))))))
(defun js2-node-top-level-decl-p (node)
"Return t if NODE's name is defined in the top-level scope.
js2-nesting-of-function 0
js2-labeled-stmt nil
js2-recorded-identifiers nil ; for js2-highlight
- js2-in-use-strict-directive nil)
+ js2-in-use-strict-directive js2-mode-assume-strict)
(while (/= (setq tt (js2-get-token)) js2-EOF)
(if (= tt js2-FUNCTION)
(progn
(list node)))
((js2-object-node-p node)
(dolist (elem (js2-object-node-elems node))
- ;; js2-infix-node-p catches both object prop node and initialized
- ;; binding element (which is directly an infix node).
- (when (js2-infix-node-p elem)
- (push (js2-define-destruct-symbols
- (js2-infix-node-left elem)
- decl-type face ignore-not-in-block)
- name-nodes)))
+ (let ((subexpr (cond
+ ((and (js2-infix-node-p elem)
+ (= js2-ASSIGN (js2-infix-node-type elem)))
+ ;; Destructuring with default argument.
+ (js2-infix-node-left elem))
+ ((and (js2-infix-node-p elem)
+ (= js2-COLON (js2-infix-node-type elem)))
+ ;; In regular destructuring {a: aa, b: bb},
+ ;; the var is on the right. In abbreviated
+ ;; destructuring {a, b}, right == left.
+ (js2-infix-node-right elem))
+ ((and (js2-unary-node-p elem)
+ (= js2-TRIPLEDOT (js2-unary-node-type elem)))
+ ;; Destructuring with spread.
+ (js2-unary-node-operand elem)))))
+ (when subexpr
+ (push (js2-define-destruct-symbols
+ subexpr decl-type face ignore-not-in-block)
+ name-nodes))))
(apply #'append (nreverse name-nodes)))
((js2-array-node-p node)
(dolist (elem (js2-array-node-elems node))
(js2-define-symbol
js2-LET (js2-name-node-name name-node) name-node t))))))
((= (js2-peek-token) js2-NAME)
- (let ((binding (js2-maybe-parse-export-binding)))
+ (let ((binding (js2-maybe-parse-export-binding t)))
(let ((node-name (js2-export-binding-node-local-name binding)))
(js2-define-symbol js2-LET (js2-name-node-name node-name) node-name t))
(setf (js2-import-clause-node-default-binding clause) binding)
js2-LC. Return a lisp list of js2-export-binding-node"
(let ((bindings (list)))
(while
- (let ((binding (js2-maybe-parse-export-binding)))
+ (let ((binding (js2-maybe-parse-export-binding import-p)))
(when binding
(push binding bindings))
(js2-match-token js2-COMMA)))
"msg.mod.rc.after.export.spec.list"))
(reverse bindings))))
-(defun js2-maybe-parse-export-binding ()
+(defun js2-maybe-parse-export-binding (&optional import-p)
"Attempt to parse a binding expression found inside an import/export statement.
This can take the form of either as single js2-NAME token as in 'foo' or as in a
rebinding expression 'bar as foo'. If it matches, it will return an instance of
node
(js2-export-binding-node-local-name node)
(js2-export-binding-node-extern-name node))
+ (if import-p
+ (js2-set-face (js2-current-token-beg) (js2-current-token-end)
+ 'font-lock-variable-name-face 'record))
node)
(js2-unget-token)
nil))
(when is-reserved-name
(js2-report-error "msg.mod.as.after.reserved.word" extern-name))
(js2-node-add-children node name-node)
+ (if import-p
+ (js2-set-face (js2-current-token-beg) (js2-current-token-end)
+ 'font-lock-variable-name-face 'record))
node))
nil)))
(pos (if node (js2-node-abs-pos node)))
(len (if node (js2-node-len node))))
(cond
- ((and symbol ; already defined
- (or (if js2-in-use-strict-directive
- ;; two const-bound vars in this block have same name
- (and (= sdt js2-CONST)
- (eq defining-scope js2-current-scope))
- (or (= sdt js2-CONST) ; old version is const
- (= decl-type js2-CONST))) ; new version is const
- ;; two let-bound vars in this block have same name
- (and (= sdt js2-LET)
- (eq defining-scope js2-current-scope))))
+ ((and symbol ; already defined in this block
+ (or (= sdt js2-LET)
+ (= sdt js2-CONST))
+ (eq defining-scope js2-current-scope))
(js2-report-error
(cond
((= sdt js2-CONST) "msg.const.redecl")
(t "msg.parm.redecl"))
name pos len))
((or (= decl-type js2-LET)
- ;; strict mode const is scoped to the current LexicalEnvironment
- (and js2-in-use-strict-directive
- (= decl-type js2-CONST)))
+ (= decl-type js2-CONST))
(if (and (= decl-type js2-LET)
(not ignore-not-in-block)
(or (= (js2-node-type js2-current-scope) js2-IF)
(js2-report-error "msg.let.decl.not.in.block")
(js2-define-new-symbol decl-type name node)))
((or (= decl-type js2-VAR)
- (= decl-type js2-FUNCTION)
- ;; sloppy mode const is scoped to the current VariableEnvironment
- (and (not js2-in-use-strict-directive)
- (= decl-type js2-CONST)))
+ (= decl-type js2-FUNCTION))
(if symbol
(if (and js2-strict-var-redeclaration-warning (= sdt js2-VAR))
(js2-add-strict-warning "msg.var.redecl" name)
`js2-method-node') as a string, or nil if it can't be
represented as a string (e.g., the key is computed by an
expression)."
- (let ((key (js2-infix-node-left property-node)))
- (when (js2-computed-prop-name-node-p key)
- (setq key (js2-computed-prop-name-node-expr key)))
- (cond
- ((js2-name-node-p key)
- (js2-name-node-name key))
- ((js2-string-node-p key)
- (js2-string-node-value key))
- ((js2-number-node-p key)
- (js2-number-node-value key)))))
+ (cond
+ ((js2-unary-node-p property-node) nil) ;; {...foo}
+ (t
+ (let ((key (js2-infix-node-left property-node)))
+ (when (js2-computed-prop-name-node-p key)
+ (setq key (js2-computed-prop-name-node-expr key)))
+ (cond
+ ((js2-name-node-p key)
+ (js2-name-node-name key))
+ ((js2-string-node-p key)
+ (js2-string-node-value key))
+ ((js2-number-node-p key)
+ (js2-number-node-value key)))))))
(defun js2-parse-object-literal-elems (&optional class-p)
(let ((pos (js2-current-token-beg))
(setq previous-token (js2-current-token)
tt (js2-get-prop-name-token))))
(cond
- ;; Found a property (of any sort)
+ ;; Rest/spread (...expr)
+ ((and (>= js2-language-version 200)
+ (not class-p) (not static) (not previous-token)
+ (= js2-TRIPLEDOT tt))
+ (setq after-comma nil
+ elem (js2-make-unary js2-TRIPLEDOT 'js2-parse-assign-expr)))
+ ;; Found a key/value property (of any sort)
((member tt (list js2-NAME js2-STRING js2-NUMBER js2-LB))
(setq after-comma nil
elem (js2-parse-named-prop tt previous-token))
("async" . ASYNC))))
'FUNCTION))
result end
- (fn (js2-parse-function-expr (eq type 'ASYNC))))
- ;; it has to be an anonymous function, as we already parsed the name
- (if (/= (js2-node-type fn) js2-FUNCTION)
- (js2-report-error "msg.bad.prop")
- (if (cl-plusp (length (js2-function-name fn)))
- (js2-report-error "msg.bad.prop")))
+ (pos (js2-current-token-beg))
+ (_ (js2-must-match js2-LP "msg.no.paren.parms"))
+ (fn (js2-parse-function 'FUNCTION_EXPRESSION pos
+ (string= type-string "*")
+ (eq type 'ASYNC)
+ nil)))
(js2-node-set-prop fn 'METHOD_TYPE type) ; for codegen
- (when (string= type-string "*")
- (setf (js2-function-node-generator-type fn) 'STAR))
(unless pos (setq pos (js2-node-pos prop)))
(setq end (js2-node-end fn)
result (make-js2-method-node :pos pos