1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | "use strict";
|
45 |
|
46 | function characters(str) {
|
47 | return str.split("");
|
48 | }
|
49 |
|
50 | function member(name, array) {
|
51 | return array.indexOf(name) >= 0;
|
52 | }
|
53 |
|
54 | function find_if(func, array) {
|
55 | for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
|
56 | }
|
57 |
|
58 | function repeat_string(str, i) {
|
59 | if (i <= 0) return "";
|
60 | if (i == 1) return str;
|
61 | var d = repeat_string(str, i >> 1);
|
62 | d += d;
|
63 | return i & 1 ? d + str : d;
|
64 | }
|
65 |
|
66 | function configure_error_stack(fn) {
|
67 | Object.defineProperty(fn.prototype, "stack", {
|
68 | get: function() {
|
69 | var err = new Error(this.message);
|
70 | err.name = this.name;
|
71 | try {
|
72 | throw err;
|
73 | } catch (e) {
|
74 | return e.stack;
|
75 | }
|
76 | }
|
77 | });
|
78 | }
|
79 |
|
80 | function DefaultsError(msg, defs) {
|
81 | this.message = msg;
|
82 | this.defs = defs;
|
83 | }
|
84 | DefaultsError.prototype = Object.create(Error.prototype);
|
85 | DefaultsError.prototype.constructor = DefaultsError;
|
86 | DefaultsError.prototype.name = "DefaultsError";
|
87 | configure_error_stack(DefaultsError);
|
88 |
|
89 | function defaults(args, defs, croak) {
|
90 | if (croak) for (var i in args) {
|
91 | if (HOP(args, i) && !HOP(defs, i)) throw new DefaultsError("`" + i + "` is not a supported option", defs);
|
92 | }
|
93 | for (var i in args) {
|
94 | if (HOP(args, i)) defs[i] = args[i];
|
95 | }
|
96 | return defs;
|
97 | }
|
98 |
|
99 | function merge(obj, ext) {
|
100 | var count = 0;
|
101 | for (var i in ext) if (HOP(ext, i)) {
|
102 | obj[i] = ext[i];
|
103 | count++;
|
104 | }
|
105 | return count;
|
106 | }
|
107 |
|
108 | function noop() {}
|
109 | function return_false() { return false; }
|
110 | function return_true() { return true; }
|
111 | function return_this() { return this; }
|
112 | function return_null() { return null; }
|
113 |
|
114 | var List = (function() {
|
115 | function List(a, f) {
|
116 | var ret = [];
|
117 | for (var i = 0; i < a.length; i++) {
|
118 | var val = f(a[i], i);
|
119 | if (val === skip) continue;
|
120 | if (val instanceof Splice) {
|
121 | ret.push.apply(ret, val.v);
|
122 | } else {
|
123 | ret.push(val);
|
124 | }
|
125 | }
|
126 | return ret;
|
127 | }
|
128 | List.is_op = function(val) {
|
129 | return val === skip || val instanceof Splice;
|
130 | };
|
131 | List.splice = function(val) {
|
132 | return new Splice(val);
|
133 | };
|
134 | var skip = List.skip = {};
|
135 | function Splice(val) {
|
136 | this.v = val;
|
137 | }
|
138 | return List;
|
139 | })();
|
140 |
|
141 | function push_uniq(array, el) {
|
142 | if (array.indexOf(el) < 0) return array.push(el);
|
143 | }
|
144 |
|
145 | function string_template(text, props) {
|
146 | return text.replace(/\{([^{}]+)\}/g, function(str, p) {
|
147 | var value = props[p];
|
148 | return value instanceof AST_Node ? value.print_to_string() : value;
|
149 | });
|
150 | }
|
151 |
|
152 | function remove(array, el) {
|
153 | var index = array.indexOf(el);
|
154 | if (index >= 0) array.splice(index, 1);
|
155 | }
|
156 |
|
157 | function makePredicate(words) {
|
158 | if (!Array.isArray(words)) words = words.split(" ");
|
159 | var map = Object.create(null);
|
160 | words.forEach(function(word) {
|
161 | map[word] = true;
|
162 | });
|
163 | return map;
|
164 | }
|
165 |
|
166 | function all(array, predicate) {
|
167 | for (var i = array.length; --i >= 0;)
|
168 | if (!predicate(array[i], i))
|
169 | return false;
|
170 | return true;
|
171 | }
|
172 |
|
173 | function Dictionary() {
|
174 | this._values = Object.create(null);
|
175 | this._size = 0;
|
176 | }
|
177 | Dictionary.prototype = {
|
178 | set: function(key, val) {
|
179 | if (!this.has(key)) ++this._size;
|
180 | this._values["$" + key] = val;
|
181 | return this;
|
182 | },
|
183 | add: function(key, val) {
|
184 | if (this.has(key)) {
|
185 | this.get(key).push(val);
|
186 | } else {
|
187 | this.set(key, [ val ]);
|
188 | }
|
189 | return this;
|
190 | },
|
191 | get: function(key) { return this._values["$" + key] },
|
192 | del: function(key) {
|
193 | if (this.has(key)) {
|
194 | --this._size;
|
195 | delete this._values["$" + key];
|
196 | }
|
197 | return this;
|
198 | },
|
199 | has: function(key) { return ("$" + key) in this._values },
|
200 | all: function(predicate) {
|
201 | for (var i in this._values)
|
202 | if (!predicate(this._values[i], i.substr(1)))
|
203 | return false;
|
204 | return true;
|
205 | },
|
206 | each: function(f) {
|
207 | for (var i in this._values)
|
208 | f(this._values[i], i.substr(1));
|
209 | },
|
210 | size: function() {
|
211 | return this._size;
|
212 | },
|
213 | map: function(f) {
|
214 | var ret = [];
|
215 | for (var i in this._values)
|
216 | ret.push(f(this._values[i], i.substr(1)));
|
217 | return ret;
|
218 | },
|
219 | clone: function() {
|
220 | var ret = new Dictionary();
|
221 | for (var i in this._values)
|
222 | ret._values[i] = this._values[i];
|
223 | ret._size = this._size;
|
224 | return ret;
|
225 | },
|
226 | toObject: function() { return this._values }
|
227 | };
|
228 | Dictionary.fromObject = function(obj) {
|
229 | var dict = new Dictionary();
|
230 | dict._size = merge(dict._values, obj);
|
231 | return dict;
|
232 | };
|
233 |
|
234 | function HOP(obj, prop) {
|
235 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
236 | }
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | function first_in_statement(stack, arrow, export_default) {
|
242 | var node = stack.parent(-1);
|
243 | for (var i = 0, p; p = stack.parent(i++); node = p) {
|
244 | if (is_arrow(p)) {
|
245 | return arrow && p.value === node;
|
246 | } else if (p instanceof AST_Binary) {
|
247 | if (p.left === node) continue;
|
248 | } else if (p.TYPE == "Call") {
|
249 | if (p.expression === node) continue;
|
250 | } else if (p instanceof AST_Conditional) {
|
251 | if (p.condition === node) continue;
|
252 | } else if (p instanceof AST_ExportDefault) {
|
253 | return export_default;
|
254 | } else if (p instanceof AST_PropAccess) {
|
255 | if (p.expression === node) continue;
|
256 | } else if (p instanceof AST_Sequence) {
|
257 | if (p.expressions[0] === node) continue;
|
258 | } else if (p instanceof AST_SimpleStatement) {
|
259 | return true;
|
260 | } else if (p instanceof AST_Template) {
|
261 | if (p.tag === node) continue;
|
262 | } else if (p instanceof AST_UnaryPostfix) {
|
263 | if (p.expression === node) continue;
|
264 | }
|
265 | return false;
|
266 | }
|
267 | }
|