From b18d7fb5509fca05618efeabb383744aae56e4ef Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 29 Jul 2010 21:23:49 -0400 Subject: [PATCH] removing the last traces of half-assignments. Issue #541. --- documentation/coffee/conditionals.coffee | 2 +- examples/code.coffee | 4 +- examples/computer_science/linked_list.coffee | 2 +- examples/poignant.coffee | 2 +- lib/lexer.js | 5 +-- src/coffee-script.coffee | 2 +- src/command.coffee | 2 +- src/lexer.coffee | 8 ++-- src/nodes.coffee | 40 ++++++++++---------- src/rewriter.coffee | 2 +- test/test_operations.coffee | 8 ++-- 11 files changed, 36 insertions(+), 41 deletions(-) diff --git a/documentation/coffee/conditionals.coffee b/documentation/coffee/conditionals.coffee index 5f7e4385..7aa329b0 100644 --- a/documentation/coffee/conditionals.coffee +++ b/documentation/coffee/conditionals.coffee @@ -8,4 +8,4 @@ else date = if friday then sue else jill -options = or defaults \ No newline at end of file +options or= defaults \ No newline at end of file diff --git a/examples/code.coffee b/examples/code.coffee index 4dd69576..255feba5 100644 --- a/examples/code.coffee +++ b/examples/code.coffee @@ -60,8 +60,8 @@ race = -> race() # Conditional assignment: -good = or evil -wine = and cheese +good or= evil +wine and= cheese # Nested property access and calls. ((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position['top'].offset('x') diff --git a/examples/computer_science/linked_list.coffee b/examples/computer_science/linked_list.coffee index 51032af0..95bb2efe 100644 --- a/examples/computer_science/linked_list.coffee +++ b/examples/computer_science/linked_list.coffee @@ -12,7 +12,7 @@ class LinkedList # Create a new node object to wrap the data. node = data: data, next: null - current = this._head = or node + current = this._head or= node if this._head isnt node (current = current.next) while current.next diff --git a/examples/poignant.coffee b/examples/poignant.coffee index 074a79d2..dc07ef88 100644 --- a/examples/poignant.coffee +++ b/examples/poignant.coffee @@ -46,7 +46,7 @@ LotteryDraw = ticket_list.each (ticket) -> score = ticket.score result return if score is 0 - winners[buyer] = or [] + winners[buyer] or= [] winners[buyer].push [ticket, score] this.tickets = {} winners diff --git a/lib/lexer.js b/lib/lexer.js index a3ec090d..d98df58b 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -70,7 +70,7 @@ return this.literalToken(); }; Lexer.prototype.identifierToken = function() { - var _d, close_index, forcedIdentifier, id, tag; + var close_index, forcedIdentifier, id, tag; if (!(id = this.match(IDENTIFIER, 1))) { return false; } @@ -86,9 +86,6 @@ if (id === 'all' && this.tag() === 'FOR') { tag = 'ALL'; } - if (('AND' === (_d = this.tag() === '=' && tag) || 'OR' === _d)) { - return this.tag(1, CONVERSIONS[id] + '='); - } if (include(JS_FORBIDDEN, id)) { if (forcedIdentifier) { tag = 'STRING'; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index e70e5a11..2d32c01e 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -30,7 +30,7 @@ lexer = new Lexer # Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison # compiler. exports.compile = compile = (code, options) -> - options = or {} + options or= {} try (parser.parse lexer.tokenize code).compile options catch err diff --git a/src/command.coffee b/src/command.coffee index 4cd396e7..c4e7217a 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -156,7 +156,7 @@ printTokens = (tokens) -> parseOptions = -> optionParser = new optparse.OptionParser SWITCHES, BANNER o = options = optionParser.parse(process.argv[2...process.argv.length]) - options.compile = or !!o.output + options.compile or= !!o.output options.run = not (o.compile or o.print or o.lint) options.print = !! (o.print or (o.eval or o.stdio and o.compile)) sources = options.arguments diff --git a/src/lexer.coffee b/src/lexer.coffee index 95d2efe8..f05ab396 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -88,8 +88,6 @@ exports.Lexer = class Lexer tag = id.toUpperCase() if include(JS_KEYWORDS, id) or (not forcedIdentifier and include(COFFEE_KEYWORDS, id)) tag = 'LEADING_WHEN' if tag is 'WHEN' and include LINE_BREAK, @tag() tag = 'ALL' if id is 'all' and @tag() is 'FOR' - if @tag() is '=' and tag in ['AND', 'OR'] - return @tag 1, CONVERSIONS[id] + '=' if include(JS_FORBIDDEN, id) if forcedIdentifier tag = 'STRING' @@ -259,7 +257,7 @@ exports.Lexer = class Lexer value = match and match[1] space = match and match[2] @tagParameters() if value and value.match CODE - value = or @chunk.substr 0, 1 + value or= @chunk.substr 0, 1 @i += value.length prevSpaced = @prev() and @prev().spaced tag = value @@ -347,7 +345,7 @@ exports.Lexer = class Lexer # contents of the string. This method allows us to have strings within # interpolations within strings, ad infinitum. balancedString: (str, delimited, options) -> - options = or {} + options or= {} slash = delimited[0][0] is '/' levels = [] i = 0 @@ -384,7 +382,7 @@ exports.Lexer = class Lexer # new Lexer, tokenize the interpolated contents, and merge them into the # token stream. interpolateString: (str, options) -> - options = or {} + options or= {} if str.length < 3 or not starts str, '"' @token 'STRING', str else diff --git a/src/nodes.coffee b/src/nodes.coffee index 6c9139bc..fecdbcd7 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -112,7 +112,7 @@ exports.BaseNode = class BaseNode # `toString` representation of the node, for inspecting the parse tree. # This is what `coffee --nodes` prints out. toString: (idt, override) -> - idt = or '' + idt or= '' children = (child.toString idt + TAB for child in @collectChildren()).join('') '\n' + idt + (override or @class) + children @@ -187,7 +187,7 @@ exports.Expressions = class Expressions extends BaseNode # An **Expressions** is the only node that can serve as the root. compile: (o) -> - o = or {} + o or= {} if o.scope then super(o) else @compileRoot(o) compileNode: (o) -> @@ -290,7 +290,7 @@ exports.ValueNode = class ValueNode extends BaseNode # A **ValueNode** has a base and a list of property accesses. constructor: (@base, @properties) -> - @properties = or [] + @properties or= [] # Add a property access to the list. push: (prop) -> @@ -345,7 +345,7 @@ exports.ValueNode = class ValueNode extends BaseNode only = del o, 'onlyFirst' op = del o, 'operation' props = if only then @properties[0...@properties.length - 1] else @properties - o.chainRoot = or this + o.chainRoot or= this baseline = @base.compile o baseline = "(#baseline)" if @hasProperties() and (@base instanceof ObjectNode or @isNumber()) complete = @last = baseline @@ -397,7 +397,7 @@ exports.CallNode = class CallNode extends BaseNode @isNew = false @isSuper = variable is 'super' @variable = if @isSuper then null else variable - @args = or [] + @args or= [] @compileSplatArguments = (o) -> SplatNode.compileSplattedArray.call(this, @args, o) @@ -489,7 +489,7 @@ exports.AccessorNode = class AccessorNode extends BaseNode compileNode: (o) -> name = @name.compile o - o.chainRoot.wrapped = or @soakNode + o.chainRoot.wrapped or= @soakNode namePart = if name.match(IS_STRING) then "[#name]" else ".#name" @prototype + namePart @@ -504,7 +504,7 @@ exports.IndexNode = class IndexNode extends BaseNode constructor: (@index) -> compileNode: (o) -> - o.chainRoot.wrapped = or @soakNode + o.chainRoot.wrapped or= @soakNode idx = @index.compile o prefix = if @proto then '.prototype' else '' "#prefix[#idx]" @@ -554,7 +554,7 @@ exports.RangeNode = class RangeNode extends BaseNode [from, to] = [parseInt(@fromNum, 10), parseInt(@toNum, 10)] idx = del o, 'index' step = del o, 'step' - step = and "#idx += #{step.compile(o)}" + step and= "#idx += #{step.compile(o)}" if from <= to "#idx = #from; #idx <#@equals #to; #{step or "#idx++"}" else @@ -633,7 +633,7 @@ exports.ArrayNode = class ArrayNode extends BaseNode children: ['objects'] constructor: (@objects) -> - @objects = or [] + @objects or= [] @compileSplatLiteral = (o) -> SplatNode.compileSplattedArray.call(this, @objects, o) @@ -668,7 +668,7 @@ exports.ClassNode = class ClassNode extends BaseNode # Initialize a **ClassNode** with its name, an optional superclass, and a # list of prototype property assignments. constructor: (@variable, @parent, @properties) -> - @properties = or [] + @properties or= [] @returns = false makeReturn: -> @@ -707,8 +707,8 @@ exports.ClassNode = class ClassNode extends BaseNode continue if func instanceof CodeNode and func.bound func.bound = false - constScope = or new Scope(o.scope, constructor.body, constructor) - me = or constScope.freeVariable() + constScope or= new Scope(o.scope, constructor.body, constructor) + me or= constScope.freeVariable() pname = pvar.compile(o) constructor.body.push new ReturnNode literal 'this' if constructor.body.empty() constructor.body.unshift literal "this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#me, arguments); }" @@ -838,8 +838,8 @@ exports.CodeNode = class CodeNode extends BaseNode children: ['params', 'body'] constructor: (@params, @body, tag) -> - @params = or [] - @body = or new Expressions + @params or= [] + @body or= new Expressions @bound = tag is 'boundfunc' # Compilation creates a new scope unless explicitly asked to share with the @@ -893,7 +893,7 @@ exports.CodeNode = class CodeNode extends BaseNode traverseChildren: (crossScope, func) -> super(crossScope, func) if crossScope toString: (idt) -> - idt = or '' + idt or= '' children = (child.toString(idt + TAB) for child in @collectChildren()).join('') "\n#idt#children" @@ -1251,7 +1251,7 @@ exports.ForNode = class ForNode extends BaseNode isStatement: -> yes constructor: (@body, source, @name, @index) -> - @index = or null + @index or= null @source = source.source @guard = source.guard @step = source.step @@ -1340,7 +1340,7 @@ exports.IfNode = class IfNode extends BaseNode children: ['condition', 'switchSubject', 'body', 'elseBody', 'assigner'] constructor: (@condition, @body, @tags) -> - @tags = or {} + @tags or= {} @condition = new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert @elseBody = null @isChain = false @@ -1386,7 +1386,7 @@ exports.IfNode = class IfNode extends BaseNode # The **IfNode** only compiles into a statement if either of its bodies needs # to be a statement. Otherwise a ternary is safe. isStatement: -> - @statement = or !!(@tags.statement or @bodyNode().isStatement() or (@elseBody and @elseBodyNode().isStatement())) + @statement or= !!(@tags.statement or @bodyNode().isStatement() or (@elseBody and @elseBodyNode().isStatement())) compileCondition: (o) -> (cond.compile(o) for cond in flatten([@condition])).join(' || ') @@ -1396,8 +1396,8 @@ exports.IfNode = class IfNode extends BaseNode makeReturn: -> if @isStatement() - @body = and @ensureExpressions(@body.makeReturn()) - @elseBody = and @ensureExpressions(@elseBody.makeReturn()) + @body and= @ensureExpressions(@body.makeReturn()) + @elseBody and= @ensureExpressions(@elseBody.makeReturn()) this else new ReturnNode this diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 9104c211..5f4378b5 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -239,7 +239,7 @@ exports.Rewriter = class Rewriter @scanTokens (prev, token, post, i) => for pair in pairs [open, close] = pair - levels[open] = or 0 + levels[open] or= 0 if token[0] is open openLine[open] = token[2] if levels[open] == 0 levels[open] += 1 diff --git a/test/test_operations.coffee b/test/test_operations.coffee index d857347e..4dab3ca0 100644 --- a/test/test_operations.coffee +++ b/test/test_operations.coffee @@ -68,14 +68,14 @@ ok x*+y is -50 # Half-operators. one = two = null -one = or 1 -two = or 2 +one or= 1 +two or= 2 ok one is 1 ok two is 2 -one = and 'one' -two = and 'two' +one and= 'one' +two and= 'two' ok one is 'one' ok two is 'two'