babel-plugin-vasille 0.99.0

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.
@@ -0,0 +1,971 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.meshOrIgnoreAllExpressions = meshOrIgnoreAllExpressions;
27
+ exports.meshAllExpressions = meshAllExpressions;
28
+ exports.meshComposeCall = meshComposeCall;
29
+ exports.meshAllUnknown = meshAllUnknown;
30
+ exports.meshLValue = meshLValue;
31
+ exports.meshOrIgnoreExpression = meshOrIgnoreExpression;
32
+ exports.meshExpression = meshExpression;
33
+ exports.meshBody = meshBody;
34
+ exports.meshStatements = meshStatements;
35
+ exports.ignoreParams = ignoreParams;
36
+ exports.reactiveArrayPattern = reactiveArrayPattern;
37
+ exports.meshStatement = meshStatement;
38
+ exports.meshFunction = meshFunction;
39
+ exports.composeExpression = composeExpression;
40
+ exports.composeStatements = composeStatements;
41
+ exports.composeStatement = composeStatement;
42
+ exports.compose = compose;
43
+ const t = __importStar(require("@babel/types"));
44
+ const call_js_1 = require("./call.js");
45
+ const internal_js_1 = require("./internal.js");
46
+ const jsx_detect_js_1 = require("./jsx-detect.js");
47
+ const lib_js_1 = require("./lib.js");
48
+ const jsx_js_1 = require("./jsx.js");
49
+ function meshOrIgnoreAllExpressions(nodePaths, internal) {
50
+ for (const path of nodePaths) {
51
+ if (t.isExpression(path.node)) {
52
+ meshExpression(path, internal);
53
+ }
54
+ }
55
+ }
56
+ function meshAllExpressions(nodePaths, internal) {
57
+ for (const path of nodePaths) {
58
+ meshExpression(path, internal);
59
+ }
60
+ }
61
+ function meshComposeCall(call, name, nodePath, internal) {
62
+ const arg = call.arguments[0];
63
+ if (call.arguments.length !== 1 || !(t.isFunctionExpression(arg) || t.isArrowFunctionExpression(arg))) {
64
+ throw nodePath.buildCodeFrameError("Vasille: Invalid arguments");
65
+ }
66
+ const fnPath = nodePath.get("arguments")[0];
67
+ compose(fnPath, internal, false);
68
+ arg.params.unshift(internal_js_1.ctx);
69
+ if (t.isArrowFunctionExpression(arg) && internal.devMode) {
70
+ fnPath.replaceWith(t.functionExpression(t.identifier(internal.prefix + (name ? name.name : "Default")), arg.params, t.isBlockStatement(arg.body) ? arg.body : t.blockStatement([t.returnStatement(arg.body)]), false, arg.async));
71
+ }
72
+ else if (t.isFunctionExpression(arg) && name && internal.devMode) {
73
+ arg.id = t.identifier(internal.prefix + name.name);
74
+ }
75
+ }
76
+ function meshAllUnknown(paths, internal) {
77
+ for (const path of paths) {
78
+ if (t.isSpreadElement(path.node)) {
79
+ meshExpression(path.get("argument"), internal);
80
+ }
81
+ else if (t.isExpression(path.node)) {
82
+ meshExpression(path, internal);
83
+ }
84
+ }
85
+ }
86
+ function meshLValue(path, internal) {
87
+ const node = path.node;
88
+ if (t.isArrayPattern(node) ||
89
+ t.isObjectPattern(node) ||
90
+ t.isTSParameterProperty(node) ||
91
+ t.isAssignmentPattern(node) ||
92
+ t.isRestElement(node)) {
93
+ return;
94
+ }
95
+ meshExpression(path, internal);
96
+ }
97
+ function meshOrIgnoreExpression(path, internal) {
98
+ if (t.isExpression(path.node)) {
99
+ meshExpression(path, internal);
100
+ }
101
+ }
102
+ function meshExpression(nodePath, internal) {
103
+ const expr = nodePath.node;
104
+ if (!expr) {
105
+ return;
106
+ }
107
+ if ((0, call_js_1.calls)(expr, ["compose", "extend"], internal)) {
108
+ meshComposeCall(expr, null, nodePath, internal);
109
+ return;
110
+ }
111
+ switch (expr.type) {
112
+ case "TemplateLiteral": {
113
+ const path = nodePath;
114
+ meshOrIgnoreAllExpressions(path.get("expressions"), internal);
115
+ break;
116
+ }
117
+ case "TaggedTemplateExpression": {
118
+ const path = nodePath;
119
+ meshExpression(path.get("quasi"), internal);
120
+ break;
121
+ }
122
+ case "Identifier": {
123
+ const state = internal.stack.get(expr.name);
124
+ if (state === 2 /* VariableState.Reactive */) {
125
+ nodePath.replaceWith(t.memberExpression(expr, t.identifier("$")));
126
+ }
127
+ else if (state === 4 /* VariableState.ReactivePointer */) {
128
+ nodePath.replaceWith(t.memberExpression(expr, t.identifier("$$")));
129
+ }
130
+ break;
131
+ }
132
+ case "ArrayExpression": {
133
+ const path = nodePath;
134
+ meshAllUnknown(path.get("elements"), internal);
135
+ break;
136
+ }
137
+ case "TupleExpression": {
138
+ const path = nodePath;
139
+ meshAllUnknown(path.get("elements"), internal);
140
+ break;
141
+ }
142
+ case "CallExpression": {
143
+ const path = nodePath;
144
+ const callsFn = (0, call_js_1.calls)(path.node, call_js_1.composeOnly, internal);
145
+ if (callsFn) {
146
+ throw path.buildCodeFrameError(`Vasille: Usage of function "${callsFn}" is restricted here`);
147
+ }
148
+ meshOrIgnoreExpression(path.get("callee"), internal);
149
+ meshAllUnknown(path.get("arguments"), internal);
150
+ if ((0, call_js_1.calls)(path.node, ["calculate"], internal)) {
151
+ if (path.node.arguments.length !== 1 && !t.isExpression(path.node.arguments[0])) {
152
+ throw path.buildCodeFrameError("Vasille: Incorrect calculate argument");
153
+ }
154
+ path.replaceWith(t.callExpression(path.node.arguments[0], []));
155
+ }
156
+ break;
157
+ }
158
+ case "OptionalCallExpression": {
159
+ const path = nodePath;
160
+ meshExpression(path.get("callee"), internal);
161
+ meshAllUnknown(path.get("arguments"), internal);
162
+ break;
163
+ }
164
+ case "AssignmentExpression": {
165
+ const path = nodePath;
166
+ const left = path.node.left;
167
+ meshLValue(path.get("left"), internal);
168
+ if (t.isIdentifier(left) && internal.stack.get(left.name) === 4 /* VariableState.ReactivePointer */) {
169
+ const replaceWith = (0, lib_js_1.forwardOnlyExpr)(path.get("right"), path.node.right, internal);
170
+ if (replaceWith) {
171
+ path.get("right").replaceWith(replaceWith);
172
+ }
173
+ else {
174
+ meshExpression(path.get("right"), internal);
175
+ }
176
+ }
177
+ else {
178
+ meshExpression(path.get("right"), internal);
179
+ }
180
+ break;
181
+ }
182
+ case "MemberExpression":
183
+ case "OptionalMemberExpression": {
184
+ const path = nodePath;
185
+ const node = path.node;
186
+ const property = path.node.property;
187
+ meshExpression(path.get("object"), internal);
188
+ if (t.isExpression(property) && !t.isIdentifier(property)) {
189
+ meshOrIgnoreExpression(path.get("property"), internal);
190
+ }
191
+ if (t.isIdentifier(node.object) && internal.stack.get(node.object.name) === 3 /* VariableState.ReactiveObject */) {
192
+ path.replaceWith(t.memberExpression(node, t.identifier("$")));
193
+ }
194
+ break;
195
+ }
196
+ case "BinaryExpression": {
197
+ const path = nodePath;
198
+ meshOrIgnoreExpression(path.get("left"), internal);
199
+ meshExpression(path.get("right"), internal);
200
+ break;
201
+ }
202
+ case "ConditionalExpression": {
203
+ const path = nodePath;
204
+ meshExpression(path.get("test"), internal);
205
+ meshExpression(path.get("consequent"), internal);
206
+ meshExpression(path.get("alternate"), internal);
207
+ break;
208
+ }
209
+ case "LogicalExpression": {
210
+ const path = nodePath;
211
+ meshExpression(path.get("left"), internal);
212
+ meshExpression(path.get("right"), internal);
213
+ break;
214
+ }
215
+ case "NewExpression": {
216
+ const path = nodePath;
217
+ meshOrIgnoreExpression(path.get("callee"), internal);
218
+ meshAllUnknown(path.get("arguments"), internal);
219
+ break;
220
+ }
221
+ case "SequenceExpression": {
222
+ const path = nodePath;
223
+ meshAllExpressions(path.get("expressions"), internal);
224
+ break;
225
+ }
226
+ case "ParenthesizedExpression": {
227
+ const path = nodePath;
228
+ meshExpression(path.get("expression"), internal);
229
+ break;
230
+ }
231
+ case "UnaryExpression": {
232
+ const path = nodePath;
233
+ meshExpression(path.get("argument"), internal);
234
+ break;
235
+ }
236
+ case "UpdateExpression": {
237
+ const path = nodePath;
238
+ meshExpression(path.get("argument"), internal);
239
+ break;
240
+ }
241
+ case "YieldExpression": {
242
+ const path = nodePath;
243
+ meshExpression(path.get("argument"), internal);
244
+ break;
245
+ }
246
+ case "AwaitExpression": {
247
+ const path = nodePath;
248
+ meshExpression(path.get("argument"), internal);
249
+ break;
250
+ }
251
+ case "TypeCastExpression": {
252
+ const path = nodePath;
253
+ meshExpression(path.get("expression"), internal);
254
+ break;
255
+ }
256
+ case "BindExpression": {
257
+ const path = nodePath;
258
+ meshExpression(path.get("callee"), internal);
259
+ meshExpression(path.get("object"), internal);
260
+ break;
261
+ }
262
+ case "PipelineTopicExpression": {
263
+ const path = nodePath;
264
+ meshExpression(path.get("expression"), internal);
265
+ break;
266
+ }
267
+ case "PipelineBareFunction": {
268
+ const path = nodePath;
269
+ meshExpression(path.get("callee"), internal);
270
+ break;
271
+ }
272
+ case "TSInstantiationExpression": {
273
+ const path = nodePath;
274
+ meshExpression(path.get("expression"), internal);
275
+ break;
276
+ }
277
+ case "TSAsExpression": {
278
+ const path = nodePath;
279
+ meshExpression(path.get("expression"), internal);
280
+ break;
281
+ }
282
+ case "TSSatisfiesExpression": {
283
+ const path = nodePath;
284
+ meshExpression(path.get("expression"), internal);
285
+ break;
286
+ }
287
+ case "TSTypeAssertion": {
288
+ const path = nodePath;
289
+ meshExpression(path.get("expression"), internal);
290
+ break;
291
+ }
292
+ case "ObjectExpression": {
293
+ const path = nodePath;
294
+ for (const propPath of path.get("properties")) {
295
+ const prop = propPath.node;
296
+ if (t.isObjectProperty(prop)) {
297
+ const path = propPath;
298
+ const valuePath = path.get("value");
299
+ if (valuePath instanceof Array) {
300
+ meshAllExpressions(valuePath, internal);
301
+ }
302
+ else {
303
+ meshOrIgnoreExpression(valuePath, internal);
304
+ }
305
+ }
306
+ else if (t.isObjectMethod(prop)) {
307
+ meshFunction(propPath, internal);
308
+ }
309
+ else {
310
+ meshAllUnknown([propPath], internal);
311
+ }
312
+ }
313
+ break;
314
+ }
315
+ case "FunctionExpression": {
316
+ meshFunction(nodePath, internal);
317
+ break;
318
+ }
319
+ case "ArrowFunctionExpression": {
320
+ meshFunction(nodePath, internal);
321
+ break;
322
+ }
323
+ case "JSXFragment": {
324
+ throw nodePath.buildCodeFrameError("Vasille: JSX fragment is not allowed here");
325
+ }
326
+ case "JSXElement": {
327
+ throw nodePath.buildCodeFrameError("Vasille: JSX element is not allowed here");
328
+ }
329
+ case "BigIntLiteral":
330
+ case "BooleanLiteral":
331
+ case "ClassExpression":
332
+ case "DecimalLiteral":
333
+ case "DoExpression":
334
+ case "Import":
335
+ case "ImportExpression":
336
+ case "MetaProperty":
337
+ case "ModuleExpression":
338
+ case "NullLiteral":
339
+ case "NumericLiteral":
340
+ case "PipelinePrimaryTopicReference":
341
+ case "RecordExpression":
342
+ case "RegExpLiteral":
343
+ case "StringLiteral":
344
+ case "Super":
345
+ case "TSNonNullExpression":
346
+ case "ThisExpression":
347
+ case "TopicReference":
348
+ }
349
+ }
350
+ function meshBody(path, internal) {
351
+ if (t.isExpression(path.node)) {
352
+ meshExpression(path, internal);
353
+ }
354
+ else {
355
+ for (const statementPath of path.get("body")) {
356
+ meshStatement(statementPath, internal);
357
+ }
358
+ }
359
+ }
360
+ function meshStatements(paths, internal) {
361
+ for (const path of paths) {
362
+ meshStatement(path, internal);
363
+ }
364
+ }
365
+ function ignoreParams(val, internal) {
366
+ if (t.isAssignmentPattern(val)) {
367
+ val = val.left;
368
+ }
369
+ if (t.isIdentifier(val)) {
370
+ internal.stack.set(val.name, 1 /* VariableState.Ignored */);
371
+ }
372
+ else if (t.isObjectPattern(val)) {
373
+ for (const prop of val.properties) {
374
+ if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
375
+ internal.stack.set(prop.value.name, 1 /* VariableState.Ignored */);
376
+ }
377
+ else if (t.isRestElement(prop) && t.isIdentifier(prop.argument)) {
378
+ internal.stack.set(prop.argument.name, 1 /* VariableState.Ignored */);
379
+ }
380
+ }
381
+ }
382
+ else if (t.isArrayPattern(val)) {
383
+ for (const element of val.elements) {
384
+ if (element) {
385
+ ignoreParams(element, internal);
386
+ }
387
+ }
388
+ }
389
+ }
390
+ function reactiveArrayPattern(expr, internal) {
391
+ if (t.isArrayPattern(expr)) {
392
+ for (const element of expr.elements) {
393
+ if (t.isIdentifier(element)) {
394
+ internal.stack.set(element.name, 2 /* VariableState.Reactive */);
395
+ }
396
+ }
397
+ }
398
+ else if (t.isIdentifier(expr)) {
399
+ internal.stack.set(expr.name, 3 /* VariableState.ReactiveObject */);
400
+ }
401
+ else {
402
+ return false;
403
+ }
404
+ return true;
405
+ }
406
+ function meshStatement(path, internal) {
407
+ const statement = path.node;
408
+ if (!statement) {
409
+ return;
410
+ }
411
+ switch (statement.type) {
412
+ case "BlockStatement":
413
+ internal.stack.push();
414
+ meshStatements(path.get("body"), internal);
415
+ internal.stack.pop();
416
+ break;
417
+ case "DoWhileStatement": {
418
+ const _path = path;
419
+ meshExpression(_path.get("test"), internal);
420
+ internal.stack.push();
421
+ meshStatement(_path.get("body"), internal);
422
+ internal.stack.pop();
423
+ break;
424
+ }
425
+ case "ExpressionStatement":
426
+ meshExpression(path.get("expression"), internal);
427
+ break;
428
+ case "ForInStatement": {
429
+ const _path = path;
430
+ const left = _path.node.left;
431
+ internal.stack.push();
432
+ meshExpression(_path.get("right"), internal);
433
+ if (t.isVariableDeclaration(left) && t.isVariableDeclarator(left.declarations[0])) {
434
+ ignoreParams(left.declarations[0].id, internal);
435
+ }
436
+ meshStatement(_path.get("body"), internal);
437
+ internal.stack.pop();
438
+ break;
439
+ }
440
+ case "ForOfStatement": {
441
+ const _path = path;
442
+ const left = _path.node.left;
443
+ internal.stack.push();
444
+ meshExpression(_path.get("right"), internal);
445
+ if (t.isVariableDeclaration(left) && t.isVariableDeclarator(left.declarations[0])) {
446
+ ignoreParams(left.declarations[0].id, internal);
447
+ }
448
+ meshStatement(_path.get("body"), internal);
449
+ internal.stack.pop();
450
+ break;
451
+ }
452
+ case "ForStatement": {
453
+ const _path = path;
454
+ const node = _path.node;
455
+ internal.stack.push();
456
+ if (node.init) {
457
+ if (t.isExpression(node.init)) {
458
+ meshExpression(_path.get("init"), internal);
459
+ }
460
+ else {
461
+ const variablePath = _path.get("init");
462
+ for (const declarationPath of variablePath.get("declarations")) {
463
+ meshExpression(declarationPath.get("init"), internal);
464
+ ignoreParams(declarationPath.node.id, internal);
465
+ }
466
+ }
467
+ }
468
+ meshExpression(_path.get("test"), internal);
469
+ meshExpression(_path.get("update"), internal);
470
+ meshStatement(_path.get("body"), internal);
471
+ internal.stack.pop();
472
+ break;
473
+ }
474
+ case "FunctionDeclaration":
475
+ meshFunction(path, internal);
476
+ break;
477
+ case "IfStatement": {
478
+ const _path = path;
479
+ meshExpression(_path.get("test"), internal);
480
+ internal.stack.push();
481
+ meshStatement(_path.get("consequent"), internal);
482
+ internal.stack.pop();
483
+ internal.stack.push();
484
+ meshStatement(_path.get("alternate"), internal);
485
+ internal.stack.pop();
486
+ break;
487
+ }
488
+ case "LabeledStatement":
489
+ meshStatement(path.get("body"), internal);
490
+ break;
491
+ case "ReturnStatement":
492
+ meshExpression(path.get("argument"), internal);
493
+ break;
494
+ case "SwitchStatement": {
495
+ const _path = path;
496
+ meshExpression(_path.get("discriminant"), internal);
497
+ internal.stack.push();
498
+ for (const _case of _path.get("cases")) {
499
+ meshExpression(_case.get("test"), internal);
500
+ meshStatements(_case.get("consequent"), internal);
501
+ }
502
+ internal.stack.pop();
503
+ break;
504
+ }
505
+ case "ThrowStatement":
506
+ meshExpression(path.get("argument"), internal);
507
+ break;
508
+ case "TryStatement":
509
+ meshStatement(path.get("block"), internal);
510
+ break;
511
+ case "VariableDeclaration": {
512
+ const _path = path;
513
+ for (const declaration of _path.get("declarations")) {
514
+ const expr = declaration.node.init;
515
+ let ignore = true;
516
+ if (expr && t.isIdentifier(declaration.node.id) && (0, call_js_1.calls)(expr, ["compose", "extend"], internal)) {
517
+ meshComposeCall(expr, declaration.node.id, declaration.get("init"), internal);
518
+ }
519
+ else {
520
+ meshExpression(declaration.get("init"), internal);
521
+ if (ignore) {
522
+ ignoreParams(declaration.node.id, internal);
523
+ }
524
+ }
525
+ }
526
+ break;
527
+ }
528
+ case "WhileStatement": {
529
+ const _path = path;
530
+ meshExpression(_path.get("test"), internal);
531
+ internal.stack.push();
532
+ meshStatement(_path.get("body"), internal);
533
+ internal.stack.pop();
534
+ break;
535
+ }
536
+ case "WithStatement": {
537
+ const _path = path;
538
+ meshExpression(_path.get("object"), internal);
539
+ internal.stack.push();
540
+ meshStatement(_path.get("body"), internal);
541
+ internal.stack.pop();
542
+ break;
543
+ }
544
+ case "ExportNamedDeclaration": {
545
+ meshStatement(path.get("declaration"), internal);
546
+ break;
547
+ }
548
+ // Ignored
549
+ case "ExportDefaultDeclaration":
550
+ case "ExportAllDeclaration":
551
+ case "BreakStatement":
552
+ case "ContinueStatement":
553
+ case "DebuggerStatement":
554
+ case "EmptyStatement":
555
+ case "ClassDeclaration":
556
+ case "ImportDeclaration":
557
+ case "DeclareClass":
558
+ case "DeclareFunction":
559
+ case "DeclareInterface":
560
+ case "DeclareModule":
561
+ case "DeclareModuleExports":
562
+ case "DeclareTypeAlias":
563
+ case "DeclareOpaqueType":
564
+ case "DeclareVariable":
565
+ case "DeclareExportDeclaration":
566
+ case "DeclareExportAllDeclaration":
567
+ case "InterfaceDeclaration":
568
+ case "OpaqueType":
569
+ case "TypeAlias":
570
+ case "EnumDeclaration":
571
+ case "TSDeclareFunction":
572
+ case "TSInterfaceDeclaration":
573
+ case "TSTypeAliasDeclaration":
574
+ case "TSEnumDeclaration":
575
+ case "TSModuleDeclaration":
576
+ case "TSImportEqualsDeclaration":
577
+ case "TSExportAssignment":
578
+ case "TSNamespaceExportDeclaration":
579
+ }
580
+ }
581
+ function meshFunction(path, internal) {
582
+ if (t.isFunctionDeclaration(path.node) && path.node.id) {
583
+ internal.stack.set(path.node.id.name, 1 /* VariableState.Ignored */);
584
+ }
585
+ internal.stack.push();
586
+ const node = path.node;
587
+ if (t.isFunctionExpression(node) && node.id) {
588
+ internal.stack.set(node.id.name, 1 /* VariableState.Ignored */);
589
+ }
590
+ for (const param of node.params) {
591
+ ignoreParams(param, internal);
592
+ }
593
+ if (t.isExpression(node.body)) {
594
+ meshExpression(path.get("body"), internal);
595
+ }
596
+ else {
597
+ const bodyPath = path.get("body");
598
+ meshStatement(bodyPath, internal);
599
+ }
600
+ internal.stack.pop();
601
+ }
602
+ function composeExpression(path, internal) {
603
+ const expr = path.node;
604
+ if (!expr) {
605
+ return;
606
+ }
607
+ switch (expr.type) {
608
+ case "AssignmentExpression": {
609
+ const assign = expr;
610
+ if ((0, call_js_1.calls)(assign.right, ["awaited"], internal)) {
611
+ reactiveArrayPattern(assign.left, internal);
612
+ }
613
+ else {
614
+ meshExpression(path, internal);
615
+ }
616
+ break;
617
+ }
618
+ case "CallExpression":
619
+ case "OptionalCallExpression": {
620
+ const call = expr;
621
+ let replaced = false;
622
+ if ((0, call_js_1.calls)(call, ["watch"], internal)) {
623
+ const args = (0, lib_js_1.parseCalculateCall)(path, internal);
624
+ if (args) {
625
+ path.replaceWith(t.callExpression(t.memberExpression(internal_js_1.ctx, t.identifier("watch")), args));
626
+ replaced = true;
627
+ }
628
+ }
629
+ else if ((0, call_js_1.calls)(call, ["arrayModel"], internal)) {
630
+ const value = call.arguments[0];
631
+ if (t.isArrayExpression(value)) {
632
+ path.replaceWith((0, lib_js_1.arrayModel)(value, internal));
633
+ replaced = true;
634
+ }
635
+ else {
636
+ path.buildCodeFrameError(`Vasille: arrayModel requires array expression as argument`);
637
+ }
638
+ }
639
+ else if ((0, call_js_1.calls)(call, ["mapModel", "setModel"], internal)) {
640
+ const args = call.arguments;
641
+ const name = (0, call_js_1.calls)(call, ["mapModel", "setModel"], internal);
642
+ path.replaceWith(name === "mapModel" ? (0, lib_js_1.mapModel)(args, internal) : (0, lib_js_1.setModel)(args, internal));
643
+ replaced = true;
644
+ }
645
+ if (!replaced) {
646
+ meshExpression(path, internal);
647
+ }
648
+ break;
649
+ }
650
+ case "JSXElement":
651
+ case "JSXFragment":
652
+ path.replaceWithMultiple((0, jsx_js_1.transformJsx)(path, internal));
653
+ break;
654
+ default:
655
+ meshExpression(path, internal);
656
+ }
657
+ }
658
+ function composeStatements(paths, internal) {
659
+ for (const path of paths) {
660
+ composeStatement(path, internal);
661
+ }
662
+ }
663
+ function composeStatement(path, internal) {
664
+ const statement = path.node;
665
+ if (!statement) {
666
+ return;
667
+ }
668
+ switch (statement.type) {
669
+ case "FunctionDeclaration": {
670
+ const _path = path;
671
+ const fn = _path.node;
672
+ if ((0, jsx_detect_js_1.bodyHasJsx)(fn.body)) {
673
+ compose(_path, internal, false);
674
+ }
675
+ else {
676
+ meshFunction(_path, internal);
677
+ }
678
+ break;
679
+ }
680
+ case "BlockStatement": {
681
+ internal.stack.push();
682
+ composeStatements(path.get("body"), internal);
683
+ internal.stack.pop();
684
+ break;
685
+ }
686
+ case "DoWhileStatement": {
687
+ const _path = path;
688
+ meshExpression(_path.get("test"), internal);
689
+ internal.stack.push();
690
+ composeStatement(_path.get("body"), internal);
691
+ internal.stack.pop();
692
+ break;
693
+ }
694
+ case "ExpressionStatement": {
695
+ composeExpression(path.get("expression"), internal);
696
+ break;
697
+ }
698
+ case "ForInStatement": {
699
+ const _path = path;
700
+ const left = _path.node.left;
701
+ internal.stack.push();
702
+ meshExpression(_path.get("right"), internal);
703
+ if (t.isVariableDeclaration(left) && t.isVariableDeclarator(left.declarations[0])) {
704
+ ignoreParams(left.declarations[0].id, internal);
705
+ }
706
+ composeStatement(_path.get("body"), internal);
707
+ internal.stack.pop();
708
+ break;
709
+ }
710
+ case "ForStatement": {
711
+ const _path = path;
712
+ const node = _path.node;
713
+ internal.stack.push();
714
+ if (node.init) {
715
+ if (t.isExpression(node.init)) {
716
+ meshExpression(_path.get("init"), internal);
717
+ }
718
+ else {
719
+ const variablePath = _path.get("init");
720
+ for (const declarationPath of variablePath.get("declarations")) {
721
+ meshExpression(declarationPath.get("init"), internal);
722
+ ignoreParams(declarationPath.node.id, internal);
723
+ }
724
+ }
725
+ }
726
+ meshExpression(_path.get("test"), internal);
727
+ meshExpression(_path.get("update"), internal);
728
+ composeStatement(_path.get("body"), internal);
729
+ internal.stack.pop();
730
+ break;
731
+ }
732
+ case "IfStatement": {
733
+ const _path = path;
734
+ meshExpression(_path.get("test"), internal);
735
+ internal.stack.push();
736
+ composeStatement(_path.get("consequent"), internal);
737
+ internal.stack.pop();
738
+ internal.stack.push();
739
+ composeStatement(_path.get("alternate"), internal);
740
+ internal.stack.pop();
741
+ break;
742
+ }
743
+ case "LabeledStatement":
744
+ composeStatement(path.get("body"), internal);
745
+ break;
746
+ case "ReturnStatement":
747
+ composeExpression(path.get("argument"), internal);
748
+ break;
749
+ case "SwitchStatement": {
750
+ const _path = path;
751
+ meshExpression(_path.get("discriminant"), internal);
752
+ internal.stack.push();
753
+ for (const _case of _path.get("cases")) {
754
+ meshExpression(_case.get("test"), internal);
755
+ composeStatements(_case.get("consequent"), internal);
756
+ }
757
+ internal.stack.pop();
758
+ break;
759
+ }
760
+ case "TryStatement":
761
+ composeStatement(path.get("block"), internal);
762
+ break;
763
+ case "VariableDeclaration": {
764
+ const _path = path;
765
+ const kind = _path.node.kind;
766
+ const declares = kind === "const" ? 1 /* VariableState.Ignored */ : 2 /* VariableState.Reactive */;
767
+ if (kind === "let" || kind === "var") {
768
+ _path.node.kind = "const";
769
+ }
770
+ for (const declaration of _path.get("declarations")) {
771
+ const id = declaration.node.id;
772
+ let meshInit = true;
773
+ ignoreParams(declaration.node.id, internal);
774
+ if ((0, call_js_1.calls)(declaration.node.init, ["awaited"], internal)) {
775
+ reactiveArrayPattern(declaration.node.id, internal);
776
+ meshAllUnknown(declaration.get("init").get("arguments"), internal);
777
+ meshInit = false;
778
+ }
779
+ else if (t.isIdentifier(id)) {
780
+ internal.stack.set(id.name, declares);
781
+ const init = declaration.node.init;
782
+ if ((0, call_js_1.calls)(init, ["value"], internal)) {
783
+ internal.stack.set(id.name, 1 /* VariableState.Ignored */);
784
+ declaration.get("init").replaceWith(init.arguments[0]);
785
+ _path.node.kind = kind;
786
+ }
787
+ else if ((0, call_js_1.calls)(init, ["bind"], internal)) {
788
+ const argument = init.arguments[0];
789
+ const replaceWith = declares === 2 /* VariableState.Reactive */
790
+ ? (0, lib_js_1.forwardOnlyExpr)(declaration.get("init"), argument, internal)
791
+ : (0, lib_js_1.exprCall)(declaration.get("init"), argument, internal);
792
+ let insertNode = replaceWith !== null && replaceWith !== void 0 ? replaceWith : (0, lib_js_1.ref)(t.isExpression(argument) ? argument : null);
793
+ if (declares === 2 /* VariableState.Reactive */) {
794
+ internal.stack.set(id.name, 4 /* VariableState.ReactivePointer */);
795
+ insertNode = (0, lib_js_1.own)(insertNode);
796
+ }
797
+ else {
798
+ internal.stack.set(id.name, 2 /* VariableState.Reactive */);
799
+ }
800
+ declaration.get("init").replaceWith(insertNode);
801
+ meshInit = !replaceWith;
802
+ }
803
+ else if ((0, call_js_1.calls)(init, ["ref"], internal)) {
804
+ const argument = init.arguments[0];
805
+ internal.stack.set(id.name, 2 /* VariableState.Reactive */);
806
+ declaration.get("init").replaceWith((0, lib_js_1.ref)(t.isExpression(argument) ? argument : null));
807
+ }
808
+ else if ((0, call_js_1.calls)(init, ["reactiveObject"], internal)) {
809
+ const value = init.arguments[0];
810
+ if (kind !== "const") {
811
+ declaration.buildCodeFrameError(`Vasille: Reactive objects must be must be declared as constants`);
812
+ }
813
+ if (t.isObjectExpression(value)) {
814
+ declaration.get("init").replaceWith((0, lib_js_1.reactiveObject)(value, internal));
815
+ internal.stack.set(id.name, 3 /* VariableState.ReactiveObject */);
816
+ }
817
+ else {
818
+ declaration.buildCodeFrameError(`Vasille: reactiveObject requires object expression as argument`);
819
+ }
820
+ }
821
+ else if ((0, call_js_1.calls)(init, ["arrayModel"], internal)) {
822
+ const value = init.arguments[0];
823
+ if (kind !== "const") {
824
+ declaration.buildCodeFrameError(`Vasille: Array models must be must be declared as constants`);
825
+ }
826
+ if (t.isArrayExpression(value)) {
827
+ declaration.get("init").replaceWith((0, lib_js_1.arrayModel)(value, internal));
828
+ }
829
+ else {
830
+ declaration.buildCodeFrameError(`Vasille: arrayModel requires array expression as argument`);
831
+ }
832
+ }
833
+ else if ((0, call_js_1.calls)(init, ["mapModel", "setModel"], internal)) {
834
+ const args = init.arguments;
835
+ const name = (0, call_js_1.calls)(init, ["mapModel", "setModel"], internal);
836
+ if (kind !== "const") {
837
+ declaration.buildCodeFrameError(`Vasille: ${name === "mapModel" ? "Map" : "Set"} models must be declared as constants`);
838
+ }
839
+ declaration
840
+ .get("init")
841
+ .replaceWith(name === "mapModel" ? (0, lib_js_1.mapModel)(args, internal) : (0, lib_js_1.setModel)(args, internal));
842
+ }
843
+ else if (t.isObjectExpression(init)) {
844
+ if (kind !== "const") {
845
+ declaration.buildCodeFrameError(`Vasille: Objects must be must be declared as constants`);
846
+ }
847
+ declaration.get("init").replaceWith((0, lib_js_1.reactiveObject)(init, internal));
848
+ internal.stack.set(id.name, 3 /* VariableState.ReactiveObject */);
849
+ }
850
+ else if (t.isArrayExpression(init)) {
851
+ if (kind !== "const") {
852
+ declaration.buildCodeFrameError(`Vasille: Arrays must be must be declared as constants`);
853
+ }
854
+ declaration.get("init").replaceWith((0, lib_js_1.arrayModel)(init, internal));
855
+ }
856
+ else if (t.isNewExpression(init) && t.isIdentifier(init.callee)) {
857
+ if (init.callee.name === "Map" || init.callee.name === "Set") {
858
+ if (kind !== "const") {
859
+ declaration.buildCodeFrameError(`Vasille: ${init.callee.name === "Map" ? "Maps" : "Sets"} must be declared as constants`);
860
+ }
861
+ declaration
862
+ .get("init")
863
+ .replaceWith(init.callee.name === "Map" ? (0, lib_js_1.mapModel)(init.arguments, internal) : (0, lib_js_1.setModel)(init.arguments, internal));
864
+ }
865
+ }
866
+ else if (declares === 2 /* VariableState.Reactive */) {
867
+ const replaceWith = (0, lib_js_1.forwardOnlyExpr)(declaration.get("init"), declaration.node.init, internal);
868
+ meshInit = !replaceWith;
869
+ internal.stack.set(id.name, replaceWith ? 4 /* VariableState.ReactivePointer */ : 2 /* VariableState.Reactive */);
870
+ declaration.get("init").replaceWith(replaceWith ? (0, lib_js_1.own)(replaceWith) : (0, lib_js_1.ref)(declaration.node.init));
871
+ }
872
+ else {
873
+ const replaceWith = (0, lib_js_1.exprCall)(declaration.get("init"), declaration.node.init, internal);
874
+ if (replaceWith) {
875
+ declaration.get("init").replaceWith(replaceWith);
876
+ }
877
+ internal.stack.set(id.name, replaceWith ? 2 /* VariableState.Reactive */ : 1 /* VariableState.Ignored */);
878
+ meshInit = !replaceWith;
879
+ }
880
+ }
881
+ if (meshInit) {
882
+ meshExpression(declaration.get("init"), internal);
883
+ }
884
+ }
885
+ break;
886
+ }
887
+ case "WhileStatement": {
888
+ const _path = path;
889
+ meshExpression(_path.get("test"), internal);
890
+ internal.stack.push();
891
+ composeStatement(_path.get("body"), internal);
892
+ internal.stack.pop();
893
+ break;
894
+ }
895
+ case "WithStatement": {
896
+ throw path.buildCodeFrameError("Vasille: Usage of 'with' in components is restricted");
897
+ }
898
+ case "ForOfStatement": {
899
+ const _path = path;
900
+ const left = _path.node.left;
901
+ internal.stack.push();
902
+ meshExpression(_path.get("right"), internal);
903
+ if (t.isVariableDeclaration(left) && t.isVariableDeclarator(left.declarations[0])) {
904
+ ignoreParams(left.declarations[0].id, internal);
905
+ }
906
+ composeStatement(_path.get("body"), internal);
907
+ internal.stack.pop();
908
+ break;
909
+ }
910
+ default:
911
+ meshStatement(path, internal);
912
+ }
913
+ }
914
+ function compose(path, internal, isInternalSlot) {
915
+ if (t.isFunctionDeclaration(path.node) && path.node.id) {
916
+ internal.stack.set(path.node.id.name, 1 /* VariableState.Ignored */);
917
+ }
918
+ internal.stack.push();
919
+ const node = path.node;
920
+ const params = node.params;
921
+ const body = node.body;
922
+ if (t.isFunctionExpression(node) && node.id) {
923
+ internal.stack.set(node.id.name, 1 /* VariableState.Ignored */);
924
+ }
925
+ if (params.length > 1) {
926
+ throw path.get("params")[1].buildCodeFrameError("Vasille: JSX compoent must have no more then 1 parameter");
927
+ }
928
+ for (const param of params) {
929
+ const target = t.isAssignmentPattern(param) ? param.left : param;
930
+ if (t.isIdentifier(target)) {
931
+ internal.stack.set(target.name, isInternalSlot ? 1 /* VariableState.Ignored */ : 3 /* VariableState.ReactiveObject */);
932
+ }
933
+ else if (t.isObjectPattern(target)) {
934
+ for (const prop of target.properties) {
935
+ if (t.isObjectProperty(prop)) {
936
+ if (t.isIdentifier(prop.value)) {
937
+ internal.stack.set(prop.value.name, isInternalSlot ? 1 /* VariableState.Ignored */ : 2 /* VariableState.Reactive */);
938
+ }
939
+ else if (t.isIdentifier(prop.key)) {
940
+ internal.stack.set(prop.key.name, isInternalSlot ? 1 /* VariableState.Ignored */ : 2 /* VariableState.Reactive */);
941
+ }
942
+ }
943
+ else if (t.isRestElement(prop) && t.isIdentifier(prop.argument)) {
944
+ internal.stack.set(prop.argument.name, isInternalSlot ? 1 /* VariableState.Ignored */ : 3 /* VariableState.ReactiveObject */);
945
+ }
946
+ }
947
+ }
948
+ else {
949
+ throw path.get("params")[0].buildCodeFrameError("Vasille: Parameter must be an identifier of object pattern");
950
+ }
951
+ }
952
+ for (const param of path.get("params")) {
953
+ if (t.isObjectPattern(param.node)) {
954
+ for (const prop of param.get("properties")) {
955
+ if (t.isObjectProperty(prop.node) && t.isAssignmentPattern(prop.node.value)) {
956
+ const assignPath = prop.get("value");
957
+ assignPath
958
+ .get("right")
959
+ .replaceWith(t.callExpression(t.memberExpression(internal.id, t.identifier("r")), [assignPath.node.right]));
960
+ }
961
+ }
962
+ }
963
+ }
964
+ if (t.isExpression(body)) {
965
+ composeExpression(path.get("body"), internal);
966
+ }
967
+ else if (t.isBlockStatement(body)) {
968
+ composeStatement(path.get("body"), internal);
969
+ }
970
+ internal.stack.pop();
971
+ }