diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index 58b45e5d..ad85b479 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -399,14 +399,14 @@ }; parseOptions = function() { - var i, o, source, _len; + var i, o, source, _i, _len; optionParser = new optparse.OptionParser(SWITCHES, BANNER); o = opts = optionParser.parse(process.argv.slice(2)); o.compile || (o.compile = !!o.output); o.run = !(o.compile || o.print || o.lint); o.print = !!(o.print || (o.eval || o.stdio && o.compile)); sources = o.arguments; - for (i = 0, _len = sources.length; i < _len; i++) { + for (i = _i = 0, _len = sources.length; _i < _len; i = ++_i) { source = sources[i]; sourceCode[i] = null; } diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 0af3cc05..3d79665d 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -451,12 +451,17 @@ }; Lexer.prototype.balancedString = function(str, end) { - var i, letter, match, prev, stack, _ref3; + var continueCount, i, letter, match, prev, stack, _i, _ref3; + continueCount = 0; stack = [end]; - for (i = 1, _ref3 = str.length; 1 <= _ref3 ? i < _ref3 : i > _ref3; 1 <= _ref3 ? i++ : i--) { + for (i = _i = 1, _ref3 = str.length; 1 <= _ref3 ? _i < _ref3 : _i > _ref3; i = 1 <= _ref3 ? ++_i : --_i) { + if (continueCount) { + --continueCount; + continue; + } switch (letter = str.charAt(i)) { case '\\': - i++; + ++continueCount; continue; case end: stack.pop(); @@ -467,7 +472,7 @@ if (end === '}' && (letter === '"' || letter === "'")) { stack.push(end = letter); } else if (end === '}' && letter === '/' && (match = HEREGEX.exec(str.slice(i)) || REGEX.exec(str.slice(i)))) { - i += match[0].length - 1; + continueCount += match[0].length - 1; } else if (end === '}' && letter === '{') { stack.push(end = '}'); } else if (end === '"' && prev === '#' && letter === '{') { @@ -479,7 +484,7 @@ }; Lexer.prototype.interpolateString = function(str, options) { - var expr, heredoc, i, inner, interpolated, len, letter, nested, pi, regex, tag, tokens, value, _len, _ref3, _ref4, _ref5; + var expr, heredoc, i, inner, interpolated, len, letter, nested, pi, regex, tag, tokens, value, _i, _len, _ref3, _ref4, _ref5; if (options == null) options = {}; heredoc = options.heredoc, regex = options.regex; tokens = []; @@ -520,7 +525,7 @@ if (!tokens.length) return this.token('STRING', '""'); if (tokens[0][0] !== 'NEOSTRING') tokens.unshift(['', '']); if (interpolated = tokens.length > 1) this.token('(', '('); - for (i = 0, _len = tokens.length; i < _len; i++) { + for (i = _i = 0, _len = tokens.length; _i < _len; i = ++_i) { _ref4 = tokens[i], tag = _ref4[0], value = _ref4[1]; if (i) this.token('+', '+'); if (tag === 'TOKENS') { diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 97b9a960..d1a96514 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -313,10 +313,10 @@ prelude = ""; if (!o.bare) { preludeExps = (function() { - var _len, _ref2, _results; + var _i, _len, _ref2, _results; _ref2 = this.expressions; _results = []; - for (i = 0, _len = _ref2.length; i < _len; i++) { + for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) { exp = _ref2[i]; if (!(exp.unwrap() instanceof Comment)) break; _results.push(exp); @@ -338,10 +338,10 @@ }; Block.prototype.compileWithDeclarations = function(o) { - var assigns, code, declars, exp, i, post, rest, scope, spaced, _len, _ref2, _ref3, _ref4; + var assigns, code, declars, exp, i, post, rest, scope, spaced, _i, _len, _ref2, _ref3, _ref4; code = post = ''; _ref2 = this.expressions; - for (i = 0, _len = _ref2.length; i < _len; i++) { + for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) { exp = _ref2[i]; exp = exp.unwrap(); if (!(exp instanceof Comment || exp instanceof Literal)) break; @@ -597,13 +597,13 @@ _this = this; if (this.unfoldedSoak != null) return this.unfoldedSoak; result = (function() { - var fst, i, ifn, prop, ref, snd, _len, _ref2; + var fst, i, ifn, prop, ref, snd, _i, _len, _ref2; if (ifn = _this.base.unfoldSoak(o)) { Array.prototype.push.apply(ifn.body.properties, _this.properties); return ifn; } _ref2 = _this.properties; - for (i = 0, _len = _ref2.length; i < _len; i++) { + for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) { prop = _ref2[i]; if (!prop.soak) continue; prop.soak = false; @@ -928,17 +928,21 @@ }; Range.prototype.compileNode = function(o) { - var cond, condPart, from, gt, idx, known, lt, stepPart, to, varPart, _ref2, _ref3; + var cond, condPart, from, gt, idx, idxName, known, lt, namedIndex, stepPart, to, varPart, _ref2, _ref3; if (!this.fromVar) this.compileVariables(o); if (!o.index) return this.compileArray(o); known = this.fromNum && this.toNum; idx = del(o, 'index'); + idxName = del(o, 'name'); + namedIndex = idxName && idxName !== idx; varPart = "" + idx + " = " + this.fromC; if (this.toC !== this.toVar) varPart += ", " + this.toC; if (this.step !== this.stepVar) varPart += ", " + this.step; _ref2 = ["" + idx + " <" + this.equals, "" + idx + " >" + this.equals], lt = _ref2[0], gt = _ref2[1]; condPart = this.stepNum ? +this.stepNum > 0 ? "" + lt + " " + this.toVar : "" + gt + " " + this.toVar : known ? ((_ref3 = [+this.fromNum, +this.toNum], from = _ref3[0], to = _ref3[1], _ref3), from <= to ? "" + lt + " " + to : "" + gt + " " + to) : (cond = "" + this.fromVar + " <= " + this.toVar, "" + cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar); - stepPart = this.stepVar ? "" + idx + " += " + this.stepVar : known ? from <= to ? "" + idx + "++" : "" + idx + "--" : "" + cond + " ? " + idx + "++ : " + idx + "--"; + stepPart = this.stepVar ? "" + idx + " += " + this.stepVar : known ? namedIndex ? from <= to ? "++" + idx : "--" + idx : from <= to ? "" + idx + "++" : "" + idx + "--" : namedIndex ? "" + cond + " ? ++" + idx + " : --" + idx : "" + cond + " ? " + idx + "++ : " + idx + "--"; + if (namedIndex) varPart = "" + idxName + " = " + varPart; + if (namedIndex) stepPart = "" + idxName + " = " + stepPart; return "" + varPart + "; " + condPart + "; " + stepPart; }; @@ -1035,9 +1039,9 @@ idt = o.indent += TAB; lastNoncom = this.lastNonComment(this.properties); props = (function() { - var _len2, _results; + var _j, _len2, _results; _results = []; - for (i = 0, _len2 = props.length; i < _len2; i++) { + for (i = _j = 0, _len2 = props.length; _j < _len2; i = ++_j) { prop = props[i]; join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n'; indent = prop instanceof Comment ? '' : idt; @@ -1221,11 +1225,11 @@ Class.prototype.walkBody = function(name, o) { var _this = this; return this.traverseChildren(false, function(child) { - var exps, i, node, _len, _ref2; + var exps, i, node, _i, _len, _ref2; if (child instanceof Class) return false; if (child instanceof Block) { _ref2 = exps = child.expressions; - for (i = 0, _len = _ref2.length; i < _len; i++) { + for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) { node = _ref2[i]; if (node instanceof Value && node.isObject(true)) { exps[i] = _this.addProperties(node, name, o); @@ -1351,7 +1355,7 @@ }; Assign.prototype.compilePatternMatch = function(o) { - var acc, assigns, code, i, idx, isObject, ivar, name, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8; + var acc, assigns, code, i, idx, isObject, ivar, name, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _i, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8; top = o.level === LEVEL_TOP; value = this.value; objects = this.variable.base.objects; @@ -1391,7 +1395,7 @@ assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + vvar); vvar = ref; } - for (i = 0, _len = objects.length; i < _len; i++) { + for (i = _i = 0, _len = objects.length; _i < _len; i = ++_i) { obj = objects[i]; idx = i; if (isObject) { @@ -1506,7 +1510,7 @@ Code.prototype.jumps = NO; Code.prototype.compileNode = function(o) { - var code, exprs, i, idt, lit, p, param, ref, splats, v, val, vars, wasEmpty, _i, _j, _k, _len, _len2, _len3, _len4, _ref2, _ref3, _ref4, _ref5, _ref6; + var code, exprs, i, idt, lit, p, param, ref, splats, v, val, vars, wasEmpty, _i, _j, _k, _l, _len, _len2, _len3, _len4, _ref2, _ref3, _ref4, _ref5, _ref6; o.scope = new Scope(o.scope, this.body, this); o.scope.shared = del(o, 'sharedScope'); o.indent += TAB; @@ -1559,7 +1563,7 @@ (_ref5 = this.body.expressions).unshift.apply(_ref5, exprs); } if (!splats) { - for (i = 0, _len4 = vars.length; i < _len4; i++) { + for (i = _l = 0, _len4 = vars.length; _l < _len4; i = ++_l) { v = vars[i]; o.scope.parameter(vars[i] = v.compile(o)); } @@ -1670,7 +1674,7 @@ }; Splat.compileSplattedArray = function(o, list, apply) { - var args, base, code, i, index, node, _len; + var args, base, code, i, index, node, _i, _len; index = -1; while ((node = list[++index]) && !(node instanceof Splat)) { continue; @@ -1682,7 +1686,7 @@ return "" + (utility('slice')) + ".call(" + code + ")"; } args = list.slice(index); - for (i = 0, _len = args.length; i < _len; i++) { + for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) { node = args[i]; code = node.compile(o, LEVEL_LIST); args[i] = node instanceof Splat ? "" + (utility('slice')) + ".call(" + code + ")" : "[" + code + "]"; @@ -1691,11 +1695,11 @@ return args[0] + (".concat(" + (args.slice(1).join(', ')) + ")"); } base = (function() { - var _i, _len2, _ref2, _results; + var _j, _len2, _ref2, _results; _ref2 = list.slice(0, index); _results = []; - for (_i = 0, _len2 = _ref2.length; _i < _len2; _i++) { - node = _ref2[_i]; + for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { + node = _ref2[_j]; _results.push(node.compile(o, LEVEL_LIST)); } return _results; @@ -1985,10 +1989,10 @@ _ref2 = this.object.cache(o, LEVEL_OP), sub = _ref2[0], ref = _ref2[1]; _ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1]; tests = (function() { - var _len, _ref4, _results; + var _i, _len, _ref4, _results; _ref4 = this.array.base.objects; _results = []; - for (i = 0, _len = _ref4.length; i < _len; i++) { + for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) { item = _ref4[i]; _results.push((i ? ref : sub) + cmp + item.compile(o, LEVEL_ACCESS)); } @@ -2198,7 +2202,7 @@ For.prototype.children = ['body', 'source', 'guard', 'step']; For.prototype.compileNode = function(o) { - var body, defPart, forPart, forVarPart, guardPart, idt1, index, ivar, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, stepvar, svar, varPart, _ref2; + 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, _ref2; body = Block.wrap([this.body]); lastJumps = (_ref2 = last(body.expressions)) != null ? _ref2.jumps() : void 0; if (lastJumps && lastJumps instanceof Return) this.returns = false; @@ -2217,7 +2221,9 @@ }); } if (this.returns) rvar = scope.freeVariable('results'); - ivar = (this.range ? name : index) || scope.freeVariable('i'); + ivar = (this.object && index) || scope.freeVariable('i'); + kvar = (this.range && name) || index || ivar; + kvarAssign = kvar !== ivar ? "" + kvar + " = " : ""; if (this.step && !this.range) stepvar = scope.freeVariable("step"); if (this.pattern) name = ivar; varPart = ''; @@ -2227,6 +2233,7 @@ if (this.range) { forPart = source.compile(merge(o, { index: ivar, + name: name, step: this.step })); } else { @@ -2236,12 +2243,15 @@ svar = ref; } if (name && !this.pattern) { - namePart = "" + name + " = " + svar + "[" + ivar + "]"; + namePart = "" + name + " = " + svar + "[" + kvar + "]"; } if (!this.object) { lvar = scope.freeVariable('len'); - forVarPart = ("" + ivar + " = 0, " + lvar + " = " + svar + ".length") + (this.step ? ", " + stepvar + " = " + (this.step.compile(o, LEVEL_OP)) : ''); - stepPart = this.step ? "" + ivar + " += " + stepvar : "" + ivar + "++"; + forVarPart = "" + kvarAssign + ivar + " = 0, " + lvar + " = " + svar + ".length"; + if (this.step) { + forVarPart += ", " + stepvar + " = " + (this.step.compile(o, LEVEL_OP)); + } + stepPart = "" + kvarAssign + (this.step ? "" + ivar + " += " + stepvar : (kvar !== ivar ? "++" + ivar : "" + ivar + "++")); forPart = "" + forVarPart + "; " + ivar + " < " + lvar + "; " + stepPart; } } @@ -2258,14 +2268,14 @@ } } if (this.pattern) { - body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + ivar + "]"))); + body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + kvar + "]"))); } defPart += this.pluckDirectCall(o, body); if (namePart) varPart = "\n" + idt1 + namePart + ";"; if (this.object) { - forPart = "" + ivar + " in " + svar; + forPart = "" + kvar + " in " + svar; if (this.own) { - guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;"; + guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + kvar + ")) continue;"; } } body = body.compile(merge(o, { @@ -2276,10 +2286,10 @@ }; For.prototype.pluckDirectCall = function(o, body) { - var base, defs, expr, fn, idx, ref, val, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; + var base, defs, expr, fn, idx, ref, val, _i, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; defs = ''; _ref2 = body.expressions; - for (idx = 0, _len = _ref2.length; idx < _len; idx++) { + for (idx = _i = 0, _len = _ref2.length; _i < _len; idx = ++_i) { expr = _ref2[idx]; expr = expr.unwrapAll(); if (!(expr instanceof Call)) continue; @@ -2347,16 +2357,16 @@ }; Switch.prototype.compileNode = function(o) { - var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _len, _len2, _ref2, _ref3, _ref4, _ref5; + var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5; idt1 = o.indent + TAB; idt2 = o.indent = idt1 + TAB; code = this.tab + ("switch (" + (((_ref2 = this.subject) != null ? _ref2.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n"); _ref3 = this.cases; - for (i = 0, _len = _ref3.length; i < _len; i++) { + for (i = _i = 0, _len = _ref3.length; _i < _len; i = ++_i) { _ref4 = _ref3[i], conditions = _ref4[0], block = _ref4[1]; _ref5 = flatten([conditions]); - for (_i = 0, _len2 = _ref5.length; _i < _len2; _i++) { - cond = _ref5[_i]; + for (_j = 0, _len2 = _ref5.length; _j < _len2; _j++) { + cond = _ref5[_j]; if (!this.subject) cond = cond.invert(); code += idt1 + ("case " + (cond.compile(o, LEVEL_PAREN)) + ":\n"); } diff --git a/lib/coffee-script/optparse.js b/lib/coffee-script/optparse.js index a73f7e7c..fb027732 100644 --- a/lib/coffee-script/optparse.js +++ b/lib/coffee-script/optparse.js @@ -9,15 +9,20 @@ } OptionParser.prototype.parse = function(args) { - var arg, i, isOption, matchedRule, options, originalArgs, pos, rule, value, _i, _len, _len2, _ref; + var arg, i, isOption, matchedRule, options, originalArgs, pos, rule, skippingArgument, value, _i, _j, _len, _len2, _ref; options = { arguments: [], literals: [] }; + skippingArgument = false; originalArgs = args; args = normalizeArguments(args); - for (i = 0, _len = args.length; i < _len; i++) { + for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) { arg = args[i]; + if (skippingArgument) { + skippingArgument = false; + continue; + } if (arg === '--') { pos = originalArgs.indexOf('--'); options.arguments = [originalArgs[1 + pos]]; @@ -27,10 +32,10 @@ isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG)); matchedRule = false; _ref = this.rules; - for (_i = 0, _len2 = _ref.length; _i < _len2; _i++) { - rule = _ref[_i]; + for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) { + rule = _ref[_j]; if (rule.shortFlag === arg || rule.longFlag === arg) { - value = rule.hasArgument ? args[i += 1] : true; + value = rule.hasArgument ? (skippingArgument = true, args[i + 1]) : true; options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value; matchedRule = true; break; diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index 4f977d5a..bfd29492 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -50,9 +50,9 @@ }; Rewriter.prototype.removeLeadingNewlines = function() { - var i, tag, _len, _ref; + var i, tag, _i, _len, _ref; _ref = this.tokens; - for (i = 0, _len = _ref.length; i < _len; i++) { + for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { tag = _ref[i][0]; if (tag !== 'TERMINATOR') break; } diff --git a/src/lexer.coffee b/src/lexer.coffee index 8be31160..b504a618 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -407,11 +407,15 @@ exports.Lexer = class Lexer # contents of the string. This method allows us to have strings within # interpolations within strings, ad infinitum. balancedString: (str, end) -> + continueCount = 0 stack = [end] for i in [1...str.length] + if continueCount + --continueCount + continue switch letter = str.charAt i when '\\' - i++ + ++continueCount continue when end stack.pop() @@ -422,7 +426,7 @@ exports.Lexer = class Lexer if end is '}' and letter in ['"', "'"] stack.push end = letter else if end is '}' and letter is '/' and match = (HEREGEX.exec(str.slice i) or REGEX.exec(str.slice i)) - i += match[0].length - 1 + continueCount += match[0].length - 1 else if end is '}' and letter is '{' stack.push end = '}' else if end is '"' and prev is '#' and letter is '{' diff --git a/src/nodes.coffee b/src/nodes.coffee index c07bfdbc..8bf04386 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -677,6 +677,8 @@ exports.Range = class Range extends Base # Set up endpoints. known = @fromNum and @toNum idx = del o, 'index' + idxName = del o, 'name' + namedIndex = idxName and idxName isnt idx varPart = "#{idx} = #{@fromC}" varPart += ", #{@toC}" if @toC isnt @toVar varPart += ", #{@step}" if @step isnt @stepVar @@ -696,9 +698,18 @@ exports.Range = class Range extends Base stepPart = if @stepVar "#{idx} += #{@stepVar}" else if known - if from <= to then "#{idx}++" else "#{idx}--" + if namedIndex + if from <= to then "++#{idx}" else "--#{idx}" + else + if from <= to then "#{idx}++" else "#{idx}--" else - "#{cond} ? #{idx}++ : #{idx}--" + if namedIndex + "#{cond} ? ++#{idx} : --#{idx}" + else + "#{cond} ? #{idx}++ : #{idx}--" + + varPart = "#{idxName} = #{varPart}" if namedIndex + stepPart = "#{idxName} = #{stepPart}" if namedIndex # The final loop body. "#{varPart}; #{condPart}; #{stepPart}" @@ -1606,7 +1617,9 @@ exports.For = class For extends While scope.find(name, immediate: yes) if name and not @pattern scope.find(index, immediate: yes) if index rvar = scope.freeVariable 'results' if @returns - ivar = (if @range then name else index) or scope.freeVariable 'i' + ivar = (@object and index) or scope.freeVariable 'i' + kvar = (@range and name) or index or ivar + kvarAssign = if kvar isnt ivar then "#{kvar} = " else "" # the `_by` variable is created twice in `Range`s if we don't prevent it from being declared here stepvar = scope.freeVariable "step" if @step and not @range name = ivar if @pattern @@ -1615,18 +1628,19 @@ exports.For = class For extends While defPart = '' idt1 = @tab + TAB if @range - forPart = source.compile merge(o, {index: ivar, @step}) + forPart = source.compile merge(o, {index: ivar, name, @step}) else svar = @source.compile o, LEVEL_LIST if (name or @own) and not IDENTIFIER.test svar defPart = "#{@tab}#{ref = scope.freeVariable 'ref'} = #{svar};\n" svar = ref if name and not @pattern - namePart = "#{name} = #{svar}[#{ivar}]" + namePart = "#{name} = #{svar}[#{kvar}]" unless @object lvar = scope.freeVariable 'len' - forVarPart = "#{ivar} = 0, #{lvar} = #{svar}.length" + if @step then ", #{stepvar} = #{@step.compile(o, LEVEL_OP)}" else '' - stepPart = if @step then "#{ivar} += #{stepvar}" else "#{ivar}++" + forVarPart = "#{kvarAssign}#{ivar} = 0, #{lvar} = #{svar}.length" + forVarPart += ", #{stepvar} = #{@step.compile o, LEVEL_OP}" if @step + stepPart = "#{kvarAssign}#{if @step then "#{ivar} += #{stepvar}" else (if kvar isnt ivar then "++#{ivar}" else "#{ivar}++")}" forPart = "#{forVarPart}; #{ivar} < #{lvar}; #{stepPart}" if @returns resultPart = "#{@tab}#{rvar} = [];\n" @@ -1638,12 +1652,12 @@ exports.For = class For extends While else body = Block.wrap [new If @guard, body] if @guard if @pattern - body.expressions.unshift new Assign @name, new Literal "#{svar}[#{ivar}]" + body.expressions.unshift new Assign @name, new Literal "#{svar}[#{kvar}]" defPart += @pluckDirectCall o, body varPart = "\n#{idt1}#{namePart};" if namePart if @object - forPart = "#{ivar} in #{svar}" - guardPart = "\n#{idt1}if (!#{utility 'hasProp'}.call(#{svar}, #{ivar})) continue;" if @own + forPart = "#{kvar} in #{svar}" + guardPart = "\n#{idt1}if (!#{utility 'hasProp'}.call(#{svar}, #{kvar})) continue;" if @own body = body.compile merge(o, indent: idt1), LEVEL_TOP body = '\n' + body + '\n' if body """ diff --git a/src/optparse.coffee b/src/optparse.coffee index 0b453d37..94bfb94a 100644 --- a/src/optparse.coffee +++ b/src/optparse.coffee @@ -25,9 +25,13 @@ exports.OptionParser = class OptionParser # for interpreting the options object. parse: (args) -> options = arguments: [], literals: [] + skippingArgument = no originalArgs = args args = normalizeArguments args for arg, i in args + if skippingArgument + skippingArgument = no + continue if arg is '--' pos = originalArgs.indexOf '--' options.arguments = [originalArgs[1 + pos]] @@ -37,7 +41,11 @@ exports.OptionParser = class OptionParser matchedRule = no for rule in @rules if rule.shortFlag is arg or rule.longFlag is arg - value = if rule.hasArgument then args[i += 1] else true + value = if rule.hasArgument + skippingArgument = yes + args[i + 1] + else + true options[rule.name] = if rule.isList then (options[rule.name] or []).concat value else value matchedRule = yes break diff --git a/test/comprehensions.coffee b/test/comprehensions.coffee index 428b1b6a..0e8acb9c 100644 --- a/test/comprehensions.coffee +++ b/test/comprehensions.coffee @@ -460,3 +460,28 @@ test "#1850: inner `for` should not be expression-ized if `return`ing", -> for b in [1..9] c = Math.sqrt a*a + b*b return String [a, b, c] unless c % 1 + +test "#1910: loop index should be mutable within a loop iteration and immutable between loop iterations", -> + n = 1 + iterations = 0 + arr = [0..n] + for v, k in arr + ++iterations + v = k = 5 + eq 5, k + eq 2, k + eq 2, iterations + + iterations = 0 + for v in [0..n] + ++iterations + eq 2, k + eq 2, iterations + + arr = ([v, v + 1] for v in [0..5]) + iterations = 0 + for own [v0, v1], k in arr when v0 + k += 3 + ++iterations + eq 6, k + eq 5, iterations diff --git a/test/control_flow.coffee b/test/control_flow.coffee index a502e1ad..2aa288d0 100644 --- a/test/control_flow.coffee +++ b/test/control_flow.coffee @@ -422,11 +422,9 @@ test "Issue #997. Switch doesn't fallthrough.", -> test "Throw should be usable as an expression.", -> - + try false or throw 'up' throw new Error 'failed' catch e ok e is 'up' - - \ No newline at end of file diff --git a/test/option_parser.coffee b/test/option_parser.coffee index eef1fd08..835f33af 100644 --- a/test/option_parser.coffee +++ b/test/option_parser.coffee @@ -15,14 +15,14 @@ opt = new OptionParser [ result = opt.parse ['one', 'two', 'three', '-r', 'dir'] -ok result.arguments.length is 5 -ok result.arguments[3] is '-r' +eq 5, result.arguments.length +eq '-r', result.arguments[3] result = opt.parse ['--optional', '-r', 'folder', 'one', 'two'] -ok result.optional is true -ok result.required is 'folder' -ok result.arguments.join(' ') is 'one two' +ok result.optional +eq 'folder', result.required +eq 'one two', result.arguments.join ' ' result = opt.parse ['-l', 'one.txt', '-l', 'two.txt', 'three']