mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
splice literals should evaluate to their right hand side, like any other type of assignment.
This commit is contained in:
11
lib/nodes.js
11
lib/nodes.js
@@ -1208,7 +1208,7 @@
|
||||
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
|
||||
};
|
||||
Assign.prototype.compileSplice = function(o) {
|
||||
var exclusive, from, fromDecl, fromRef, name, to, val, _ref, _ref2;
|
||||
var code, exclusive, from, fromDecl, fromRef, name, to, valDef, valRef, _ref, _ref2, _ref3;
|
||||
_ref = this.variable.properties.pop().range, from = _ref.from, to = _ref.to, exclusive = _ref.exclusive;
|
||||
name = this.variable.compile(o);
|
||||
_ref2 = (from != null ? from.cache(o, LEVEL_OP) : void 0) || ['0', '0'], fromDecl = _ref2[0], fromRef = _ref2[1];
|
||||
@@ -1227,8 +1227,13 @@
|
||||
} else {
|
||||
to = "9e9";
|
||||
}
|
||||
val = this.value.compile(o);
|
||||
return "[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat(" + val + "))";
|
||||
_ref3 = this.value.cache(o, LEVEL_LIST), valDef = _ref3[0], valRef = _ref3[1];
|
||||
code = "[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat(" + valDef + ")), " + valRef;
|
||||
if (o.level > LEVEL_TOP) {
|
||||
return "(" + code + ")";
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
};
|
||||
return Assign;
|
||||
})();
|
||||
|
||||
@@ -983,8 +983,9 @@ exports.Assign = class Assign extends Base
|
||||
to += ' + 1' unless exclusive
|
||||
else
|
||||
to = "9e9"
|
||||
val = @value.compile(o)
|
||||
"[].splice.apply(#{name}, [#{fromDecl}, #{to}].concat(#{val}))"
|
||||
[valDef, valRef] = @value.cache o, LEVEL_LIST
|
||||
code = "[].splice.apply(#{name}, [#{fromDecl}, #{to}].concat(#{valDef})), #{valRef}"
|
||||
if o.level > LEVEL_TOP then "(#{code})" else code
|
||||
|
||||
#### Code
|
||||
|
||||
|
||||
@@ -175,3 +175,12 @@ test "splicing to the end, against a one-time function", ->
|
||||
fn()[0..] = 1
|
||||
|
||||
arrayEq ary, [1]
|
||||
|
||||
test "the return value of a splice literal should be the RHS", ->
|
||||
ary = [0, 0, 0]
|
||||
eq (ary[0..1] = 2), 2
|
||||
|
||||
ary = [0, 0, 0]
|
||||
eq (ary[0..] = 3), 3
|
||||
|
||||
arrayEq [ary[0..0] = 0], [0]
|
||||
|
||||
Reference in New Issue
Block a user