From 8abf087d0a66aee071fdf2dd5a249a22a0855959 Mon Sep 17 00:00:00 2001 From: Carl Lei Date: Fri, 30 Oct 2015 21:17:16 +0800 Subject: [PATCH] Report error for binding initializer outside destructuring This reports error for both object literals and class bodies, but only tests object literals; because if ES7 class property initializers are added, the error no longer makes sense and needs another message, e.g. missing semicolon after property initializer. --- js2-mode.el | 5 +++++ tests/parser.el | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/js2-mode.el b/js2-mode.el index 249f4ca8b..643e921ce 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.") @@ -10725,6 +10728,8 @@ When `js2-is-in-destructuring' is t, forms like {a, b, c} will be permitted." ;; 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 diff --git a/tests/parser.el b/tests/parser.el index 51b03eb56..136b97c5a 100644 --- a/tests/parser.el +++ b/tests/parser.el @@ -198,6 +198,11 @@ the test." (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 -- 2.39.2