Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix object lookups via object literals #65

Merged
merged 1 commit into from

2 participants

Ryunosuke SATO Rico Sta. Cruz
Ryunosuke SATO

I noticed that the following JavaSsript does not compile correctly.

var o = {}
o[{a: 1, b: 2}] = 3

compiled:

o = {}
o[
  a: 1
  b: 2
] = 3

But when run this code, this occurs error.

I modified this compilation.

Thanks.

Rico Sta. Cruz
Owner

Thanks!

Just as an afterthought: which do you think would look better?

#1
o[a: 1] = 3
o[(a: 2, b: 3)] = 4
#2
o[{a: 1}] = 3
o[{a: 2, b: 3}] = 4
#3
o[a: 1] = 3
o[(
  a: 2
  b: 3
)] = 4
Rico Sta. Cruz rstacruz merged commit 02cd263 into from
Rico Sta. Cruz
Owner

(I've renamed this issue from Fix object literal to Fix object lookups via object literals.)

Ryunosuke SATO

I want to use '{}' rather than '()' , because I would like to express the object literal notation.
In CoffeeScript, the good way I think is that '{}' is not used.
So, the followings looks good.

#4
o[a: 1] = 3
o[a: 2, b: 3] = 4

If you think would like to this idea, may I write patch and send as pull request?

Rob Jentzema clojens referenced this pull request from a commit in coffeebook/js2coffee
Maurice Machado mauricemach A few tweaks to pull #65. ecf2c35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2011
  1. Ryunosuke SATO
This page is out of date. Refresh to see the latest.
7 lib/js2coffee.coffee
View
@@ -199,7 +199,12 @@ class Builder
'~': (n) -> "~#{@build n.left()}"
'typeof': (n) -> "typeof #{@build n.left()}"
- 'index': (n) -> "#{@build n.left()}[#{@build n.right()}]"
+ 'index': (n) ->
+ right = @build n.right()
+ if _.any(n.children, (child) -> child.typeName() == 'object_init' and child.children.length > 1)
+ right = "{#{right}}"
+ "#{@build n.left()}[#{right}]"
+
'throw': (n) -> "throw #{@build n.exception}"
'!': (n) ->
6 test/features/object_literal_at_index.coffee
View
@@ -0,0 +1,6 @@
+o = {}
+o[{
+ a: 1
+ b: 2
+}] = 3
+o[c: 4] = 5
5 test/features/object_literal_at_index.js
View
@@ -0,0 +1,5 @@
+var o = {}
+
+o[{a: 1, b: 2}] = 3
+
+o[{c: 4}] = 5
Something went wrong with that request. Please try again.