|
1 | 1 | (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; |
3 | 3 | 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); |
11 | 5 | // Some helper functions |
12 | 6 | // Tabs are two spaces for pretty printing. |
13 | 7 | TAB = ' '; |
|
16 | 10 | IDENTIFIER = /^[a-zA-Z$_](\w|\$)*$/; |
17 | 11 | // Merge objects. |
18 | 12 | 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; |
23 | 51 | }; |
24 | 52 | // Delete a key from an object, returning the value. |
25 | 53 | del = function del(obj, key) { |
|
69 | 97 | // already been asked to return the result. |
70 | 98 | Node.prototype.compile = function compile(o) { |
71 | 99 | var closure, top; |
72 | | - this.options = _.clone(o || {}); |
| 100 | + this.options = merge(o || {}); |
73 | 101 | this.indent = o.indent; |
74 | 102 | top = this.top_sensitive() ? this.options.top : del(this.options, 'top'); |
75 | 103 | closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) { |
|
111 | 139 | }; |
112 | 140 | // toString representation of the node, for inspecting the parse tree. |
113 | 141 | Node.prototype.toString = function toString(idt) { |
| 142 | + var _a, _b, _c, child; |
114 | 143 | 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(''); |
118 | 152 | }; |
119 | 153 | // Default implementations of the common node methods. |
120 | 154 | Node.prototype.unwrap = function unwrap() { |
|
134 | 168 | Expressions = (exports.Expressions = inherit(Node, { |
135 | 169 | type: 'Expressions', |
136 | 170 | constructor: function constructor(nodes) { |
137 | | - this.children = (this.expressions = _.compact(_.flatten(nodes || []))); |
| 171 | + this.children = (this.expressions = compact(flatten(nodes || []))); |
138 | 172 | return this; |
139 | 173 | }, |
140 | 174 | // Tack an expression on to the end of this expression list. |
|
173 | 207 | _a = []; _b = this.expressions; |
174 | 208 | for (_c = 0; _c < _b.length; _c++) { |
175 | 209 | node = _b[_c]; |
176 | | - _a.push(this.compile_expression(node, _.clone(o))); |
| 210 | + _a.push(this.compile_expression(node, merge(o))); |
177 | 211 | } |
178 | 212 | return _a; |
179 | 213 | }).call(this)).join("\n"); |
|
284 | 318 | type: 'Value', |
285 | 319 | SOAK: " == undefined ? undefined : ", |
286 | 320 | 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 || []))]); |
288 | 322 | return this; |
289 | 323 | }, |
290 | 324 | push: function push(prop) { |
|
368 | 402 | CallNode = (exports.CallNode = inherit(Node, { |
369 | 403 | type: 'Call', |
370 | 404 | 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 || []))]); |
372 | 406 | this.prefix = ''; |
373 | 407 | return this; |
374 | 408 | }, |
|
384 | 418 | // Compile a vanilla function call. |
385 | 419 | compile_node: function compile_node(o) { |
386 | 420 | 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) { |
390 | 422 | return this.compile_splat(o); |
391 | 423 | } |
392 | 424 | args = ((function() { |
|
812 | 844 | return true; |
813 | 845 | }, |
814 | 846 | toString: function toString(idt) { |
815 | | - var children; |
| 847 | + var _a, _b, _c, child, children; |
816 | 848 | 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(''); |
821 | 858 | } |
822 | 859 | })); |
823 | 860 | // A splat, either as a parameter to a function, an argument to a call, |
|
894 | 931 | ASSIGNMENT: ['||=', '&&=', '?='], |
895 | 932 | PREFIX_OPERATORS: ['typeof', 'delete'], |
896 | 933 | 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)]); |
898 | 935 | this.operator = this.CONVERSIONS[operator] || operator; |
899 | 936 | this.flip = !!flip; |
900 | 937 | return this; |
|
966 | 1003 | TryNode = (exports.TryNode = inherit(Node, { |
967 | 1004 | type: 'Try', |
968 | 1005 | 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)]); |
970 | 1007 | this.error = error; |
971 | 1008 | return this; |
972 | 1009 | }, |
|
1055 | 1092 | this.name = _a[0]; |
1056 | 1093 | this.index = _a[1]; |
1057 | 1094 | } |
1058 | | - this.children = _.compact([this.body, this.source, this.filter]); |
| 1095 | + this.children = compact([this.body, this.source, this.filter]); |
1059 | 1096 | return this; |
1060 | 1097 | }, |
1061 | 1098 | top_sensitive: function top_sensitive() { |
|
1142 | 1179 | this.condition = condition; |
1143 | 1180 | this.body = body && body.unwrap(); |
1144 | 1181 | 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]); |
1146 | 1183 | this.tags = tags || {}; |
1147 | 1184 | if (this.condition instanceof Array) { |
1148 | 1185 | this.multiple = true; |
|
1200 | 1237 | compile_condition: function compile_condition(o) { |
1201 | 1238 | var _a, _b, _c, cond; |
1202 | 1239 | return ((function() { |
1203 | | - _a = []; _b = _.flatten([this.condition]); |
| 1240 | + _a = []; _b = flatten([this.condition]); |
1204 | 1241 | for (_c = 0; _c < _b.length; _c++) { |
1205 | 1242 | cond = _b[_c]; |
1206 | 1243 | _a.push(cond.compile(o)); |
|
1216 | 1253 | compile_statement: function compile_statement(o) { |
1217 | 1254 | var body, child, com_dent, cond_o, else_part, if_dent, if_part, prefix; |
1218 | 1255 | child = del(o, 'chain_child'); |
1219 | | - cond_o = _.clone(o); |
| 1256 | + cond_o = merge(o); |
1220 | 1257 | del(cond_o, 'returns'); |
1221 | 1258 | o.indent = this.idt(1); |
1222 | 1259 | o.top = true; |
|
0 commit comments