From a5ba0c27aea0453dbe8924d5269ff1e8a403ccf3 Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Mon, 8 Aug 2011 12:27:53 -0400 Subject: [PATCH] breaking long lines created by 803a7d06e304854396216bbdf983e9eefd40a1dd --- lib/command.js | 6 +++-- lib/lexer.js | 18 ++++++++----- lib/nodes.js | 70 ++++++++++++++++++++++++++++++++---------------- lib/rewriter.js | 15 +++++++---- src/nodes.coffee | 3 ++- 5 files changed, 75 insertions(+), 37 deletions(-) diff --git a/lib/command.js b/lib/command.js index 04ed2df0..cc2c98a4 100644 --- a/lib/command.js +++ b/lib/command.js @@ -59,7 +59,8 @@ return total; }; return path.exists(source, function(exists) { - if (topLevel && !exists && source.slice(-7) !== '.coffee') return compile("" + source + ".coffee", sourceIndex, topLevel); + if (topLevel && !exists && source.slice(-7) !== '.coffee') + return compile("" + source + ".coffee", sourceIndex, topLevel); if (topLevel && !exists) throw new Error("File not found: " + source); return fs.stat(source, function(err, stats) { if (err) throw err; @@ -80,7 +81,8 @@ unprocessed[sourceIndex] -= 1; if (opts.join) { contents[sourceIndex] = helpers.compact([contents[sourceIndex], code.toString()]).join('\n'); - if (helpers.compact(contents).length > 0 && remaining_files() === 0) return compileJoin(); + if (helpers.compact(contents).length > 0 && remaining_files() === 0) + return compileJoin(); } else { return compileScript(source, code.toString(), base); } diff --git a/lib/lexer.js b/lib/lexer.js index 3f574211..8f0105d1 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -178,7 +178,8 @@ return length; } prev = last(this.tokens); - if (prev && (_ref2 = prev[0], __indexOf.call((prev.spaced ? NOT_REGEX : NOT_SPACED_REGEX), _ref2) >= 0)) return 0; + if (prev && (_ref2 = prev[0], __indexOf.call((prev.spaced ? NOT_REGEX : NOT_SPACED_REGEX), _ref2) >= 0)) + return 0; if (!(match = REGEX.exec(this.chunk))) return 0; regex = match[0]; this.token('REGEX', regex === '//' ? '/(?:)/' : regex); @@ -210,7 +211,8 @@ tokens.push(['+', '+']); } tokens.pop(); - if (((_ref4 = tokens[0]) != null ? _ref4[0] : void 0) !== 'STRING') this.tokens.push(['STRING', '""'], ['+', '+']); + if (((_ref4 = tokens[0]) != null ? _ref4[0] : void 0) !== 'STRING') + this.tokens.push(['STRING', '""'], ['+', '+']); (_ref5 = this.tokens).push.apply(_ref5, tokens); if (flags) this.tokens.push([',', ','], ['STRING', '"' + flags + '"']); this.token(')', ')'); @@ -302,7 +304,8 @@ tag = value; prev = last(this.tokens); if (value === '=' && prev) { - if (!prev[1].reserved && (_ref2 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref2) >= 0)) this.assignmentError(); + if (!prev[1].reserved && (_ref2 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref2) >= 0)) + this.assignmentError(); if ((_ref3 = prev[1]) === '||' || _ref3 === '&&') { prev[0] = 'COMPOUND_ASSIGN'; prev[1] += '='; @@ -345,12 +348,14 @@ var attempt, herecomment, indent, match, _ref2; indent = options.indent, herecomment = options.herecomment; if (herecomment) { - if (HEREDOC_ILLEGAL.test(doc)) throw new Error("block comment cannot contain \"*/\", starting on line " + (this.line + 1)); + if (HEREDOC_ILLEGAL.test(doc)) + throw new Error("block comment cannot contain \"*/\", starting on line " + (this.line + 1)); if (doc.indexOf('\n') <= 0) return doc; } else { while (match = HEREDOC_INDENT.exec(doc)) { attempt = match[1]; - if (indent === null || (0 < (_ref2 = attempt.length) && _ref2 < indent.length)) indent = attempt; + if (indent === null || (0 < (_ref2 = attempt.length) && _ref2 < indent.length)) + indent = attempt; } } if (indent) doc = doc.replace(RegExp("\\n" + indent, "g"), '\n'); @@ -431,7 +436,8 @@ i += 1; continue; } - if (!(letter === '#' && str.charAt(i + 1) === '{' && (expr = this.balancedString(str.slice(i + 1), '}')))) continue; + if (!(letter === '#' && str.charAt(i + 1) === '{' && (expr = this.balancedString(str.slice(i + 1), '}')))) + continue; if (pi < i) tokens.push(['NEOSTRING', str.slice(pi, i)]); inner = expr.slice(1, -1); if (inner.length) { diff --git a/lib/nodes.js b/lib/nodes.js index aa865da3..5d1cb4f4 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -44,7 +44,8 @@ } }; Base.prototype.compileClosure = function(o) { - if (this.jumps() || this instanceof Throw) throw SyntaxError('cannot use a pure statement in an expression.'); + if (this.jumps() || this instanceof Throw) + throw SyntaxError('cannot use a pure statement in an expression.'); o.sharedScope = true; return Closure.wrap(this).compileNode(o); }; @@ -66,7 +67,8 @@ Base.prototype.compileLoopReference = function(o, name) { var src, tmp; src = tmp = this.compile(o, LEVEL_LIST); - if (!((-Infinity < +src && +src < Infinity) || IDENTIFIER.test(src) && o.scope.check(src, true))) src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src; + if (!((-Infinity < +src && +src < Infinity) || IDENTIFIER.test(src) && o.scope.check(src, true))) + src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src; return [src, tmp]; }; Base.prototype.makeReturn = function() { @@ -280,7 +282,8 @@ assigns = scope.hasAssignments; if ((declars || assigns) && i) code += '\n'; if (declars) code += "" + this.tab + "var " + (scope.declaredVariables().join(', ')) + ";\n"; - if (assigns) code += "" + this.tab + "var " + (multident(scope.assignedVariables().join(', '), this.tab)) + ";\n"; + if (assigns) + code += "" + this.tab + "var " + (multident(scope.assignedVariables().join(', '), this.tab)) + ";\n"; } return code + post; }; @@ -429,7 +432,8 @@ Value.prototype.cacheReference = function(o) { var base, bref, name, nref; name = last(this.properties); - if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) return [this, this]; + if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) + return [this, this]; base = new Value(this.base, this.properties.slice(0, -1)); if (base.isComplex()) { bref = new Literal(o.scope.freeVariable('base')); @@ -708,7 +712,8 @@ }); _ref2 = this.from.cache(o, LEVEL_LIST), this.fromC = _ref2[0], this.fromVar = _ref2[1]; _ref3 = this.to.cache(o, LEVEL_LIST), this.toC = _ref3[0], this.toVar = _ref3[1]; - if (step = del(o, 'step')) _ref4 = step.cache(o, LEVEL_LIST), this.step = _ref4[0], this.stepVar = _ref4[1]; + if (step = del(o, 'step')) + _ref4 = step.cache(o, LEVEL_LIST), this.step = _ref4[0], this.stepVar = _ref4[1]; _ref5 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref5[0], this.toNum = _ref5[1]; if (this.stepVar) return this.stepNum = this.stepVar.match(SIMPLENUM); }; @@ -772,7 +777,8 @@ _ref2 = this.range, to = _ref2.to, from = _ref2.from; fromStr = from && from.compile(o, LEVEL_PAREN) || '0'; compiled = to && to.compile(o, LEVEL_PAREN); - if (to && !(!this.range.exclusive && +compiled === -1)) toStr = ', ' + (this.range.exclusive ? compiled : SIMPLENUM.test(compiled) ? (+compiled + 1).toString() : "(" + compiled + " + 1) || 9e9"); + if (to && !(!this.range.exclusive && +compiled === -1)) + toStr = ', ' + (this.range.exclusive ? compiled : SIMPLENUM.test(compiled) ? (+compiled + 1).toString() : "(" + compiled + " + 1) || 9e9"); return ".slice(" + fromStr + (toStr || '') + ")"; }; return Slice; @@ -797,7 +803,8 @@ if (this.generated) { for (_i = 0, _len = props.length; _i < _len; _i++) { node = props[_i]; - if (node instanceof Value) throw new Error('cannot have an implicit value in an implicit object'); + if (node instanceof Value) + throw new Error('cannot have an implicit value in an implicit object'); } } idt = o.indent += TAB; @@ -809,7 +816,8 @@ prop = props[i]; join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n'; indent = prop instanceof Comment ? '' : idt; - if (prop instanceof Value && prop["this"]) prop = new Assign(prop.properties[0].name, prop, 'object'); + if (prop instanceof Value && prop["this"]) + prop = new Assign(prop.properties[0].name, prop, 'object'); if (!(prop instanceof Comment)) { if (!(prop instanceof Assign)) prop = new Assign(prop, prop, 'object'); (prop.variable.base || prop.variable).asKey = true; @@ -937,7 +945,8 @@ assign = new Assign(new Literal(this.externalCtor), func); } } else { - if (!assign.variable["this"]) assign.variable = new Value(new Literal(name), [new Access(base, 'proto')]); + if (!assign.variable["this"]) + assign.variable = new Value(new Literal(name), [new Access(base, 'proto')]); if (func instanceof Code && func.bound && !assign.variable["this"]) { this.boundFuncs.push(base); func.bound = false; @@ -958,7 +967,8 @@ _ref2 = exps = child.expressions; for (i = 0, _len = _ref2.length; i < _len; i++) { node = _ref2[i]; - if (node instanceof Value && node.isObject(true)) exps[i] = this.addProperties(node, name, o); + if (node instanceof Value && node.isObject(true)) + exps[i] = this.addProperties(node, name, o); } return child.expressions = exps = flatten(exps); } @@ -967,8 +977,10 @@ Class.prototype.ensureConstructor = function(name) { if (!this.ctor) { this.ctor = new Code; - if (this.parent) this.ctor.body.push(new Literal("" + name + ".__super__.constructor.apply(this, arguments)")); - if (this.externalCtor) this.ctor.body.push(new Literal("" + this.externalCtor + ".apply(this, arguments)")); + if (this.parent) + this.ctor.body.push(new Literal("" + name + ".__super__.constructor.apply(this, arguments)")); + if (this.externalCtor) + this.ctor.body.push(new Literal("" + this.externalCtor + ".apply(this, arguments)")); this.body.expressions.unshift(this.ctor); } this.ctor.ctor = this.ctor.name = name; @@ -1017,10 +1029,12 @@ if (isValue = this.variable instanceof Value) { if (this.variable.isArray() || this.variable.isObject()) return this.compilePatternMatch(o); if (this.variable.isSplice()) return this.compileSplice(o); - if ((_ref2 = this.context) === '||=' || _ref2 === '&&=' || _ref2 === '?=') return this.compileConditional(o); + if ((_ref2 = this.context) === '||=' || _ref2 === '&&=' || _ref2 === '?=') + return this.compileConditional(o); } name = this.variable.compile(o, LEVEL_LIST); - if (!(this.context || this.variable.isAssignable())) throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned."); + if (!(this.context || this.variable.isAssignable())) + throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned."); if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) { if (this.param) { o.scope.add(name, 'var'); @@ -1498,7 +1512,8 @@ Op.prototype.compileUnary = function(o) { var op, parts; parts = [op = this.operator]; - if ((op === 'new' || op === 'typeof' || op === 'delete') || (op === '+' || op === '-') && this.first instanceof Op && this.first.operator === op) parts.push(' '); + if ((op === 'new' || op === 'typeof' || op === 'delete') || (op === '+' || op === '-') && this.first instanceof Op && this.first.operator === op) + parts.push(' '); if (op === 'new' && this.first.isStatement(o)) this.first = new Parens(this.first); parts.push(this.first.compile(o, LEVEL_OP)); if (this.flip) parts.reverse(); @@ -1677,7 +1692,8 @@ this.own = !!source.own; this.object = !!source.object; if (this.object) _ref2 = [this.index, this.name], this.name = _ref2[0], this.index = _ref2[1]; - if (this.index instanceof Value) throw SyntaxError('index cannot be a pattern matching expression'); + if (this.index instanceof Value) + throw SyntaxError('index cannot be a pattern matching expression'); this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length; this.pattern = this.name instanceof Value; if (this.range && this.index) throw SyntaxError('indexes do not apply to range loops'); @@ -1749,12 +1765,14 @@ if (this.guard) body = Block.wrap([new If(this.guard, body)]); } } - if (this.pattern) body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + ivar + "]"))); + if (this.pattern) + body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + ivar + "]"))); defPart += this.pluckDirectCall(o, body); if (namePart) varPart = "\n" + idt1 + namePart + ";"; if (this.object) { forPart = "" + ivar + " in " + svar; - if (this.own) guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;"; + if (this.own) + guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;"; } body = body.compile(merge(o, { indent: idt1 @@ -1771,7 +1789,8 @@ expr = expr.unwrapAll(); if (!(expr instanceof Call)) continue; val = expr.variable.unwrapAll(); - if (!((val instanceof Code) || (val instanceof Value && ((_ref3 = val.base) != null ? _ref3.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((_ref4 = (_ref5 = val.properties[0].name) != null ? _ref5.value : void 0) === 'call' || _ref4 === 'apply')))) continue; + if (!((val instanceof Code) || (val instanceof Value && ((_ref3 = val.base) != null ? _ref3.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((_ref4 = (_ref5 = val.properties[0].name) != null ? _ref5.value : void 0) === 'call' || _ref4 === 'apply')))) + continue; fn = ((_ref6 = val.base) != null ? _ref6.unwrapAll() : void 0) || val; ref = new Literal(o.scope.freeVariable('fn')); base = new Value(ref); @@ -1833,10 +1852,12 @@ if (body = block.compile(o, LEVEL_TOP)) code += body + '\n'; if (i === this.cases.length - 1 && !this.otherwise) break; expr = this.lastNonComment(block.expressions); - if (expr instanceof Return || (expr instanceof Literal && expr.jumps() && expr.value !== 'debugger')) continue; + if (expr instanceof Return || (expr instanceof Literal && expr.jumps() && expr.value !== 'debugger')) + continue; code += idt2 + 'break;\n'; } - if (this.otherwise && this.otherwise.expressions.length) code += idt1 + ("default:\n" + (this.otherwise.compile(o, LEVEL_TOP)) + "\n"); + if (this.otherwise && this.otherwise.expressions.length) + code += idt1 + ("default:\n" + (this.otherwise.compile(o, LEVEL_TOP)) + "\n"); return code + this.tab + '}'; }; return Switch; @@ -1897,7 +1918,7 @@ } }; If.prototype.compileStatement = function(o) { - var body, bodyc, child, cond, exeq, ifPart; + var body, bodyc, child, cond, exeq, ifPart, separator; child = del(o, 'chainChild'); exeq = del(o, 'isExistentialEquals'); if (exeq) { @@ -1909,7 +1930,10 @@ o.indent += TAB; body = this.ensureBlock(this.body); bodyc = body.compile(o); - if (body.expressions.length === 1 && !this.elseBody && !child && bodyc && -1 === bodyc.indexOf('\n')) return "" + this.tab + "if (" + cond + ") " + (bodyc.replace(/^\s+/, '')); + if (body.expressions.length === 1 && !this.elseBody && !child && bodyc && -1 === bodyc.indexOf('\n')) { + separator = cond.length + bodyc.length > 100 ? "\n" + this.tab + TAB : ' '; + return "" + this.tab + "if (" + cond + ")" + separator + (bodyc.replace(/^\s+/, '')); + } if (bodyc) bodyc = "\n" + bodyc + "\n" + this.tab; ifPart = "if (" + cond + ") {" + bodyc + "}"; if (!child) ifPart = this.tab + ifPart; diff --git a/lib/rewriter.js b/lib/rewriter.js index 89a21831..7e581add 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -59,7 +59,8 @@ Rewriter.prototype.removeMidExpressionNewlines = function() { return this.scanTokens(function(token, i, tokens) { var _ref; - if (!(token[0] === 'TERMINATOR' && (_ref = this.tag(i + 1), __indexOf.call(EXPRESSION_CLOSE, _ref) >= 0))) return 1; + if (!(token[0] === 'TERMINATOR' && (_ref = this.tag(i + 1), __indexOf.call(EXPRESSION_CLOSE, _ref) >= 0))) + return 1; tokens.splice(i, 1); return 0; }); @@ -120,7 +121,8 @@ start = stack.pop(); return 1; } - if (!(tag === ':' && ((ago = this.tag(i - 2)) === ':' || ((_ref2 = stack[stack.length - 1]) != null ? _ref2[0] : void 0) !== '{'))) return 1; + if (!(tag === ':' && ((ago = this.tag(i - 2)) === ':' || ((_ref2 = stack[stack.length - 1]) != null ? _ref2[0] : void 0) !== '{'))) + return 1; stack.push(['{']); idx = ago === '@' ? i - 2 : i - 1; while (this.tag(idx - 2) === 'HERECOMMENT') { @@ -154,15 +156,18 @@ if (__indexOf.call(LINEBREAKS, tag) >= 0) noCall = false; if (prev && !prev.spaced && tag === '?') token.call = true; if (token.fromThen) return 1; - if (!(callObject || (prev != null ? prev.spaced : void 0) && (prev.call || (_ref3 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref3) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) return 1; + if (!(callObject || (prev != null ? prev.spaced : void 0) && (prev.call || (_ref3 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref3) >= 0)) && (__indexOf.call(IMPLICIT_CALL, tag) >= 0 || !(token.spaced || token.newLine) && __indexOf.call(IMPLICIT_UNSPACED_CALL, tag) >= 0))) + return 1; tokens.splice(i, 0, ['CALL_START', '(', token[2]]); this.detectEnd(i + 1, function(token, i) { var post, _ref4; tag = token[0]; if (!seenSingle && token.fromThen) return true; - if (tag === 'IF' || tag === 'ELSE' || tag === 'CATCH' || tag === '->' || tag === '=>') seenSingle = true; + if (tag === 'IF' || tag === 'ELSE' || tag === 'CATCH' || tag === '->' || tag === '=>') + seenSingle = true; if (tag === 'IF' || tag === 'ELSE' || tag === 'SWITCH' || tag === 'TRY') seenControl = true; - if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') return true; + if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') + return true; return !token.generated && this.tag(i - 1) !== ',' && (__indexOf.call(IMPLICIT_END, tag) >= 0 || (tag === 'INDENT' && !seenControl)) && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref4 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref4) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{'))); }, action); if (prev[0] === '?') prev[0] = 'FUNC_EXIST'; diff --git a/src/nodes.coffee b/src/nodes.coffee index e5ed9763..340c7fa0 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1699,7 +1699,8 @@ exports.If = class If extends Base body = @ensureBlock(@body) bodyc = body.compile o if body.expressions.length is 1 and !@elseBody and !child and bodyc and -1 is bodyc.indexOf '\n' - return "#{@tab}if (#{cond}) #{bodyc.replace /^\s+/, ''}" + separator = if cond.length + bodyc.length > 100 then "\n#{@tab}#{TAB}" else ' ' + return "#{@tab}if (#{cond})#{separator}#{bodyc.replace /^\s+/, ''}" bodyc = "\n#{bodyc}\n#{@tab}" if bodyc ifPart = "if (#{cond}) {#{bodyc}}" ifPart = @tab + ifPart unless child