From 08ab4abd43ac8cba1a5bd4c5505863657d83ca3a Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 27 Jun 2010 23:55:18 -0400 Subject: [PATCH] removing obvious redundant parentheses. --- lib/cake.js | 4 ++-- lib/coffee-script.js | 4 ++-- lib/command.js | 6 +++--- lib/helpers.js | 4 ++-- lib/lexer.js | 8 ++++---- lib/nodes.js | 18 ++++++++++-------- lib/optparse.js | 6 +++--- lib/rewriter.js | 4 ++-- lib/scope.js | 4 ++-- src/coffee-script.coffee | 2 +- src/nodes.coffee | 6 ++++++ 11 files changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/cake.js b/lib/cake.js index f3e09b8b..0fcfdd81 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -73,14 +73,14 @@ return _b; })().join('') : ''; desc = task.description ? ("# " + task.description) : ''; - puts(("cake " + name + spaces + " " + desc)); + puts("cake " + name + spaces + " " + desc); }} if (switches.length) { return puts(oparse.help()); } }; missingTask = function(task) { - puts(("No such task: \"" + task + "\"")); + puts("No such task: \"" + task + "\""); return process.exit(1); }; })(); diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 696c5b33..b11bc666 100644 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -1,6 +1,6 @@ (function(){ var Lexer, compile, helpers, lexer, parser, path, processScripts; - if ((typeof process !== "undefined" && process !== null)) { + if (typeof process !== "undefined" && process !== null) { path = require('path'); Lexer = require('./lexer').Lexer; parser = require('./parser').parser; @@ -15,7 +15,7 @@ parser = this.parser; helpers = this.helpers; } - exports.VERSION = '0.6.2'; + exports.VERSION = '0.7.0'; lexer = new Lexer(); exports.compile = (compile = function(code, options) { options = options || {}; diff --git a/lib/command.js b/lib/command.js index 287172ac..832d57cd 100644 --- a/lib/command.js +++ b/lib/command.js @@ -54,7 +54,7 @@ compile = function(source, topLevel) { return path.exists(source, function(exists) { if (!(exists)) { - throw new Error(("File not found: " + source)); + throw new Error("File not found: " + source); } return fs.stat(source, function(err, stats) { if (stats.isDirectory()) { @@ -148,7 +148,7 @@ compile = function() { return fs.writeFile(jsPath, js, function(err) { if (options.compile && options.watch) { - return puts(("Compiled " + source)); + return puts("Compiled " + source); } }); }; @@ -210,7 +210,7 @@ return process.exit(0); }; version = function() { - puts(("CoffeeScript version " + CoffeeScript.VERSION)); + puts("CoffeeScript version " + CoffeeScript.VERSION); return process.exit(0); }; })(); diff --git a/lib/helpers.js b/lib/helpers.js index 38b3148b..a5c6b12b 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,7 +1,7 @@ (function(){ var balancedString, compact, count, del, extend, flatten, helpers, include, indexOf, merge, starts; var __hasProp = Object.prototype.hasOwnProperty; - if (!((typeof process !== "undefined" && process !== null))) { + if (!(typeof process !== "undefined" && process !== null)) { this.exports = this; } helpers = (exports.helpers = {}); @@ -66,7 +66,7 @@ _a = []; _b = properties; for (key in _b) { if (__hasProp.call(_b, key)) { val = _b[key]; - _a.push((object[key] = val)); + _a.push(object[key] = val); }} return _a; }); diff --git a/lib/lexer.js b/lib/lexer.js index 2bdc0e9f..8aa0bee7 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -1,7 +1,7 @@ (function(){ var ASSIGNED, ASSIGNMENT, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, CONVERSIONS, HALF_ASSIGNMENTS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, INTERPOLATION, JS_CLEANER, JS_FORBIDDEN, JS_KEYWORDS, LAST_DENT, LAST_DENTS, LINE_BREAK, Lexer, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_END, REGEX_ESCAPE, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, STRING_NEWLINES, WHITESPACE, _a, _b, _c, balancedString, compact, count, helpers, include, starts; var __slice = Array.prototype.slice; - if ((typeof process !== "undefined" && process !== null)) { + if (typeof process !== "undefined" && process !== null) { _a = require('./rewriter'); Rewriter = _a.Rewriter; _b = require('./helpers'); @@ -147,7 +147,7 @@ doc = this.sanitizeHeredoc(match[2] || match[4], { quote: quote }); - this.interpolateString(("" + quote + doc + quote)); + this.interpolateString("" + quote + doc + quote); this.line += count(match[1], "\n"); this.i += match[1].length; return true; @@ -486,7 +486,7 @@ if (!(tok = this.prev(index))) { return null; } - if ((typeof newTag !== "undefined" && newTag !== null)) { + if (typeof newTag !== "undefined" && newTag !== null) { tok[0] = newTag; return tok[0]; } @@ -497,7 +497,7 @@ if (!(tok = this.prev(index))) { return null; } - if ((typeof val !== "undefined" && val !== null)) { + if (typeof val !== "undefined" && val !== null) { tok[1] = val; return tok[1]; } diff --git a/lib/nodes.js b/lib/nodes.js index aa01afe1..65cc16fd 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1,5 +1,5 @@ (function(){ - var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NUMBER, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, include, indexOf, literal, merge, starts, utility; + var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, DOUBLE_PARENS, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NUMBER, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, include, indexOf, literal, merge, starts, utility; var __extends = function(child, parent) { var ctor = function(){ }; ctor.prototype = parent.prototype; @@ -7,7 +7,7 @@ child.prototype = new ctor(); child.prototype.constructor = child; }; - if ((typeof process !== "undefined" && process !== null)) { + if (typeof process !== "undefined" && process !== null) { Scope = require('./scope').Scope; helpers = require('./helpers').helpers; } else { @@ -228,6 +228,7 @@ o.scope = new Scope(null, this, null); code = o.globals ? this.compileNode(o) : this.compileWithDeclarations(o); code = code.replace(TRAILING_WHITESPACE, ''); + code = code.replace(DOUBLE_PARENS, '($1)'); if (o.noWrap) { return code; } else { @@ -685,7 +686,7 @@ } else if (i === this.objects.length - 1) { objects.push(code); } else { - objects.push(("" + code + ", ")); + objects.push("" + code + ", "); } } objects = objects.join(''); @@ -761,7 +762,7 @@ props.push(prop); } if (me) { - constructor.body.unshift(literal(("" + me + " = this"))); + constructor.body.unshift(literal("" + me + " = this")); } construct = this.idt() + (new AssignNode(this.variable, constructor)).compile(merge(o, { sharedScope: constScope @@ -922,7 +923,7 @@ splat.trailings = []; splat.arglength = this.params.length; this.body.unshift(splat); - } else if ((typeof splat !== "undefined" && splat !== null)) { + } else if (typeof splat !== "undefined" && splat !== null) { splat.trailings.push(param); } else { params.push(param); @@ -1427,7 +1428,7 @@ svar = scope.freeVariable(); sourcePart = ("" + svar + " = " + (this.source.compile(o)) + ";"); if (this.pattern) { - namePart = new AssignNode(this.name, literal(("" + svar + "[" + ivar + "]"))).compile(merge(o, { + namePart = new AssignNode(this.name, literal("" + svar + "[" + ivar + "]")).compile(merge(o, { indent: this.idt(1), top: true })) + "\n"; @@ -1451,10 +1452,10 @@ this.guard ? (body = Expressions.wrap([new IfNode(this.guard, body)])) : null; if (codeInBody) { if (namePart) { - body.unshift(literal(("var " + namePart))); + body.unshift(literal("var " + namePart)); } if (index) { - body.unshift(literal(("var " + index + " = " + ivar))); + body.unshift(literal("var " + index + " = " + ivar)); } body = ClosureNode.wrap(body, true); } else { @@ -1648,6 +1649,7 @@ }; TAB = ' '; TRAILING_WHITESPACE = /[ \t]+$/gm; + DOUBLE_PARENS = /\(\(([^\(\)\n]*)\)\)/g; IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/; NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i; IS_STRING = /^['"]/; diff --git a/lib/optparse.js b/lib/optparse.js index 94c307c3..e6461a6f 100755 --- a/lib/optparse.js +++ b/lib/optparse.js @@ -25,7 +25,7 @@ } } if (isOption && !matchedRule) { - throw new Error(("unrecognized option: " + arg)); + throw new Error("unrecognized option: " + arg); } if (!(isOption)) { options.arguments.push(arg); @@ -37,7 +37,7 @@ var _a, _b, _c, _d, i, letPart, lines, rule, spaces; lines = ['Available options:']; if (this.banner) { - lines.unshift(("" + this.banner + "\n")); + lines.unshift("" + this.banner + "\n"); } _b = this.rules; for (_a = 0, _c = _b.length; _a < _c; _a++) { @@ -51,7 +51,7 @@ return _d; })().join('') : ''; letPart = rule.shortFlag ? rule.shortFlag + ', ' : ' '; - lines.push((" " + letPart + rule.longFlag + spaces + rule.description)); + lines.push(" " + letPart + rule.longFlag + spaces + rule.description); } return "\n" + (lines.join('\n')) + "\n"; }; diff --git a/lib/rewriter.js b/lib/rewriter.js index a1ecde3b..a918898c 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -1,7 +1,7 @@ (function(){ var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair; var __hasProp = Object.prototype.hasOwnProperty; - if ((typeof process !== "undefined" && process !== null)) { + if (typeof process !== "undefined" && process !== null) { _a = require('./helpers'); helpers = _a.helpers; } else { @@ -256,7 +256,7 @@ if (unclosed.length) { open = unclosed[0]; line = openLine[open] + 1; - throw new Error(("unclosed " + open + " on line " + line)); + throw new Error("unclosed " + open + " on line " + line); } }; Rewriter.prototype.rewriteClosingParens = function() { diff --git a/lib/scope.js b/lib/scope.js index 4767b78b..f1e6dd06 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -1,7 +1,7 @@ (function(){ var Scope; var __hasProp = Object.prototype.hasOwnProperty; - if (!((typeof process !== "undefined" && process !== null))) { + if (!(typeof process !== "undefined" && process !== null)) { this.exports = this; } exports.Scope = (function() { @@ -91,7 +91,7 @@ _a = []; _b = this.variables; for (key in _b) { if (__hasProp.call(_b, key)) { val = _b[key]; - val.assigned ? _a.push(("" + key + " = " + val.value)) : null; + val.assigned ? _a.push("" + key + " = " + val.value) : null; }} return _a; }; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index bdb389aa..576acc7a 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -22,7 +22,7 @@ else helpers: this.helpers # The current CoffeeScript version number. -exports.VERSION: '0.6.2' +exports.VERSION: '0.7.0' # Instantiate a Lexer for our use here. lexer: new Lexer() diff --git a/src/nodes.coffee b/src/nodes.coffee index 90ee99fb..a6a8f1ff 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -193,11 +193,14 @@ exports.Expressions: class Expressions extends BaseNode # If we happen to be the top-level **Expressions**, wrap everything in # a safety closure, unless requested not to. + # It would be better not to generate them in the first place, but for now, + # clean up obvious double-parentheses. compileRoot: (o) -> o.indent: @tab: if o.noWrap then '' else TAB o.scope: new Scope(null, this, null) code: if o.globals then @compileNode(o) else @compileWithDeclarations(o) code: code.replace(TRAILING_WHITESPACE, '') + code: code.replace(DOUBLE_PARENS, '($1)') if o.noWrap then code else "(function(){\n$code\n})();\n" # Compile the expressions body for the contents of a function, with @@ -1422,6 +1425,9 @@ TAB: ' ' # with Git. TRAILING_WHITESPACE: /[ \t]+$/gm +# Obvious redundant parentheses should be removed. +DOUBLE_PARENS: /\(\(([^\(\)\n]*)\)\)/g + # Keep these identifier regexes in sync with the Lexer. IDENTIFIER: /^[a-zA-Z\$_](\w|\$)*$/ NUMBER : /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i