From 2c4c4cc93ea1550df1ce3e749bf40f2049cf4242 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 13 Feb 2010 02:30:47 -0500 Subject: [PATCH] using Underscore's compact in the code generation, insstead of our home-rolled one. --- lib/coffee_script/nodes.js | 24 ++++++------------------ src/nodes.coffee | 14 +++++--------- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 2dec5b4c..c6057d3d 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -1,5 +1,5 @@ (function(){ - var AccessorNode, ArrayNode, AssignNode, CallNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, Node, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThisNode, ThrowNode, TryNode, ValueNode, WhileNode, _, compact, del, dup, flatten, inherit, merge, statement; + var AccessorNode, ArrayNode, AssignNode, CallNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, Node, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThisNode, ThrowNode, TryNode, ValueNode, WhileNode, _, del, dup, flatten, inherit, merge, statement; var __hasProp = Object.prototype.hasOwnProperty; process.mixin(require('./scope')); _ = require('./underscore')._; @@ -24,18 +24,6 @@ } return memo; }; - // Remove all null values from an array. - compact = function compact(input) { - var __a, __b, __c, item; - __a = []; __b = input; - for (__c = 0; __c < __b.length; __c++) { - item = __b[__c]; - if ((typeof item !== "undefined" && item !== null)) { - __a.push(item); - } - } - return __a; - }; // Dup an array or object. dup = function dup(input) { var __a, __b, __c, __d, key, output, val; @@ -199,7 +187,7 @@ Expressions = (exports.Expressions = inherit(Node, { type: 'Expressions', constructor: function constructor(nodes) { - this.children = (this.expressions = compact(flatten(nodes || []))); + this.children = (this.expressions = _.compact(flatten(nodes || []))); return this; }, // Tack an expression on to the end of this expression list. @@ -944,7 +932,7 @@ ASSIGNMENT: ['||=', '&&=', '?='], PREFIX_OPERATORS: ['typeof', 'delete'], constructor: function constructor(operator, first, second, flip) { - this.children = compact([(this.first = first), (this.second = second)]); + this.children = _.compact([(this.first = first), (this.second = second)]); this.operator = this.CONVERSIONS[operator] || operator; this.flip = !!flip; return this; @@ -1016,7 +1004,7 @@ TryNode = (exports.TryNode = inherit(Node, { type: 'Try', constructor: function constructor(attempt, error, recovery, ensure) { - this.children = compact([(this.attempt = attempt), (this.recovery = recovery), (this.ensure = ensure)]); + this.children = _.compact([(this.attempt = attempt), (this.recovery = recovery), (this.ensure = ensure)]); this.error = error; return this; }, @@ -1105,7 +1093,7 @@ this.name = __a[0]; this.index = __a[1]; } - this.children = compact([this.body, this.source, this.filter]); + this.children = _.compact([this.body, this.source, this.filter]); return this; }, top_sensitive: function top_sensitive() { @@ -1196,7 +1184,7 @@ this.condition = condition; this.body = body && body.unwrap(); this.else_body = else_body && else_body.unwrap(); - this.children = compact([this.condition, this.body, this.else_body]); + this.children = _.compact([this.condition, this.body, this.else_body]); this.tags = tags || {}; if (this.condition instanceof Array) { this.multiple = true; diff --git a/src/nodes.coffee b/src/nodes.coffee index d4631f9b..4497acce 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -19,10 +19,6 @@ flatten: (list) -> memo memo -# Remove all null values from an array. -compact: (input) -> - item for item in input when item? - # Dup an array or object. dup: (input) -> if input instanceof Array @@ -116,7 +112,7 @@ Expressions: exports.Expressions: inherit Node, { type: 'Expressions' constructor: (nodes) -> - @children: @expressions: compact flatten nodes or [] + @children: @expressions: _.compact flatten nodes or [] this # Tack an expression on to the end of this expression list. @@ -749,7 +745,7 @@ OpNode: exports.OpNode: inherit Node, { PREFIX_OPERATORS: ['typeof', 'delete'] constructor: (operator, first, second, flip) -> - @children: compact [@first: first, @second: second] + @children: _.compact [@first: first, @second: second] @operator: @CONVERSIONS[operator] or operator @flip: !!flip this @@ -797,7 +793,7 @@ TryNode: exports.TryNode: inherit Node, { type: 'Try' constructor: (attempt, error, recovery, ensure) -> - @children: compact [@attempt: attempt, @recovery: recovery, @ensure: ensure] + @children: _.compact [@attempt: attempt, @recovery: recovery, @ensure: ensure] @error: error this @@ -878,7 +874,7 @@ ForNode: exports.ForNode: inherit Node, { @step: source.step @object: !!source.object [@name, @index]: [@index, @name] if @object - @children: compact [@body, @source, @filter] + @children: _.compact [@body, @source, @filter] this top_sensitive: -> @@ -948,7 +944,7 @@ IfNode: exports.IfNode: inherit Node, { @condition: condition @body: body and body.unwrap() @else_body: else_body and else_body.unwrap() - @children: compact [@condition, @body, @else_body] + @children: _.compact [@condition, @body, @else_body] @tags: tags or {} @multiple: true if @condition instanceof Array @condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert