mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
dynakeys: {(x), y}
now compiles correctly
This commit is contained in:
parent
863f3f6b47
commit
0d6d221568
3 changed files with 34 additions and 16 deletions
20
lib/nodes.js
20
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 + ")";
|
||||
|
|
|
@ -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})"
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Add table
Reference in a new issue