Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix object lookups via object literals #65

Merged
merged 1 commit into from

2 participants

@tricknotes

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.

@rstacruz
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
@rstacruz rstacruz merged commit 02cd263 into js2coffee:master
@rstacruz
Owner

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

@tricknotes

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?

@clojens clojens referenced this pull request from a commit in coffeebook/js2coffee
@mauricemach 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. @tricknotes
This page is out of date. Refresh to see the latest.
View
7 lib/js2coffee.coffee
@@ -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) ->
View
6 test/features/object_literal_at_index.coffee
@@ -0,0 +1,6 @@
+o = {}
+o[{
+ a: 1
+ b: 2
+}] = 3
+o[c: 4] = 5
View
5 test/features/object_literal_at_index.js
@@ -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.