mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-14 01:07:55 -05:00
simplifying generated output for common-case splices.
This commit is contained in:
16
lib/nodes.js
16
lib/nodes.js
@@ -1163,15 +1163,23 @@
|
|||||||
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
|
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
|
||||||
};
|
};
|
||||||
Assign.prototype.compileSplice = function(o) {
|
Assign.prototype.compileSplice = function(o) {
|
||||||
var from, name, plus, range, ref, to, val;
|
var from, name, plus, range, to, val;
|
||||||
range = this.variable.properties.pop().range;
|
range = this.variable.properties.pop().range;
|
||||||
name = this.variable.compile(o);
|
name = this.variable.compile(o);
|
||||||
plus = range.exclusive ? '' : ' + 1';
|
plus = range.exclusive ? '' : ' + 1';
|
||||||
from = range.from ? range.from.compile(o) : '0';
|
from = range.from ? range.from.compile(o) : '0';
|
||||||
to = range.to ? range.to.compile(o) + ' - ' + from + plus : "" + name + ".length";
|
if (!range.to) {
|
||||||
ref = o.scope.freeVariable('ref');
|
to = "" + name + ".length";
|
||||||
|
}
|
||||||
|
if (!to) {
|
||||||
|
if (range.from && range.from.isSimpleNumber() && range.to.isSimpleNumber()) {
|
||||||
|
to = (+range.to.compile(o)) - +from + +plus;
|
||||||
|
} else {
|
||||||
|
to = range.to.compile(o) + ' - ' + from + plus;
|
||||||
|
}
|
||||||
|
}
|
||||||
val = this.value.compile(o);
|
val = this.value.compile(o);
|
||||||
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
|
return "[].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + val + "))";
|
||||||
};
|
};
|
||||||
return Assign;
|
return Assign;
|
||||||
}();
|
}();
|
||||||
|
|||||||
@@ -944,13 +944,17 @@ exports.Assign = class Assign extends Base
|
|||||||
# `Array#splice` method.
|
# `Array#splice` method.
|
||||||
compileSplice: (o) ->
|
compileSplice: (o) ->
|
||||||
{range} = @variable.properties.pop()
|
{range} = @variable.properties.pop()
|
||||||
name = @variable.compile o
|
name = @variable.compile o
|
||||||
plus = if range.exclusive then '' else ' + 1'
|
plus = if range.exclusive then '' else ' + 1'
|
||||||
from = if range.from then range.from.compile(o) else '0'
|
from = if range.from then range.from.compile(o) else '0'
|
||||||
to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
|
to = "#{name}.length" unless range.to
|
||||||
ref = o.scope.freeVariable 'ref'
|
unless to
|
||||||
val = @value.compile(o)
|
if range.from and range.from.isSimpleNumber() and range.to.isSimpleNumber()
|
||||||
"([].splice.apply(#{name}, [#{from}, #{to}].concat(#{ref} = #{val})), #{ref})"
|
to = (+range.to.compile(o)) - +from + +plus
|
||||||
|
else
|
||||||
|
to = range.to.compile(o) + ' - ' + from + plus
|
||||||
|
val = @value.compile(o)
|
||||||
|
"[].splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))"
|
||||||
|
|
||||||
#### Code
|
#### Code
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user