Skip to content

Commit d72daca

Browse files
committed
simpler implementation of the previous commit
1 parent c37202e commit d72daca

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

lib/coffee-script/nodes.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -2566,7 +2566,7 @@
25662566
For.prototype.children = ['body', 'source', 'guard', 'step'];
25672567

25682568
For.prototype.compileNode = function(o) {
2569-
var body, defPart, forPart, forVarPart, guardPart, idt1, index, ivar, kvar, kvarAssign, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, stepvar, svar, varPart, _base, _ref2;
2569+
var body, defPart, forPart, forVarPart, guardPart, idt1, index, ivar, kvar, kvarAssign, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, step, stepPart, stepVar, svar, varPart, _ref2, _ref3;
25702570
body = Block.wrap([this.body]);
25712571
lastJumps = (_ref2 = last(body.expressions)) != null ? _ref2.jumps() : void 0;
25722572
if (lastJumps && lastJumps instanceof Return) {
@@ -2589,8 +2589,7 @@
25892589
kvar = (this.range && name) || index || ivar;
25902590
kvarAssign = kvar !== ivar ? "" + kvar + " = " : "";
25912591
if (this.step && !this.range) {
2592-
this.complexStep = !(typeof (_base = this.step).isSimpleNumber === "function" ? _base.isSimpleNumber() : void 0);
2593-
stepvar = this.complexStep ? scope.freeVariable("step") : this.step.compile(o, LEVEL_OP);
2592+
_ref3 = this.step.cache(o, LEVEL_LIST), step = _ref3[0], stepVar = _ref3[1];
25942593
}
25952594
if (this.pattern) {
25962595
name = ivar;
@@ -2617,10 +2616,10 @@
26172616
if (!this.object) {
26182617
lvar = scope.freeVariable('len');
26192618
forVarPart = "" + kvarAssign + ivar + " = 0, " + lvar + " = " + svar + ".length";
2620-
if (this.complexStep) {
2621-
forVarPart += ", " + stepvar + " = " + (this.step.compile(o, LEVEL_OP));
2619+
if (step !== stepVar) {
2620+
forVarPart += ", " + step;
26222621
}
2623-
stepPart = "" + kvarAssign + (this.step ? "" + ivar + " += " + stepvar : (kvar !== ivar ? "++" + ivar : "" + ivar + "++"));
2622+
stepPart = "" + kvarAssign + (this.step ? "" + ivar + " += " + stepVar : (kvar !== ivar ? "++" + ivar : "" + ivar + "++"));
26242623
forPart = "" + forVarPart + "; " + ivar + " < " + lvar + "; " + stepPart;
26252624
}
26262625
}

src/nodes.coffee

+3-4
Original file line numberDiff line numberDiff line change
@@ -1743,8 +1743,7 @@ exports.For = class For extends While
17431743
kvarAssign = if kvar isnt ivar then "#{kvar} = " else ""
17441744
# the `_by` variable is created twice in `Range`s if we don't prevent it from being declared here
17451745
if @step and not @range
1746-
@complexStep = not @step.isSimpleNumber?()
1747-
stepvar = if @complexStep then scope.freeVariable "step" else @step.compile(o, LEVEL_OP)
1746+
[step, stepVar] = @step.cache o, LEVEL_LIST
17481747
name = ivar if @pattern
17491748
varPart = ''
17501749
guardPart = ''
@@ -1762,8 +1761,8 @@ exports.For = class For extends While
17621761
unless @object
17631762
lvar = scope.freeVariable 'len'
17641763
forVarPart = "#{kvarAssign}#{ivar} = 0, #{lvar} = #{svar}.length"
1765-
forVarPart += ", #{stepvar} = #{@step.compile o, LEVEL_OP}" if @complexStep
1766-
stepPart = "#{kvarAssign}#{if @step then "#{ivar} += #{stepvar}" else (if kvar isnt ivar then "++#{ivar}" else "#{ivar}++")}"
1764+
forVarPart += ", #{step}" if step isnt stepVar
1765+
stepPart = "#{kvarAssign}#{if @step then "#{ivar} += #{stepVar}" else (if kvar isnt ivar then "++#{ivar}" else "#{ivar}++")}"
17671766
forPart = "#{forVarPart}; #{ivar} < #{lvar}; #{stepPart}"
17681767
if @returns
17691768
resultPart = "#{@tab}#{rvar} = [];\n"

0 commit comments

Comments
 (0)