removing underscore as a dependency for nodes.coffee -- let's be minimal

This commit is contained in:
Jeremy Ashkenas
2010-02-16 18:38:03 -05:00
parent 0f2cf552e9
commit 495ca64c46
4 changed files with 102 additions and 56 deletions

View File

@@ -1,13 +1,7 @@
(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, _, del, 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, compact, del, flatten, inherit, merge, statement;
var __hasProp = Object.prototype.hasOwnProperty;
if ((typeof process !== "undefined" && process !== null)) {
process.mixin(require('./scope'));
_ = require('./underscore')._;
} else {
this.exports = this;
_ = this._;
}
(typeof process !== "undefined" && process !== null) ? process.mixin(require('./scope')) : (this.exports = this);
// Some helper functions
// Tabs are two spaces for pretty printing.
TAB = ' ';
@@ -16,10 +10,44 @@
IDENTIFIER = /^[a-zA-Z$_](\w|\$)*$/;
// Merge objects.
merge = function merge(options, overrides) {
return _.tap({}, function(fresh) {
_.extend(fresh, options);
return _.extend(fresh, overrides);
});
var _a, _b, fresh, key, val;
fresh = {};
_a = options;
for (key in _a) if (__hasProp.call(_a, key)) {
val = _a[key];
((fresh[key] = val));
}
if (overrides) {
_b = overrides;
for (key in _b) if (__hasProp.call(_b, key)) {
val = _b[key];
((fresh[key] = val));
}
}
return fresh;
};
// Trim out all falsy values from an array.
compact = function compact(array) {
var _a, _b, _c, item;
_a = []; _b = array;
for (_c = 0; _c < _b.length; _c++) {
item = _b[_c];
if (item) {
_a.push(item);
}
}
return _a;
};
// Return a completely flattened version of an array.
flatten = function flatten(array) {
var _a, _b, item, memo;
memo = [];
_a = array;
for (_b = 0; _b < _a.length; _b++) {
item = _a[_b];
item instanceof Array ? (memo = memo.concat(item)) : memo.push(item);
}
return memo;
};
// Delete a key from an object, returning the value.
del = function del(obj, key) {
@@ -69,7 +97,7 @@
// already been asked to return the result.
Node.prototype.compile = function compile(o) {
var closure, top;
this.options = _.clone(o || {});
this.options = merge(o || {});
this.indent = o.indent;
top = this.top_sensitive() ? this.options.top : del(this.options, 'top');
closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) {
@@ -111,10 +139,16 @@
};
// toString representation of the node, for inspecting the parse tree.
Node.prototype.toString = function toString(idt) {
var _a, _b, _c, child;
idt = idt || '';
return '\n' + idt + this.type + _.map(this.children, function(child) {
return child.toString(idt + TAB);
}).join('');
return '\n' + idt + this.type + ((function() {
_a = []; _b = this.children;
for (_c = 0; _c < _b.length; _c++) {
child = _b[_c];
_a.push(child.toString(idt + TAB));
}
return _a;
}).call(this)).join('');
};
// Default implementations of the common node methods.
Node.prototype.unwrap = function unwrap() {
@@ -134,7 +168,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.
@@ -173,7 +207,7 @@
_a = []; _b = this.expressions;
for (_c = 0; _c < _b.length; _c++) {
node = _b[_c];
_a.push(this.compile_expression(node, _.clone(o)));
_a.push(this.compile_expression(node, merge(o)));
}
return _a;
}).call(this)).join("\n");
@@ -284,7 +318,7 @@
type: 'Value',
SOAK: " == undefined ? undefined : ",
constructor: function constructor(base, properties) {
this.children = _.flatten([(this.base = base), (this.properties = (properties || []))]);
this.children = flatten([(this.base = base), (this.properties = (properties || []))]);
return this;
},
push: function push(prop) {
@@ -368,7 +402,7 @@
CallNode = (exports.CallNode = inherit(Node, {
type: 'Call',
constructor: function constructor(variable, args) {
this.children = _.flatten([(this.variable = variable), (this.args = (args || []))]);
this.children = flatten([(this.variable = variable), (this.args = (args || []))]);
this.prefix = '';
return this;
},
@@ -384,9 +418,7 @@
// Compile a vanilla function call.
compile_node: function compile_node(o) {
var _a, _b, _c, arg, args;
if (_.any(this.args, function(a) {
return a instanceof SplatNode;
})) {
if (this.args[this.args.length - 1] instanceof SplatNode) {
return this.compile_splat(o);
}
args = ((function() {
@@ -812,12 +844,17 @@
return true;
},
toString: function toString(idt) {
var children;
var _a, _b, _c, child, children;
idt = idt || '';
children = _.flatten([this.params, this.body.expressions]);
return '\n' + idt + this.type + _.map(children, function(child) {
return child.toString(idt + TAB);
}).join('');
children = flatten([this.params, this.body.expressions]);
return '\n' + idt + this.type + ((function() {
_a = []; _b = children;
for (_c = 0; _c < _b.length; _c++) {
child = _b[_c];
_a.push(child.toString(idt + TAB));
}
return _a;
}).call(this)).join('');
}
}));
// A splat, either as a parameter to a function, an argument to a call,
@@ -894,7 +931,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;
@@ -966,7 +1003,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;
},
@@ -1055,7 +1092,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() {
@@ -1142,7 +1179,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;
@@ -1200,7 +1237,7 @@
compile_condition: function compile_condition(o) {
var _a, _b, _c, cond;
return ((function() {
_a = []; _b = _.flatten([this.condition]);
_a = []; _b = flatten([this.condition]);
for (_c = 0; _c < _b.length; _c++) {
cond = _b[_c];
_a.push(cond.compile(o));
@@ -1216,7 +1253,7 @@
compile_statement: function compile_statement(o) {
var body, child, com_dent, cond_o, else_part, if_dent, if_part, prefix;
child = del(o, 'chain_child');
cond_o = _.clone(o);
cond_o = merge(o);
del(cond_o, 'returns');
o.indent = this.idt(1);
o.top = true;