Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 495ca64

Browse files
committed
removing underscore as a dependency for nodes.coffee -- let's be minimal
1 parent 0f2cf55 commit 495ca64

4 files changed

Lines changed: 102 additions & 56 deletions

File tree

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace :build do
3535

3636
desc "Rebuild the Underscore.coffee documentation page"
3737
task :underscore do
38-
sh "uv -s coffeescript -t idle -h src/underscore.coffee > documentation/underscore.html"
38+
sh "uv -s coffeescript -t idle -h examples/underscore.coffee > documentation/underscore.html"
3939
end
4040

4141
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240

241241

242242
# Trim out all falsy values from an array.
243-
_.compact: (array) -> array[i] for i in [0...array.length] when array[i]
243+
_.compact: (array) -> item for item in array when item
244244

245245

246246
# Return a completely flattened version of an array.

lib/coffee_script/nodes.js

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
(function(){
2-
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;
2+
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;
33
var __hasProp = Object.prototype.hasOwnProperty;
4-
if ((typeof process !== "undefined" && process !== null)) {
5-
process.mixin(require('./scope'));
6-
_ = require('./underscore')._;
7-
} else {
8-
this.exports = this;
9-
_ = this._;
10-
}
4+
(typeof process !== "undefined" && process !== null) ? process.mixin(require('./scope')) : (this.exports = this);
115
// Some helper functions
126
// Tabs are two spaces for pretty printing.
137
TAB = ' ';
@@ -16,10 +10,44 @@
1610
IDENTIFIER = /^[a-zA-Z$_](\w|\$)*$/;
1711
// Merge objects.
1812
merge = function merge(options, overrides) {
19-
return _.tap({}, function(fresh) {
20-
_.extend(fresh, options);
21-
return _.extend(fresh, overrides);
22-
});
13+
var _a, _b, fresh, key, val;
14+
fresh = {};
15+
_a = options;
16+
for (key in _a) if (__hasProp.call(_a, key)) {
17+
val = _a[key];
18+
((fresh[key] = val));
19+
}
20+
if (overrides) {
21+
_b = overrides;
22+
for (key in _b) if (__hasProp.call(_b, key)) {
23+
val = _b[key];
24+
((fresh[key] = val));
25+
}
26+
}
27+
return fresh;
28+
};
29+
// Trim out all falsy values from an array.
30+
compact = function compact(array) {
31+
var _a, _b, _c, item;
32+
_a = []; _b = array;
33+
for (_c = 0; _c < _b.length; _c++) {
34+
item = _b[_c];
35+
if (item) {
36+
_a.push(item);
37+
}
38+
}
39+
return _a;
40+
};
41+
// Return a completely flattened version of an array.
42+
flatten = function flatten(array) {
43+
var _a, _b, item, memo;
44+
memo = [];
45+
_a = array;
46+
for (_b = 0; _b < _a.length; _b++) {
47+
item = _a[_b];
48+
item instanceof Array ? (memo = memo.concat(item)) : memo.push(item);
49+
}
50+
return memo;
2351
};
2452
// Delete a key from an object, returning the value.
2553
del = function del(obj, key) {
@@ -69,7 +97,7 @@
6997
// already been asked to return the result.
7098
Node.prototype.compile = function compile(o) {
7199
var closure, top;
72-
this.options = _.clone(o || {});
100+
this.options = merge(o || {});
73101
this.indent = o.indent;
74102
top = this.top_sensitive() ? this.options.top : del(this.options, 'top');
75103
closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) {
@@ -111,10 +139,16 @@
111139
};
112140
// toString representation of the node, for inspecting the parse tree.
113141
Node.prototype.toString = function toString(idt) {
142+
var _a, _b, _c, child;
114143
idt = idt || '';
115-
return '\n' + idt + this.type + _.map(this.children, function(child) {
116-
return child.toString(idt + TAB);
117-
}).join('');
144+
return '\n' + idt + this.type + ((function() {
145+
_a = []; _b = this.children;
146+
for (_c = 0; _c < _b.length; _c++) {
147+
child = _b[_c];
148+
_a.push(child.toString(idt + TAB));
149+
}
150+
return _a;
151+
}).call(this)).join('');
118152
};
119153
// Default implementations of the common node methods.
120154
Node.prototype.unwrap = function unwrap() {
@@ -134,7 +168,7 @@
134168
Expressions = (exports.Expressions = inherit(Node, {
135169
type: 'Expressions',
136170
constructor: function constructor(nodes) {
137-
this.children = (this.expressions = _.compact(_.flatten(nodes || [])));
171+
this.children = (this.expressions = compact(flatten(nodes || [])));
138172
return this;
139173
},
140174
// Tack an expression on to the end of this expression list.
@@ -173,7 +207,7 @@
173207
_a = []; _b = this.expressions;
174208
for (_c = 0; _c < _b.length; _c++) {
175209
node = _b[_c];
176-
_a.push(this.compile_expression(node, _.clone(o)));
210+
_a.push(this.compile_expression(node, merge(o)));
177211
}
178212
return _a;
179213
}).call(this)).join("\n");
@@ -284,7 +318,7 @@
284318
type: 'Value',
285319
SOAK: " == undefined ? undefined : ",
286320
constructor: function constructor(base, properties) {
287-
this.children = _.flatten([(this.base = base), (this.properties = (properties || []))]);
321+
this.children = flatten([(this.base = base), (this.properties = (properties || []))]);
288322
return this;
289323
},
290324
push: function push(prop) {
@@ -368,7 +402,7 @@
368402
CallNode = (exports.CallNode = inherit(Node, {
369403
type: 'Call',
370404
constructor: function constructor(variable, args) {
371-
this.children = _.flatten([(this.variable = variable), (this.args = (args || []))]);
405+
this.children = flatten([(this.variable = variable), (this.args = (args || []))]);
372406
this.prefix = '';
373407
return this;
374408
},
@@ -384,9 +418,7 @@
384418
// Compile a vanilla function call.
385419
compile_node: function compile_node(o) {
386420
var _a, _b, _c, arg, args;
387-
if (_.any(this.args, function(a) {
388-
return a instanceof SplatNode;
389-
})) {
421+
if (this.args[this.args.length - 1] instanceof SplatNode) {
390422
return this.compile_splat(o);
391423
}
392424
args = ((function() {
@@ -812,12 +844,17 @@
812844
return true;
813845
},
814846
toString: function toString(idt) {
815-
var children;
847+
var _a, _b, _c, child, children;
816848
idt = idt || '';
817-
children = _.flatten([this.params, this.body.expressions]);
818-
return '\n' + idt + this.type + _.map(children, function(child) {
819-
return child.toString(idt + TAB);
820-
}).join('');
849+
children = flatten([this.params, this.body.expressions]);
850+
return '\n' + idt + this.type + ((function() {
851+
_a = []; _b = children;
852+
for (_c = 0; _c < _b.length; _c++) {
853+
child = _b[_c];
854+
_a.push(child.toString(idt + TAB));
855+
}
856+
return _a;
857+
}).call(this)).join('');
821858
}
822859
}));
823860
// A splat, either as a parameter to a function, an argument to a call,
@@ -894,7 +931,7 @@
894931
ASSIGNMENT: ['||=', '&&=', '?='],
895932
PREFIX_OPERATORS: ['typeof', 'delete'],
896933
constructor: function constructor(operator, first, second, flip) {
897-
this.children = _.compact([(this.first = first), (this.second = second)]);
934+
this.children = compact([(this.first = first), (this.second = second)]);
898935
this.operator = this.CONVERSIONS[operator] || operator;
899936
this.flip = !!flip;
900937
return this;
@@ -966,7 +1003,7 @@
9661003
TryNode = (exports.TryNode = inherit(Node, {
9671004
type: 'Try',
9681005
constructor: function constructor(attempt, error, recovery, ensure) {
969-
this.children = _.compact([(this.attempt = attempt), (this.recovery = recovery), (this.ensure = ensure)]);
1006+
this.children = compact([(this.attempt = attempt), (this.recovery = recovery), (this.ensure = ensure)]);
9701007
this.error = error;
9711008
return this;
9721009
},
@@ -1055,7 +1092,7 @@
10551092
this.name = _a[0];
10561093
this.index = _a[1];
10571094
}
1058-
this.children = _.compact([this.body, this.source, this.filter]);
1095+
this.children = compact([this.body, this.source, this.filter]);
10591096
return this;
10601097
},
10611098
top_sensitive: function top_sensitive() {
@@ -1142,7 +1179,7 @@
11421179
this.condition = condition;
11431180
this.body = body && body.unwrap();
11441181
this.else_body = else_body && else_body.unwrap();
1145-
this.children = _.compact([this.condition, this.body, this.else_body]);
1182+
this.children = compact([this.condition, this.body, this.else_body]);
11461183
this.tags = tags || {};
11471184
if (this.condition instanceof Array) {
11481185
this.multiple = true;
@@ -1200,7 +1237,7 @@
12001237
compile_condition: function compile_condition(o) {
12011238
var _a, _b, _c, cond;
12021239
return ((function() {
1203-
_a = []; _b = _.flatten([this.condition]);
1240+
_a = []; _b = flatten([this.condition]);
12041241
for (_c = 0; _c < _b.length; _c++) {
12051242
cond = _b[_c];
12061243
_a.push(cond.compile(o));
@@ -1216,7 +1253,7 @@
12161253
compile_statement: function compile_statement(o) {
12171254
var body, child, com_dent, cond_o, else_part, if_dent, if_part, prefix;
12181255
child = del(o, 'chain_child');
1219-
cond_o = _.clone(o);
1256+
cond_o = merge(o);
12201257
del(cond_o, 'returns');
12211258
o.indent = this.idt(1);
12221259
o.top = true;

src/nodes.coffee

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
if process?
22
process.mixin require './scope'
3-
_: require('./underscore')._
43
else
54
this.exports: this
6-
_: this._
75

86
# Some helper functions
97

@@ -16,9 +14,20 @@ IDENTIFIER: /^[a-zA-Z$_](\w|\$)*$/
1614

1715
# Merge objects.
1816
merge: (options, overrides) ->
19-
_.tap {}, (fresh) ->
20-
_.extend fresh, options
21-
_.extend fresh, overrides
17+
fresh: {}
18+
(fresh[key]: val) for key, val of options
19+
(fresh[key]: val) for key, val of overrides if overrides
20+
fresh
21+
22+
# Trim out all falsy values from an array.
23+
compact: (array) -> item for item in array when item
24+
25+
# Return a completely flattened version of an array.
26+
flatten: (array) ->
27+
memo: []
28+
for item in array
29+
if item instanceof Array then memo: memo.concat(item) else memo.push(item)
30+
memo
2231

2332
# Delete a key from an object, returning the value.
2433
del: (obj, key) ->
@@ -52,7 +61,7 @@ Node: exports.Node: ->
5261
# the top level of a block (which would be unnecessary), and we haven't
5362
# already been asked to return the result.
5463
Node::compile: (o) ->
55-
@options: _.clone o or {}
64+
@options: merge o or {}
5665
@indent: o.indent
5766
top: if @top_sensitive() then @options.top else del @options, 'top'
5867
closure: @is_statement() and not @is_statement_only() and not top and
@@ -83,7 +92,7 @@ Node::contains: (block) ->
8392
# toString representation of the node, for inspecting the parse tree.
8493
Node::toString: (idt) ->
8594
idt ||= ''
86-
'\n' + idt + @type + _.map(@children, (child) -> child.toString(idt + TAB)).join('')
95+
'\n' + idt + @type + (child.toString(idt + TAB) for child in @children).join('')
8796

8897
# Default implementations of the common node methods.
8998
Node::unwrap: -> this
@@ -97,7 +106,7 @@ Expressions: exports.Expressions: inherit Node, {
97106
type: 'Expressions'
98107

99108
constructor: (nodes) ->
100-
@children: @expressions: _.compact _.flatten nodes or []
109+
@children: @expressions: compact flatten nodes or []
101110
this
102111

103112
# Tack an expression on to the end of this expression list.
@@ -130,7 +139,7 @@ Expressions: exports.Expressions: inherit Node, {
130139

131140
# Compile each expression in the Expressions body.
132141
compile_node: (o) ->
133-
(@compile_expression(node, _.clone(o)) for node in @expressions).join("\n")
142+
(@compile_expression(node, merge(o)) for node in @expressions).join("\n")
134143

135144
# If this is the top-level Expressions, wrap everything in a safety closure.
136145
compile_root: (o) ->
@@ -222,7 +231,7 @@ ValueNode: exports.ValueNode: inherit Node, {
222231
SOAK: " == undefined ? undefined : "
223232

224233
constructor: (base, properties) ->
225-
@children: _.flatten [@base: base, @properties: (properties or [])]
234+
@children: flatten [@base: base, @properties: (properties or [])]
226235
this
227236

228237
push: (prop) ->
@@ -303,7 +312,7 @@ CallNode: exports.CallNode: inherit Node, {
303312
type: 'Call'
304313

305314
constructor: (variable, args) ->
306-
@children: _.flatten [@variable: variable, @args: (args or [])]
315+
@children: flatten [@variable: variable, @args: (args or [])]
307316
@prefix: ''
308317
this
309318

@@ -318,7 +327,7 @@ CallNode: exports.CallNode: inherit Node, {
318327

319328
# Compile a vanilla function call.
320329
compile_node: (o) ->
321-
return @compile_splat(o) if _.any @args, (a) -> a instanceof SplatNode
330+
return @compile_splat(o) if @args[@args.length - 1] instanceof SplatNode
322331
args: (arg.compile(o) for arg in @args).join(', ')
323332
return @compile_super(args, o) if @variable is 'super'
324333
@prefix + @variable.compile(o) + '(' + args + ')'
@@ -660,8 +669,8 @@ CodeNode: exports.CodeNode: inherit Node, {
660669

661670
toString: (idt) ->
662671
idt ||= ''
663-
children: _.flatten [@params, @body.expressions]
664-
'\n' + idt + @type + _.map(children, (child) -> child.toString(idt + TAB)).join('')
672+
children: flatten [@params, @body.expressions]
673+
'\n' + idt + @type + (child.toString(idt + TAB) for child in children).join('')
665674

666675
}
667676

@@ -740,7 +749,7 @@ OpNode: exports.OpNode: inherit Node, {
740749
PREFIX_OPERATORS: ['typeof', 'delete']
741750

742751
constructor: (operator, first, second, flip) ->
743-
@children: _.compact [@first: first, @second: second]
752+
@children: compact [@first: first, @second: second]
744753
@operator: @CONVERSIONS[operator] or operator
745754
@flip: !!flip
746755
this
@@ -788,7 +797,7 @@ TryNode: exports.TryNode: inherit Node, {
788797
type: 'Try'
789798

790799
constructor: (attempt, error, recovery, ensure) ->
791-
@children: _.compact [@attempt: attempt, @recovery: recovery, @ensure: ensure]
800+
@children: compact [@attempt: attempt, @recovery: recovery, @ensure: ensure]
792801
@error: error
793802
this
794803

@@ -869,7 +878,7 @@ ForNode: exports.ForNode: inherit Node, {
869878
@step: source.step
870879
@object: !!source.object
871880
[@name, @index]: [@index, @name] if @object
872-
@children: _.compact [@body, @source, @filter]
881+
@children: compact [@body, @source, @filter]
873882
this
874883

875884
top_sensitive: ->
@@ -933,7 +942,7 @@ IfNode: exports.IfNode: inherit Node, {
933942
@condition: condition
934943
@body: body and body.unwrap()
935944
@else_body: else_body and else_body.unwrap()
936-
@children: _.compact [@condition, @body, @else_body]
945+
@children: compact [@condition, @body, @else_body]
937946
@tags: tags or {}
938947
@multiple: true if @condition instanceof Array
939948
@condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert
@@ -973,7 +982,7 @@ IfNode: exports.IfNode: inherit Node, {
973982
@statement ||= !!(@comment or @tags.statement or @body.is_statement() or (@else_body and @else_body.is_statement()))
974983

975984
compile_condition: (o) ->
976-
(cond.compile(o) for cond in _.flatten([@condition])).join(' || ')
985+
(cond.compile(o) for cond in flatten([@condition])).join(' || ')
977986

978987
compile_node: (o) ->
979988
if @is_statement() then @compile_statement(o) else @compile_ternary(o)
@@ -982,7 +991,7 @@ IfNode: exports.IfNode: inherit Node, {
982991
# force sub-else bodies into statement form.
983992
compile_statement: (o) ->
984993
child: del o, 'chain_child'
985-
cond_o: _.clone o
994+
cond_o: merge o
986995
del cond_o, 'returns'
987996
o.indent: @idt(1)
988997
o.top: true

0 commit comments

Comments
 (0)