[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master dd344ff 01/45: Support short-hand object literals {foo, ba
From: |
Dmitry Gutov |
Subject: |
[elpa] master dd344ff 01/45: Support short-hand object literals {foo, bar} everywhere. |
Date: |
Mon, 02 Feb 2015 03:18:31 +0000 |
branch: master
commit dd344ff6d7b079f1ca9fe967cd7e8b6850a4231c
Author: Stephen Hicks <address@hidden>
Commit: Stephen Hicks <address@hidden>
Support short-hand object literals {foo, bar} everywhere.
This is specified in ยง12.2.5 of the draft spec
(http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object-initializer)
by the production [PropertyDefinition : IdentifierReference], and discussed
e.g. in
http://ariya.ofilabs.com/2013/02/es6-and-object-literal-property-value-shorthand.html.
---
js2-mode.el | 54 +++++++++++++++++++++++++++++++++++++++++-------------
tests/parser.el | 5 +++++
2 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/js2-mode.el b/js2-mode.el
index d6a0d29..db625ca 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -3523,10 +3523,20 @@ The `right' field is a `js2-node' representing the
initializer value.")
(put 'cl-struct-js2-object-prop-node 'js2-printer 'js2-print-object-prop-node)
(defun js2-print-object-prop-node (n i)
- (insert (js2-make-pad i))
- (js2-print-ast (js2-object-prop-node-left n) 0)
- (insert ": ")
- (js2-print-ast (js2-object-prop-node-right n) 0))
+ (let* ((left (js2-object-prop-node-left n))
+ (computed (not (or (js2-string-node-p left)
+ (js2-number-node-p left)
+ (js2-name-node-p left)))))
+ (insert (js2-make-pad i))
+ (if computed
+ (insert "["))
+ (js2-print-ast left 0)
+ (if computed
+ (insert "]"))
+ (if (not (js2-node-get-prop n 'SHORTHAND))
+ (progn
+ (insert ": ")
+ (js2-print-ast (js2-object-prop-node-right n) 0)))))
(defstruct (js2-getter-setter-node
(:include js2-infix-node)
@@ -9602,13 +9612,31 @@ When `js2-is-in-destructuring' is t, forms like {a, b,
c} will be permitted."
(defun js2-parse-plain-property (prop)
"Parse a non-getter/setter property in an object literal.
PROP is the node representing the property: a number, name or string."
- (let ((pos (js2-node-pos prop))
- colon expr)
- (if (js2-must-match js2-COLON "msg.no.colon.prop")
- (setq colon (- (js2-current-token-beg) pos)
- expr (js2-parse-assign-expr))
- (setq expr (make-js2-error-node)))
- (let ((result (make-js2-object-prop-node
+ (let* ((tt (js2-get-token))
+ (pos (js2-node-pos prop))
+ colon expr result)
+ (cond
+ ;; Abbreviated property, as in {foo, bar}
+ ((and (>= js2-language-version 200)
+ (or (= tt js2-COMMA)
+ (= tt js2-RC)))
+ (js2-unget-token)
+ (setq result (make-js2-object-prop-node
+ :pos pos
+ :left prop
+ :right prop
+ :op-pos (js2-current-token-len)))
+ (js2-node-add-children result prop)
+ (js2-node-set-prop result 'SHORTHAND t)
+ result)
+ ;; Normal property
+ (t
+ (if (= tt js2-COLON)
+ (setq colon (- (js2-current-token-beg) pos)
+ expr (js2-parse-assign-expr))
+ (js2-report-error "msg.no.colon.prop")
+ (setq expr (make-js2-error-node)))
+ (setq result (make-js2-object-prop-node
:pos pos
;; don't include last consumed token in length
:len (- (+ (js2-node-pos expr)
@@ -9616,9 +9644,9 @@ PROP is the node representing the property: a number,
name or string."
pos)
:left prop
:right expr
- :op-pos colon)))
+ :op-pos colon))
(js2-node-add-children result prop expr)
- result)))
+ result))))
(defun js2-parse-getter-setter-prop (pos prop get-p)
"Parse getter or setter property in an object literal.
diff --git a/tests/parser.el b/tests/parser.el
index ffda856..1c903be 100644
--- a/tests/parser.el
+++ b/tests/parser.el
@@ -179,6 +179,11 @@ the test."
(js2-deftest-parse destruct-in-catch-clause
"try {\n} catch ({a, b}) {\n a + b;\n}")
+;;; Object literals
+
+(js2-deftest-parse abbreviated-object
+ "var x = {a: 1, b, c: 1, d};")
+
;;; Function parameters
(js2-deftest-parse function-with-default-parameters
- [elpa] master updated (d47c872 -> a1ac1e0), Dmitry Gutov, 2015/02/01
- [elpa] master 6037c0f 04/45: Add support for 'u' and 'y' RegExp flags in ES6 mode., Dmitry Gutov, 2015/02/01
- [elpa] master dad2f12 03/45: Support method definitions in object literals., Dmitry Gutov, 2015/02/01
- [elpa] master 926cb45 05/45: Support ES6 computed property names., Dmitry Gutov, 2015/02/01
- [elpa] master 30249fa 07/45: Support parsing the ES6 'static' keyword., Dmitry Gutov, 2015/02/01
- [elpa] master c79d426 06/45: Support ES6 class statements/expressions., Dmitry Gutov, 2015/02/01
- [elpa] master 2b0f5ba 08/45: Support parsing the ES6 'super' keyword., Dmitry Gutov, 2015/02/01
- [elpa] master dd344ff 01/45: Support short-hand object literals {foo, bar} everywhere.,
Dmitry Gutov <=
- [elpa] master b1f7e6c 11/45: Simplify parsing of destructuring literals, Dmitry Gutov, 2015/02/01
- [elpa] master 68b4abd 02/45: Add ES6 class externs, Dmitry Gutov, 2015/02/01
- [elpa] master ee17084 13/45: js2-parse-plain-property: Disallow numbers as abbreviated props, Dmitry Gutov, 2015/02/01
- [elpa] master b19ea52 12/45: js2-parse-named-prop: Highlight externals in abbreviated props, Dmitry Gutov, 2015/02/01
- [elpa] master 31c49e0 14/45: js2-imenu-extension-styles: Turn into defvar, Dmitry Gutov, 2015/02/01
- [elpa] master 9b77d64 15/45: Implement template strings, ignoring substitutions, Dmitry Gutov, 2015/02/01
- [elpa] master 7b3d590 17/45: No need to explicitly apply face to string nodes, Dmitry Gutov, 2015/02/01
- [elpa] master 3f09ff3 09/45: Fix string literals in parser.el test harness., Dmitry Gutov, 2015/02/01
- [elpa] master d0c6bf3 18/45: Comment the new token types, Dmitry Gutov, 2015/02/01
- [elpa] master 9525eb3 10/45: Merge branch 'master' of github.com:shicks/js2-mode into shicks-master, Dmitry Gutov, 2015/02/01