[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 221282a 028/110: Merge pull request #280 from XeCycle/bind
From: |
Dmitry Gutov |
Subject: |
[elpa] master 221282a 028/110: Merge pull request #280 from XeCycle/binding-pattern-initializer |
Date: |
Thu, 23 Jun 2016 01:12:54 +0000 (UTC) |
branch: master
commit 221282ab7fd3854c79c0b3373856928f730bccb3
Merge: d201a30 8abf087
Author: Dmitry Gutov <address@hidden>
Commit: Dmitry Gutov <address@hidden>
Merge pull request #280 from XeCycle/binding-pattern-initializer
Add default parameters support to destructuring
---
NEWS.md | 2 ++
js2-mode.el | 48 ++++++++++++++++++++++++++++++++++++------------
tests/parser.el | 25 +++++++++++++++++++++++++
3 files changed, 63 insertions(+), 12 deletions(-)
diff --git a/NEWS.md b/NEWS.md
index aa04a42..30500e0 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -8,6 +8,8 @@
with its related functions. It already handles generator methods,
and will in the future add support for async methods, so the old
name will get more confusing.
+* Support for default parameters in destructuring. It should work for both
+ objects and arrays, in both literals and function arguments.
## 20150909
diff --git a/js2-mode.el b/js2-mode.el
index 865f287..643e921 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -1717,6 +1717,9 @@ the correct number of ARGS must be provided."
(js2-msg "msg.destruct.assign.no.init"
"Missing = in destructuring declaration")
+(js2-msg "msg.init.no.destruct"
+ "Binding initializer not in destructuring assignment")
+
(js2-msg "msg.no.octal.strict"
"Octal numbers prohibited in strict mode.")
@@ -8003,16 +8006,18 @@ declared; probably to check them for errors."
(list node)))
((js2-object-node-p node)
(dolist (elem (js2-object-node-elems node))
- (when (js2-object-prop-node-p elem)
+ ;; 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
- ;; In abbreviated destructuring {a, b}, right == left.
- (js2-object-prop-node-right elem)
+ (js2-infix-node-left elem)
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))
(when elem
+ (if (js2-infix-node-p elem) (setq elem (js2-infix-node-left elem)))
(push (js2-define-destruct-symbols
elem decl-type face ignore-not-in-block)
name-nodes)))
@@ -9632,7 +9637,7 @@ If NODE is non-nil, it is the AST node associated with
the symbol."
(js2-node-add-children pn test-expr if-true if-false))
pn))
-(defun js2-make-binary (type left parser)
+(defun js2-make-binary (type left parser &optional no-get)
"Helper for constructing a binary-operator AST node.
LEFT is the left-side-expression, already parsed, and the
binary operator should have just been matched.
@@ -9643,7 +9648,7 @@ FIXME: The latter option is unused?"
(op-pos (- (js2-current-token-beg) pos))
(right (if (js2-node-p parser)
parser
- (js2-get-token)
+ (unless no-get (js2-get-token))
(funcall parser)))
(pn (make-js2-infix-node :type type
:pos pos
@@ -10364,14 +10369,20 @@ array-literals, array comprehensions and regular
expressions."
(apply #'js2-node-add-children pn (js2-array-node-elems pn)))
;; destructuring binding
(js2-is-in-destructuring
- (push (if (or (= tt js2-LC)
- (= tt js2-LB)
- (= tt js2-NAME))
- ;; [a, b, c] | {a, b, c} | {a:x, b:y, c:z} | a
- (js2-parse-destruct-primary-expr)
- ;; invalid pattern
+ (push (cond
+ ((and (= tt js2-NAME)
+ (= js2-ASSIGN (js2-peek-token)))
+ ;; a=defaultValue
+ (js2-parse-initialized-binding (js2-parse-name js2-NAME)))
+ ((or (= tt js2-LC)
+ (= tt js2-LB)
+ (= tt js2-NAME))
+ ;; [a, b, c] | {a, b, c} | {a:x, b:y, c:z} | a
+ (js2-parse-destruct-primary-expr))
+ ;; invalid pattern
+ (t
(js2-report-error "msg.bad.var")
- (make-js2-error-node))
+ (make-js2-error-node)))
elems)
(setq after-lb-or-comma nil
after-comma nil))
@@ -10714,6 +10725,12 @@ When `js2-is-in-destructuring' is t, forms like {a, b,
c} will be permitted."
(when (js2-name-node-p key) ; highlight function name properties
(js2-record-face 'font-lock-function-name-face))
(js2-parse-method-prop pos key property-type))
+ ;; binding element with initializer
+ ((and (= (js2-peek-token) js2-ASSIGN)
+ (>= js2-language-version 200))
+ (if (not js2-is-in-destructuring)
+ (js2-report-error "msg.init.no.destruct"))
+ (js2-parse-initialized-binding key))
;; regular prop
(t
(let ((beg (js2-current-token-beg))
@@ -10732,6 +10749,13 @@ When `js2-is-in-destructuring' is t, forms like {a, b,
c} will be permitted."
'record)
expr)))))
+(defun js2-parse-initialized-binding (name)
+ "Parse a `SingleNameBinding' with initializer.
+
+`name' is the `BindingIdentifier'."
+ (when (js2-match-token js2-ASSIGN)
+ (js2-make-binary js2-ASSIGN name 'js2-parse-assign-expr t)))
+
(defun js2-parse-prop-name (tt)
(cond
;; Literal string keys: {'foo': 'bar'}
diff --git a/tests/parser.el b/tests/parser.el
index c65be2c..136b97c 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -178,6 +178,31 @@ the test."
(js2-deftest-parse destruct-in-catch-clause
"try {\n} catch ({a, b}) {\n a + b;\n}")
+(js2-deftest-parse destruct-with-initializer-in-object
+ "var {a, b = 2, c} = {};")
+
+(js2-deftest-parse destruct-with-initializer-in-array
+ "var [a, b = 2, c] = [];")
+
+(js2-deftest-parse destruct-non-name-target-is-error
+ "var {1=1} = {};" :syntax-error "1" :errors-count 1)
+
+(js2-deftest-parse destruct-with-initializer-in-function-arguments
+ "function f({a, b = 1, c}, [d, e = 1, f]) {\n}")
+
+(js2-deftest-parse destruct-name-conflict-is-error-in-object
+ "\"use strict\";\nvar {a=1,a=2} = {};" :syntax-error "a" :errors-count 1)
+
+(js2-deftest destruct-name-conflict-is-warning-in-array "\"use strict\";\nvar
[a=1,a=2] = [];"
+ (js2-mode)
+ (should (equal '("msg.var.redecl" "a")
+ (caar js2-parsed-warnings))))
+
+(js2-deftest initializer-outside-destruct-is-error "({a=1});"
+ (js2-mode)
+ (should (equal "msg.init.no.destruct"
+ (car (caar js2-parsed-errors)))))
+
;;; Object literals
(js2-deftest-parse object-literal-shorthand
- [elpa] master c63c700 029/110: Update MELPA URL, (continued)
- [elpa] master c63c700 029/110: Update MELPA URL, Dmitry Gutov, 2016/06/22
- [elpa] master a28a648 007/110: Remove await-node for 'awayt' and use unary-node, Dmitry Gutov, 2016/06/22
- [elpa] master 2cf52e9 015/110: js2-parse-highlight-prop-get: Take care now to overwrite the js2-function-call face, Dmitry Gutov, 2016/06/22
- [elpa] master 3603bc4 018/110: Add test on shorthand method name and scoping, Dmitry Gutov, 2016/06/22
- [elpa] master 0555a8a 024/110: Support initializer in destructuring, Dmitry Gutov, 2016/06/22
- [elpa] master 1f95553 033/110: Add js2-jsx-mode usage instructions, Dmitry Gutov, 2016/06/22
- [elpa] master 47b215d 011/110: Define js2-object-property face, Dmitry Gutov, 2016/06/22
- [elpa] master c0e0309 022/110: Allow semicolons in a class body, Dmitry Gutov, 2016/06/22
- [elpa] master df3f97f 030/110: Add js2-jsx-mode, Dmitry Gutov, 2016/06/22
- [elpa] master 6aba571 021/110: Merge pull request #276 from XeCycle/methods, Dmitry Gutov, 2016/06/22
- [elpa] master 221282a 028/110: Merge pull request #280 from XeCycle/binding-pattern-initializer,
Dmitry Gutov <=
- [elpa] master 2e7fdb3 012/110: Also highlight property lookups with the same face, Dmitry Gutov, 2016/06/22
- [elpa] master b1234ca 013/110: Don't misindent generator methods, Dmitry Gutov, 2016/06/22
- [elpa] master d82a58d 019/110: Refactor: rename getter-setter-node to method-node, Dmitry Gutov, 2016/06/22
- [elpa] master 263852b 026/110: Add melpa badges, Dmitry Gutov, 2016/06/22
- [elpa] master 98433fb 036/110: `js2-print-json-path' added, Dmitry Gutov, 2016/06/22
- [elpa] master f9d28d3 052/110: Adjust js2-print-export-node, Dmitry Gutov, 2016/06/22
- [elpa] master d307285 039/110: Move method type handling to property METHOD_TYPE, Dmitry Gutov, 2016/06/22
- [elpa] master f652d17 044/110: Add setImmediate and clearImmediate to js2-node-externs, Dmitry Gutov, 2016/06/22
- [elpa] master e0972fb 048/110: s/will/would, Dmitry Gutov, 2016/06/22
- [elpa] master d201a30 027/110: Merge pull request #281 from robbyoconnor/patch-1, Dmitry Gutov, 2016/06/22