diff --git a/lib/nodes.js b/lib/nodes.js index 9c06d7c7..c3edef81 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -703,23 +703,27 @@ return this.tags.front ? "(" + obj + ")" : obj; }; Obj.prototype.compileDynamic = function(o, idx) { - var _len, _ref2, _ref3, code, i, key, obj, prop, ref, sub; + var _len, _ref2, _ref3, acc, code, i, key, obj, prop, ref, val; obj = o.scope.freeVariable('obj'); code = "" + obj + " = " + (new Obj(this.properties.slice(0, idx)).compile(o)) + ", "; _ref2 = this.properties.slice(idx); for (i = 0, _len = _ref2.length; i < _len; i++) { prop = _ref2[i]; - if (prop instanceof Assign) { - key = prop.variable.compile(o, LEVEL_PAREN); - code += "" + obj + "[" + key + "] = " + (prop.value.compile(o, LEVEL_LIST)) + ", "; - continue; - } if (prop instanceof Comment) { code += prop.compile(o) + ' '; continue; } - _ref3 = prop.base.cache(o, LEVEL_LIST, ref), sub = _ref3[0], ref = _ref3[1]; - code += "" + obj + "[" + sub + "] = " + ref + ", "; + if (prop instanceof Assign) { + acc = prop.variable.base; + key = acc.compile(o, LEVEL_PAREN); + val = prop.value.compile(o, LEVEL_LIST); + } else { + acc = prop.base; + _ref3 = acc.cache(o, LEVEL_LIST, ref), key = _ref3[0], val = _ref3[1]; + ref = val; + } + key = acc instanceof Literal && IDENTIFIER.test(key) ? '.' + key : '[' + key + ']'; + code += "" + obj + key + " = " + val + ", "; } code += obj; return o.level <= LEVEL_PAREN ? code : "(" + code + ")"; diff --git a/src/nodes.coffee b/src/nodes.coffee index 266b8376..b7f34c36 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -588,15 +588,22 @@ exports.Obj = class Obj extends Base obj = o.scope.freeVariable 'obj' code = "#{obj} = #{ new Obj(@properties.slice 0, idx).compile o }, " for prop, i in @properties.slice idx - if prop instanceof Assign - key = prop.variable.compile o, LEVEL_PAREN - code += "#{obj}[#{key}] = #{ prop.value.compile o, LEVEL_LIST }, " - continue if prop instanceof Comment code += prop.compile(o) + ' ' continue - [sub, ref] = prop.base.cache o, LEVEL_LIST, ref - code += "#{obj}[#{sub}] = #{ref}, " + if prop instanceof Assign + acc = prop.variable.base + key = acc.compile o, LEVEL_PAREN + val = prop.value.compile o, LEVEL_LIST + else + acc = prop.base + [key, val] = acc.cache o, LEVEL_LIST, ref + ref = val + key = if acc instanceof Literal and IDENTIFIER.test key + '.' + key + else + '[' + key + ']' + code += "#{obj}#{key} = #{val}, " code += obj if o.level <= LEVEL_PAREN then code else "(#{code})" diff --git a/test/test_literals.coffee b/test/test_literals.coffee index 8376e8d9..af5259dc 100644 --- a/test/test_literals.coffee +++ b/test/test_literals.coffee @@ -235,12 +235,19 @@ obj = { (4 * 2): 8 ### cached shorthand ### (++i) - "#{'interpolated'}": """#{"key"}""": 123 + ### normal keys ### + key: ok + 's': ok + 0.0: ok + + "#{'interpolated'}": + """#{"nested"}""": 123: 456 ### traling comment ### } -eq obj.interpolated.key, 123 +eq obj.interpolated.nested[123], 456 eq obj[8], 8 eq obj[1], 1 +ok obj.key is obj.s is obj[0] eq 'braceless dynamic key', (key for key of """braceless #{ 0 of ((0):(0)) and 'dynamic' } key""": 0)[0]