using Underscore's compact in the code generation, insstead of our home-rolled one.

This commit is contained in:
Jeremy Ashkenas
2010-02-13 02:30:47 -05:00
parent 1ab3b183a8
commit 2c4c4cc93e
2 changed files with 11 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
(function(){ (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; var __hasProp = Object.prototype.hasOwnProperty;
process.mixin(require('./scope')); process.mixin(require('./scope'));
_ = require('./underscore')._; _ = require('./underscore')._;
@@ -24,18 +24,6 @@
} }
return memo; 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 an array or object.
dup = function dup(input) { dup = function dup(input) {
var __a, __b, __c, __d, key, output, val; var __a, __b, __c, __d, key, output, val;
@@ -199,7 +187,7 @@
Expressions = (exports.Expressions = inherit(Node, { Expressions = (exports.Expressions = inherit(Node, {
type: 'Expressions', type: 'Expressions',
constructor: function constructor(nodes) { constructor: function constructor(nodes) {
this.children = (this.expressions = compact(flatten(nodes || []))); this.children = (this.expressions = _.compact(flatten(nodes || [])));
return this; return this;
}, },
// Tack an expression on to the end of this expression list. // Tack an expression on to the end of this expression list.
@@ -944,7 +932,7 @@
ASSIGNMENT: ['||=', '&&=', '?='], ASSIGNMENT: ['||=', '&&=', '?='],
PREFIX_OPERATORS: ['typeof', 'delete'], PREFIX_OPERATORS: ['typeof', 'delete'],
constructor: function constructor(operator, first, second, flip) { 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.operator = this.CONVERSIONS[operator] || operator;
this.flip = !!flip; this.flip = !!flip;
return this; return this;
@@ -1016,7 +1004,7 @@
TryNode = (exports.TryNode = inherit(Node, { TryNode = (exports.TryNode = inherit(Node, {
type: 'Try', type: 'Try',
constructor: function constructor(attempt, error, recovery, ensure) { 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; this.error = error;
return this; return this;
}, },
@@ -1105,7 +1093,7 @@
this.name = __a[0]; this.name = __a[0];
this.index = __a[1]; this.index = __a[1];
} }
this.children = compact([this.body, this.source, this.filter]); this.children = _.compact([this.body, this.source, this.filter]);
return this; return this;
}, },
top_sensitive: function top_sensitive() { top_sensitive: function top_sensitive() {
@@ -1196,7 +1184,7 @@
this.condition = condition; this.condition = condition;
this.body = body && body.unwrap(); this.body = body && body.unwrap();
this.else_body = else_body && else_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 || {}; this.tags = tags || {};
if (this.condition instanceof Array) { if (this.condition instanceof Array) {
this.multiple = true; this.multiple = true;

View File

@@ -19,10 +19,6 @@ flatten: (list) ->
memo memo
memo memo
# Remove all null values from an array.
compact: (input) ->
item for item in input when item?
# Dup an array or object. # Dup an array or object.
dup: (input) -> dup: (input) ->
if input instanceof Array if input instanceof Array
@@ -116,7 +112,7 @@ Expressions: exports.Expressions: inherit Node, {
type: 'Expressions' type: 'Expressions'
constructor: (nodes) -> constructor: (nodes) ->
@children: @expressions: compact flatten nodes or [] @children: @expressions: _.compact flatten nodes or []
this this
# Tack an expression on to the end of this expression list. # Tack an expression on to the end of this expression list.
@@ -749,7 +745,7 @@ OpNode: exports.OpNode: inherit Node, {
PREFIX_OPERATORS: ['typeof', 'delete'] PREFIX_OPERATORS: ['typeof', 'delete']
constructor: (operator, first, second, flip) -> constructor: (operator, first, second, flip) ->
@children: compact [@first: first, @second: second] @children: _.compact [@first: first, @second: second]
@operator: @CONVERSIONS[operator] or operator @operator: @CONVERSIONS[operator] or operator
@flip: !!flip @flip: !!flip
this this
@@ -797,7 +793,7 @@ TryNode: exports.TryNode: inherit Node, {
type: 'Try' type: 'Try'
constructor: (attempt, error, recovery, ensure) -> constructor: (attempt, error, recovery, ensure) ->
@children: compact [@attempt: attempt, @recovery: recovery, @ensure: ensure] @children: _.compact [@attempt: attempt, @recovery: recovery, @ensure: ensure]
@error: error @error: error
this this
@@ -878,7 +874,7 @@ ForNode: exports.ForNode: inherit Node, {
@step: source.step @step: source.step
@object: !!source.object @object: !!source.object
[@name, @index]: [@index, @name] if @object [@name, @index]: [@index, @name] if @object
@children: compact [@body, @source, @filter] @children: _.compact [@body, @source, @filter]
this this
top_sensitive: -> top_sensitive: ->
@@ -948,7 +944,7 @@ IfNode: exports.IfNode: inherit Node, {
@condition: condition @condition: condition
@body: body and body.unwrap() @body: body and body.unwrap()
@else_body: else_body and else_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 {} @tags: tags or {}
@multiple: true if @condition instanceof Array @multiple: true if @condition instanceof Array
@condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert @condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert