@angular-wave/angular.ts 0.0.49 → 0.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,561 +0,0 @@
1
- import {
2
- findConstantAndWatchExpressions,
3
- assignableAST,
4
- getInputs,
5
- ifDefined,
6
- getStringValue,
7
- plusFn,
8
- } from "./shared";
9
- import { forEach, isDefined, isNumber, isString } from "../../shared/utils";
10
- import { ASTType } from "./ast-type";
11
- import { $parseMinErr } from "./parse";
12
-
13
- export function ASTCompiler($filter) {
14
- this.$filter = $filter;
15
- }
16
-
17
- ASTCompiler.prototype = {
18
- compile(ast) {
19
- const self = this;
20
- this.state = {
21
- nextId: 0,
22
- filters: {},
23
- fn: { vars: [], body: [], own: {} },
24
- assign: { vars: [], body: [], own: {} },
25
- inputs: [],
26
- };
27
- findConstantAndWatchExpressions(ast, self.$filter);
28
- let extra = "";
29
- let assignable;
30
- this.stage = "assign";
31
- if ((assignable = assignableAST(ast))) {
32
- this.state.computing = "assign";
33
- const result = this.nextId();
34
- this.recurse(assignable, result);
35
- this.return_(result);
36
- extra = `fn.assign=${this.generateFunction("assign", "s,v,l")}`;
37
- }
38
- const toWatch = getInputs(ast.body);
39
- self.stage = "inputs";
40
- forEach(toWatch, (watch, key) => {
41
- const fnKey = `fn${key}`;
42
- self.state[fnKey] = { vars: [], body: [], own: {} };
43
- self.state.computing = fnKey;
44
- const intoId = self.nextId();
45
- self.recurse(watch, intoId);
46
- self.return_(intoId);
47
- self.state.inputs.push({ name: fnKey, isPure: watch.isPure });
48
- watch.watchId = key;
49
- });
50
- this.state.computing = "fn";
51
- this.stage = "main";
52
- this.recurse(ast);
53
- const fnString = `\n${this.filterPrefix()}let fn=${this.generateFunction(
54
- "fn",
55
- "s,l,a,i",
56
- )}${extra}${this.watchFns()}return fn;`;
57
-
58
- const fn = new Function(
59
- "$filter",
60
- "getStringValue",
61
- "ifDefined",
62
- "plus",
63
- fnString,
64
- )(this.$filter, getStringValue, ifDefined, plusFn);
65
- this.state = this.stage = undefined;
66
- return fn;
67
- },
68
-
69
- watchFns() {
70
- const result = [];
71
- const { inputs } = this.state;
72
- const self = this;
73
- forEach(inputs, (input) => {
74
- result.push(
75
- `let ${input.name}=${self.generateFunction(input.name, "s")}`,
76
- );
77
- if (input.isPure) {
78
- result.push(input.name, `.isPure=${JSON.stringify(input.isPure)};`);
79
- }
80
- });
81
- if (inputs.length) {
82
- result.push(`fn.inputs=[${inputs.map((i) => i.name).join(",")}];`);
83
- }
84
- return result.join("");
85
- },
86
-
87
- generateFunction(name, params) {
88
- return `function(${params}){${this.varsPrefix(name)}${this.body(name)}};`;
89
- },
90
-
91
- filterPrefix() {
92
- const parts = [];
93
- const self = this;
94
- forEach(this.state.filters, (id, filter) => {
95
- parts.push(`${id}=$filter(${self.escape(filter)})`);
96
- });
97
- if (parts.length) return `let ${parts.join(",")};`;
98
- return "";
99
- },
100
-
101
- varsPrefix(section) {
102
- return this.state[section].vars.length
103
- ? `let ${this.state[section].vars.join(",")};`
104
- : "";
105
- },
106
-
107
- body(section) {
108
- return this.state[section].body.join("");
109
- },
110
-
111
- recurse(ast, intoId, nameId, recursionFn, create, skipWatchIdCheck) {
112
- let left;
113
- let right;
114
- const self = this;
115
- let args;
116
- let expression;
117
- let computed;
118
- recursionFn = recursionFn || (() => {});
119
- if (!skipWatchIdCheck && isDefined(ast.watchId)) {
120
- intoId = intoId || this.nextId();
121
- this.if_(
122
- "i",
123
- this.lazyAssign(intoId, this.computedMember("i", ast.watchId)),
124
- this.lazyRecurse(ast, intoId, nameId, recursionFn, create, true),
125
- );
126
- return;
127
- }
128
- switch (ast.type) {
129
- case ASTType.Program:
130
- forEach(ast.body, (expression, pos) => {
131
- self.recurse(expression.expression, undefined, undefined, (expr) => {
132
- right = expr;
133
- });
134
- if (pos !== ast.body.length - 1) {
135
- self.current().body.push(right, ";");
136
- } else {
137
- self.return_(right);
138
- }
139
- });
140
- break;
141
- case ASTType.Literal:
142
- expression = this.escape(ast.value);
143
- this.assign(intoId, expression);
144
- recursionFn(intoId || expression);
145
- break;
146
- case ASTType.UnaryExpression:
147
- this.recurse(ast.argument, undefined, undefined, (expr) => {
148
- right = expr;
149
- });
150
- expression = `${ast.operator}(${this.ifDefined(right, 0)})`;
151
- this.assign(intoId, expression);
152
- recursionFn(expression);
153
- break;
154
- case ASTType.BinaryExpression:
155
- this.recurse(ast.left, undefined, undefined, (expr) => {
156
- left = expr;
157
- });
158
- this.recurse(ast.right, undefined, undefined, (expr) => {
159
- right = expr;
160
- });
161
- if (ast.operator === "+") {
162
- expression = this.plus(left, right);
163
- } else if (ast.operator === "-") {
164
- expression =
165
- this.ifDefined(left, 0) + ast.operator + this.ifDefined(right, 0);
166
- } else {
167
- expression = `(${left})${ast.operator}(${right})`;
168
- }
169
- this.assign(intoId, expression);
170
- recursionFn(expression);
171
- break;
172
- case ASTType.LogicalExpression:
173
- intoId = intoId || this.nextId();
174
- self.recurse(ast.left, intoId);
175
- self.if_(
176
- ast.operator === "&&" ? intoId : self.not(intoId),
177
- self.lazyRecurse(ast.right, intoId),
178
- );
179
- recursionFn(intoId);
180
- break;
181
- case ASTType.ConditionalExpression:
182
- intoId = intoId || this.nextId();
183
- self.recurse(ast.test, intoId);
184
- self.if_(
185
- intoId,
186
- self.lazyRecurse(ast.alternate, intoId),
187
- self.lazyRecurse(ast.consequent, intoId),
188
- );
189
- recursionFn(intoId);
190
- break;
191
- case ASTType.Identifier:
192
- intoId = intoId || this.nextId();
193
- if (nameId) {
194
- nameId.context =
195
- self.stage === "inputs"
196
- ? "s"
197
- : this.assign(
198
- this.nextId(),
199
- `${this.getHasOwnProperty("l", ast.name)}?l:s`,
200
- );
201
- nameId.computed = false;
202
- nameId.name = ast.name;
203
- }
204
- self.if_(
205
- self.stage === "inputs" ||
206
- self.not(self.getHasOwnProperty("l", ast.name)),
207
- () => {
208
- self.if_(self.stage === "inputs" || "s", () => {
209
- if (create && create !== 1) {
210
- self.if_(
211
- self.isNull(self.nonComputedMember("s", ast.name)),
212
- self.lazyAssign(self.nonComputedMember("s", ast.name), "{}"),
213
- );
214
- }
215
- self.assign(intoId, self.nonComputedMember("s", ast.name));
216
- });
217
- },
218
- intoId &&
219
- self.lazyAssign(intoId, self.nonComputedMember("l", ast.name)),
220
- );
221
- recursionFn(intoId);
222
- break;
223
- case ASTType.MemberExpression:
224
- left = (nameId && (nameId.context = this.nextId())) || this.nextId();
225
- intoId = intoId || this.nextId();
226
- self.recurse(
227
- ast.object,
228
- left,
229
- undefined,
230
- () => {
231
- self.if_(
232
- self.notNull(left),
233
- () => {
234
- if (ast.computed) {
235
- right = self.nextId();
236
- self.recurse(ast.property, right);
237
- self.getStringValue(right);
238
- if (create && create !== 1) {
239
- self.if_(
240
- self.not(self.computedMember(left, right)),
241
- self.lazyAssign(self.computedMember(left, right), "{}"),
242
- );
243
- }
244
- expression = self.computedMember(left, right);
245
- self.assign(intoId, expression);
246
- if (nameId) {
247
- nameId.computed = true;
248
- nameId.name = right;
249
- }
250
- } else {
251
- if (create && create !== 1) {
252
- self.if_(
253
- self.isNull(
254
- self.nonComputedMember(left, ast.property.name),
255
- ),
256
- self.lazyAssign(
257
- self.nonComputedMember(left, ast.property.name),
258
- "{}",
259
- ),
260
- );
261
- }
262
- expression = self.nonComputedMember(left, ast.property.name);
263
- self.assign(intoId, expression);
264
- if (nameId) {
265
- nameId.computed = false;
266
- nameId.name = ast.property.name;
267
- }
268
- }
269
- },
270
- () => {
271
- self.assign(intoId, "undefined");
272
- },
273
- );
274
- recursionFn(intoId);
275
- },
276
- !!create,
277
- );
278
- break;
279
- case ASTType.CallExpression:
280
- intoId = intoId || this.nextId();
281
- if (ast.filter) {
282
- right = self.filter(ast.callee.name);
283
- args = [];
284
- forEach(ast.arguments, (expr) => {
285
- const argument = self.nextId();
286
- self.recurse(expr, argument);
287
- args.push(argument);
288
- });
289
- expression = `${right}(${args.join(",")})`;
290
- self.assign(intoId, expression);
291
- recursionFn(intoId);
292
- } else {
293
- right = self.nextId();
294
- left = {};
295
- args = [];
296
- self.recurse(ast.callee, right, left, () => {
297
- self.if_(
298
- self.notNull(right),
299
- () => {
300
- forEach(ast.arguments, (expr) => {
301
- self.recurse(
302
- expr,
303
- ast.constant ? undefined : self.nextId(),
304
- undefined,
305
- (argument) => {
306
- args.push(argument);
307
- },
308
- );
309
- });
310
- if (left.name) {
311
- expression = `${self.member(
312
- left.context,
313
- left.name,
314
- left.computed,
315
- )}(${args.join(",")})`;
316
- } else {
317
- expression = `${right}(${args.join(",")})`;
318
- }
319
- self.assign(intoId, expression);
320
- },
321
- () => {
322
- self.assign(intoId, "undefined");
323
- },
324
- );
325
- recursionFn(intoId);
326
- });
327
- }
328
- break;
329
- case ASTType.AssignmentExpression:
330
- right = this.nextId();
331
- left = {};
332
- this.recurse(
333
- ast.left,
334
- undefined,
335
- left,
336
- () => {
337
- self.if_(self.notNull(left.context), () => {
338
- self.recurse(ast.right, right);
339
- expression =
340
- self.member(left.context, left.name, left.computed) +
341
- ast.operator +
342
- right;
343
- self.assign(intoId, expression);
344
- recursionFn(intoId || expression);
345
- });
346
- },
347
- 1,
348
- );
349
- break;
350
- case ASTType.ArrayExpression:
351
- args = [];
352
- forEach(ast.elements, (expr) => {
353
- self.recurse(
354
- expr,
355
- ast.constant ? undefined : self.nextId(),
356
- undefined,
357
- (argument) => {
358
- args.push(argument);
359
- },
360
- );
361
- });
362
- expression = `[${args.join(",")}]`;
363
- this.assign(intoId, expression);
364
- recursionFn(intoId || expression);
365
- break;
366
- case ASTType.ObjectExpression:
367
- args = [];
368
- computed = false;
369
- forEach(ast.properties, (property) => {
370
- if (property.computed) {
371
- computed = true;
372
- }
373
- });
374
- if (computed) {
375
- intoId = intoId || this.nextId();
376
- this.assign(intoId, "{}");
377
- forEach(ast.properties, (property) => {
378
- if (property.computed) {
379
- left = self.nextId();
380
- self.recurse(property.key, left);
381
- } else {
382
- left =
383
- property.key.type === ASTType.Identifier
384
- ? property.key.name
385
- : `${property.key.value}`;
386
- }
387
- right = self.nextId();
388
- self.recurse(property.value, right);
389
- self.assign(self.member(intoId, left, property.computed), right);
390
- });
391
- } else {
392
- forEach(ast.properties, (property) => {
393
- self.recurse(
394
- property.value,
395
- ast.constant ? undefined : self.nextId(),
396
- undefined,
397
- (expr) => {
398
- args.push(
399
- `${self.escape(
400
- property.key.type === ASTType.Identifier
401
- ? property.key.name
402
- : `${property.key.value}`,
403
- )}:${expr}`,
404
- );
405
- },
406
- );
407
- });
408
- expression = `{${args.join(",")}}`;
409
- this.assign(intoId, expression);
410
- }
411
- recursionFn(intoId || expression);
412
- break;
413
- case ASTType.ThisExpression:
414
- this.assign(intoId, "s");
415
- recursionFn(intoId || "s");
416
- break;
417
- case ASTType.LocalsExpression:
418
- this.assign(intoId, "l");
419
- recursionFn(intoId || "l");
420
- break;
421
- case ASTType.NGValueParameter:
422
- this.assign(intoId, "v");
423
- recursionFn(intoId || "v");
424
- break;
425
- }
426
- },
427
-
428
- getHasOwnProperty(element, property) {
429
- const key = `${element}.${property}`;
430
- const { own } = this.current();
431
- if (!Object.prototype.hasOwnProperty.call(own, key)) {
432
- own[key] = this.nextId(
433
- false,
434
- `${element}&&(${this.escape(property)} in ${element})`,
435
- );
436
- }
437
- return own[key];
438
- },
439
-
440
- assign(id, value) {
441
- if (!id) return;
442
- this.current().body.push(id, "=", value, ";");
443
- return id;
444
- },
445
-
446
- filter(filterName) {
447
- if (!Object.prototype.hasOwnProperty.call(this.state.filters, filterName)) {
448
- this.state.filters[filterName] = this.nextId(true);
449
- }
450
- return this.state.filters[filterName];
451
- },
452
-
453
- ifDefined(id, defaultValue) {
454
- return `ifDefined(${id},${this.escape(defaultValue)})`;
455
- },
456
-
457
- plus(left, right) {
458
- return `plus(${left},${right})`;
459
- },
460
-
461
- return_(id) {
462
- this.current().body.push("return ", id, ";");
463
- },
464
-
465
- if_(test, alternate, consequent) {
466
- if (test === true) {
467
- alternate();
468
- } else {
469
- const { body } = this.current();
470
- body.push("if(", test, "){");
471
- alternate();
472
- body.push("}");
473
- if (consequent) {
474
- body.push("else{");
475
- consequent();
476
- body.push("}");
477
- }
478
- }
479
- },
480
-
481
- not(expression) {
482
- return `!(${expression})`;
483
- },
484
-
485
- isNull(expression) {
486
- return `${expression}==null`;
487
- },
488
-
489
- notNull(expression) {
490
- return `${expression}!=null`;
491
- },
492
-
493
- nonComputedMember(left, right) {
494
- const SAFE_IDENTIFIER = /^[$_a-zA-Z][$_a-zA-Z0-9]*$/;
495
- const UNSAFE_CHARACTERS = /[^$_a-zA-Z0-9]/g;
496
- if (SAFE_IDENTIFIER.test(right)) {
497
- return `${left}.${right}`;
498
- }
499
- return `${left}["${right.replace(
500
- UNSAFE_CHARACTERS,
501
- this.stringEscapeFn,
502
- )}"]`;
503
- },
504
-
505
- computedMember(left, right) {
506
- return `${left}[${right}]`;
507
- },
508
-
509
- member(left, right, computed) {
510
- if (computed) return this.computedMember(left, right);
511
- return this.nonComputedMember(left, right);
512
- },
513
-
514
- getStringValue(item) {
515
- this.assign(item, `getStringValue(${item})`);
516
- },
517
-
518
- lazyRecurse(ast, intoId, nameId, recursionFn, create, skipWatchIdCheck) {
519
- const self = this;
520
- return function () {
521
- self.recurse(ast, intoId, nameId, recursionFn, create, skipWatchIdCheck);
522
- };
523
- },
524
-
525
- lazyAssign(id, value) {
526
- const self = this;
527
- return function () {
528
- self.assign(id, value);
529
- };
530
- },
531
-
532
- stringEscapeRegex: /[^ a-zA-Z0-9]/g,
533
-
534
- stringEscapeFn(c) {
535
- return `\\u${`0000${c.charCodeAt(0).toString(16)}`.slice(-4)}`;
536
- },
537
-
538
- escape(value) {
539
- if (isString(value))
540
- return `'${value.replace(this.stringEscapeRegex, this.stringEscapeFn)}'`;
541
- if (isNumber(value)) return value.toString();
542
- if (value === true) return "true";
543
- if (value === false) return "false";
544
- if (value === null) return "null";
545
- if (typeof value === "undefined") return "undefined";
546
-
547
- throw $parseMinErr("esc", "IMPOSSIBLE");
548
- },
549
-
550
- nextId(skip, init) {
551
- const id = `v${this.state.nextId++}`;
552
- if (!skip) {
553
- this.current().vars.push(id + (init ? `=${init}` : ""));
554
- }
555
- return id;
556
- },
557
-
558
- current() {
559
- return this.state[this.state.computing];
560
- },
561
- };
@@ -1,49 +0,0 @@
1
- export function ASTCompiler($filter: any): void;
2
- export class ASTCompiler {
3
- constructor($filter: any);
4
- $filter: any;
5
- compile(ast: any): any;
6
- state: {
7
- nextId: number;
8
- filters: {};
9
- fn: {
10
- vars: any[];
11
- body: any[];
12
- own: {};
13
- };
14
- assign: {
15
- vars: any[];
16
- body: any[];
17
- own: {};
18
- };
19
- inputs: any[];
20
- };
21
- stage: string;
22
- watchFns(): string;
23
- generateFunction(name: any, params: any): string;
24
- filterPrefix(): string;
25
- varsPrefix(section: any): string;
26
- body(section: any): any;
27
- recurse(ast: any, intoId: any, nameId: any, recursionFn: any, create: any, skipWatchIdCheck: any): void;
28
- getHasOwnProperty(element: any, property: any): any;
29
- assign(id: any, value: any): any;
30
- filter(filterName: any): any;
31
- ifDefined(id: any, defaultValue: any): string;
32
- plus(left: any, right: any): string;
33
- return_(id: any): void;
34
- if_(test: any, alternate: any, consequent: any): void;
35
- not(expression: any): string;
36
- isNull(expression: any): string;
37
- notNull(expression: any): string;
38
- nonComputedMember(left: any, right: any): string;
39
- computedMember(left: any, right: any): string;
40
- member(left: any, right: any, computed: any): string;
41
- getStringValue(item: any): void;
42
- lazyRecurse(ast: any, intoId: any, nameId: any, recursionFn: any, create: any, skipWatchIdCheck: any): () => void;
43
- lazyAssign(id: any, value: any): () => void;
44
- stringEscapeRegex: RegExp;
45
- stringEscapeFn(c: any): string;
46
- escape(value: any): any;
47
- nextId(skip: any, init: any): string;
48
- current(): any;
49
- }