From 9447796d8e8d9ac78e2fc78fcb91c7b6f643a46d Mon Sep 17 00:00:00 2001 From: satyr Date: Thu, 7 Oct 2010 20:05:22 +0900 Subject: [PATCH] made `unless` less parenfull --- lib/cake.js | 8 +++---- lib/command.js | 4 ++-- lib/grammar.js | 4 ++-- lib/lexer.js | 10 ++++----- lib/nodes.js | 57 ++++++++++++++++++++++++++---------------------- lib/rewriter.js | 4 ++-- src/nodes.coffee | 18 ++++++++------- 7 files changed, 56 insertions(+), 49 deletions(-) diff --git a/lib/cake.js b/lib/cake.js index c378d48a..7d91f000 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -12,7 +12,7 @@ helpers.extend(global, { task: function(name, description, action) { var _ref; - if (!(action)) { + if (!action) { _ref = [description, action], action = _ref[0], description = _ref[1]; } return (tasks[name] = { @@ -25,7 +25,7 @@ return switches.push([letter, flag, description]); }, invoke: function(name) { - if (!(tasks[name])) { + if (!tasks[name]) { missingTask(name); } return tasks[name].action(options); @@ -34,7 +34,7 @@ exports.run = function() { return path.exists('Cakefile', function(exists) { var _i, _len, _ref, _result, arg, args; - if (!(exists)) { + if (!exists) { throw new Error("Cakefile not found in " + (process.cwd())); } args = process.argv.slice(2, process.argv.length); @@ -42,7 +42,7 @@ fileName: 'Cakefile' }); oparse = new optparse.OptionParser(switches); - if (!(args.length)) { + if (!args.length) { return printTasks(); } options = oparse.parse(args); diff --git a/lib/command.js b/lib/command.js index 212d1a68..04b4c6d4 100644 --- a/lib/command.js +++ b/lib/command.js @@ -32,7 +32,7 @@ if (opts.eval) { return compileScript('console', sources[0]); } - if (!(sources.length)) { + if (!sources.length) { return require('./repl'); } separator = sources.indexOf('--'); @@ -59,7 +59,7 @@ base = source; compile = function(source, topLevel) { return path.exists(source, function(exists) { - if (!(exists)) { + if (!exists) { throw new Error("File not found: " + source); } return fs.stat(source, function(err, stats) { diff --git a/lib/grammar.js b/lib/grammar.js index 73701e4d..08f74b05 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -5,7 +5,7 @@ unwrap = /function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/; o = function(patternString, action, options) { var match; - if (!(action)) { + if (!action) { return [patternString, '$$ = $1;', options]; } action = (match = (action + '').match(unwrap)) ? match[1] : ("(" + action + "())"); @@ -614,7 +614,7 @@ _result.push((function() { for (_j = 0, _len2 = (_ref = alt[0].split(' ')).length; _j < _len2; _j++) { token = _ref[_j]; - if (!(grammar[token])) { + if (!grammar[token]) { tokens.push(token); } } diff --git a/lib/lexer.js b/lib/lexer.js index 91f53276..bfed52aa 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -71,7 +71,7 @@ this.identifierError(id); } } - if (!(forcedIdentifier)) { + if (!forcedIdentifier) { if (COFFEE_ALIASES.hasOwnProperty(id)) { tag = (id = COFFEE_ALIASES[id]); } @@ -398,7 +398,7 @@ if (herecomment && 0 > doc.indexOf('\n')) { return doc; } - if (!(herecomment)) { + if (!herecomment) { while (match = HEREDOC_INDENT.exec(doc)) { attempt = match[1]; if (indent === null || (0 < (_ref3 = attempt.length)) && (_ref3 < indent.length)) { @@ -409,7 +409,7 @@ if (indent) { doc = doc.replace(RegExp("\\n" + indent, "g"), '\n'); } - if (!(herecomment)) { + if (!herecomment) { doc = doc.replace(/^\n/, ''); } return doc; @@ -463,7 +463,7 @@ if (levels.length && starts(str, close, i) && last(levels) === pair) { levels.pop(); i += close.length - 1; - if (!(levels.length)) { + if (!levels.length) { i += 1; } break; @@ -524,7 +524,7 @@ if (regex) { return tokens; } - if (!(tokens.length)) { + if (!tokens.length) { return this.token('STRING', '""'); } if (interpolated = tokens.length > 1) { diff --git a/lib/nodes.js b/lib/nodes.js index 1bf7e9c2..618cea0c 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -43,7 +43,7 @@ Base.prototype.compileReference = function(o, options) { var _len, compiled, i, node, pair, reference; pair = (function() { - if (!(this.isComplex())) { + if (!this.isComplex()) { return [this, this]; } else { reference = new Literal(o.scope.freeVariable('ref')); @@ -111,7 +111,7 @@ }; Base.prototype.eachChild = function(func) { var _i, _j, _len, _len2, _ref2, _ref3, _result, attr, child; - if (!(this.children)) { + if (!this.children) { return; } _result = []; @@ -362,7 +362,7 @@ bref = new Literal(o.scope.freeVariable('base')); base = new Value(new Parens(new Assign(bref, base))); } - if (!(name)) { + if (!name) { return [base, bref]; } if (name.isComplex()) { @@ -428,7 +428,7 @@ } else if (node instanceof Value) { ifnode = node.unfoldSoak(o); } - if (!(ifnode)) { + if (!ifnode) { return; } parent[name] = ifnode.body; @@ -481,11 +481,11 @@ Call.prototype.superReference = function(o) { var method, name; method = o.scope.method; - if (!(method)) { + if (!method) { throw Error("cannot call super outside of a function"); } name = method.name; - if (!(name)) { + if (!name) { throw Error("cannot call super on an anonymous function."); } return method.klass ? ("" + (method.klass) + ".__super__." + name) : ("" + name + ".__super__.constructor"); @@ -567,7 +567,7 @@ if (this.isSuper) { return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")"); } - if (!(this.isNew)) { + if (!this.isNew) { if (!((base = this.variable) instanceof Value)) { base = new Value(base); } @@ -694,7 +694,7 @@ Range.prototype.compileNode = function(o) { var compare, idx, incr, intro, step, stepPart, vars; this.compileVariables(o); - if (!(o.index)) { + if (!o.index) { return this.compileArray(o); } if (this.fromNum && this.toNum) { @@ -1059,7 +1059,7 @@ } assigns.push(new Assign(obj, val).compile(otop)); } - if (!(top)) { + if (!top) { assigns.push(valVar); } code = assigns.join(', '); @@ -1141,7 +1141,7 @@ } return _result; })(); - if (!(empty)) { + if (!empty) { this.body.makeReturn(); } for (_i = 0, _len2 = params.length; _i < _len2; _i++) { @@ -1200,7 +1200,7 @@ Splat = (function() { return function Splat(name) { Splat.__super__.constructor.call(this); - if (!(name.compile)) { + if (!name.compile) { name = new Literal(name); } this.name = name; @@ -1301,7 +1301,7 @@ this.condition.parenthetical = true; cond = this.condition.compile(o); set = ''; - if (!(top)) { + if (!top) { rvar = o.scope.freeVariable('result'); set = ("" + (this.tab) + rvar + " = [];\n"); if (this.body) { @@ -1363,7 +1363,7 @@ }; Op.prototype.isInvertible = function() { var _ref2; - return (('===' === (_ref2 = this.operator) || '!==' === _ref2)) && !(this.first instanceof Op) && !(this.second instanceof Op); + return ('===' === (_ref2 = this.operator) || '!==' === _ref2); }; Op.prototype.isComplex = function() { return this.operator !== '!' || this.first.isComplex(); @@ -1650,7 +1650,7 @@ immediate: true }); } - if (!(topLevel)) { + if (!topLevel) { rvar = scope.freeVariable('result'); } ivar = range ? name : index; @@ -1672,7 +1672,7 @@ immediate: true }))) { sourcePart = ("" + (ref = scope.freeVariable('ref')) + " = " + svar); - if (!(this.object)) { + if (!this.object) { sourcePart = ("(" + sourcePart + ")"); } svar = ref; @@ -1680,7 +1680,7 @@ namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, { top: true })) : (name ? ("" + name + " = " + svar + "[" + ivar + "]") : undefined); - if (!(this.object)) { + if (!this.object) { lvar = scope.freeVariable('len'); stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++"); forPart = ("" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart); @@ -1688,7 +1688,7 @@ } resultPart = rvar ? ("" + (this.tab) + rvar + " = [];\n") : ''; returnResult = this.compileReturnValue(rvar, o); - if (!(topLevel)) { + if (!topLevel) { body = Push.wrap(rvar, body); } if (this.guard) { @@ -1712,7 +1712,7 @@ } if (this.object) { forPart = ("" + ivar + " in " + sourcePart); - if (!(this.raw)) { + if (!this.raw) { guardPart = ("\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;"); } } @@ -1782,18 +1782,23 @@ })(); exports.If = (function() { If = (function() { - return function If(_arg, _arg2, _arg3) { - this.tags = _arg3; - this.body = _arg2; - this.condition = _arg; + return function If(condition, _arg, _arg2) { + var op; + this.tags = _arg2; + this.body = _arg; this.tags || (this.tags = {}); if (this.tags.invert) { - if (this.condition instanceof Op && this.condition.isInvertible()) { - this.condition.invert(); + op = condition instanceof Op; + if (op && condition.isInvertible()) { + condition.invert(); } else { - this.condition = new Op('!', new Parens(this.condition)); + if (op && !condition.isUnary()) { + condition = new Parens(condition); + } + condition = new Op('!', condition); } } + this.condition = condition; this.elseBody = null; this.isChain = false; return this; @@ -1863,7 +1868,7 @@ comDent = child ? this.idt() : ''; body = this.body.compile(o); ifPart = ("" + ifDent + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + (this.tab) + "}"); - if (!(this.elseBody)) { + if (!this.elseBody) { return ifPart; } elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, { diff --git a/lib/rewriter.js b/lib/rewriter.js index 9a9152da..7cde5fd5 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -265,7 +265,7 @@ }; return this.scanTokens(function(token, i) { var _ref, original; - if (!(('IF' === (_ref = token[0]) || 'UNLESS' === _ref))) { + if (!('IF' === (_ref = token[0]) || 'UNLESS' === _ref)) { return 1; } original = token; @@ -326,7 +326,7 @@ stack.push(token); return 1; } - if (!(include(EXPRESSION_END, tag))) { + if (!include(EXPRESSION_END, tag)) { return 1; } if (debt[(inv = INVERSES[tag])] > 0) { diff --git a/src/nodes.coffee b/src/nodes.coffee index b42a608d..8315c7c8 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1184,8 +1184,7 @@ exports.Op = class Op extends Base not @second isInvertible: -> - (@operator in ['===', '!==']) and - not (@first instanceof Op) and not (@second instanceof Op) + @operator in ['===', '!=='] isComplex: -> @operator isnt '!' or @first.isComplex() @@ -1516,15 +1515,18 @@ exports.If = class If extends Base topSensitive: YES - constructor: (@condition, @body, @tags) -> + constructor: (condition, @body, @tags) -> @tags or= {} if @tags.invert - if @condition instanceof Op and @condition.isInvertible() - @condition.invert() + op = condition instanceof Op + if op and condition.isInvertible() + condition.invert() else - @condition = new Op '!', new Parens @condition - @elseBody = null - @isChain = false + condition = new Parens condition if op and not condition.isUnary() + condition = new Op '!', condition + @condition = condition + @elseBody = null + @isChain = false bodyNode: -> @body?.unwrap() elseBodyNode: -> @elseBody?.unwrap()