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,88 @@
1
+ import * as t from "@babel/types";
2
+ import { StackedStates } from "./internal.js";
3
+ import { meshStatement } from "./mesh.js";
4
+ const imports = new Map([["vasille-dx", "VasilleDX"], ["vasille-web", "VasilleWeb"]]);
5
+ const ignoreMembers = new Set([
6
+ "value",
7
+ "ref",
8
+ "bind",
9
+ "calculate",
10
+ "watch",
11
+ "arrayModel",
12
+ "mapModel",
13
+ "reactiveObject",
14
+ "setModel",
15
+ ]);
16
+ function extractText(node) {
17
+ return t.isIdentifier(node) ? node.name : node.value;
18
+ }
19
+ export function trProgram(path, devMode) {
20
+ let id;
21
+ const internal = {
22
+ get id() {
23
+ this.internalUsed = true;
24
+ return id;
25
+ },
26
+ set id(expr) {
27
+ id = expr;
28
+ },
29
+ stack: new StackedStates(),
30
+ mapping: new Map(),
31
+ global: "",
32
+ prefix: "Vasille_",
33
+ importStatement: null,
34
+ internalUsed: false,
35
+ devMode: devMode,
36
+ };
37
+ for (const statementPath of path.get("body")) {
38
+ const statement = statementPath.node;
39
+ if (t.isImportDeclaration(statement)) {
40
+ const name = imports.get(statement.source.value);
41
+ if (name) {
42
+ internal.prefix = `${name}_`;
43
+ for (const specifier of statement.specifiers) {
44
+ if (t.isImportNamespaceSpecifier(specifier)) {
45
+ internal.global = specifier.local.name;
46
+ id = t.memberExpression(t.identifier(internal.global), t.identifier("$"));
47
+ }
48
+ else if (t.isImportSpecifier(specifier)) {
49
+ const imported = extractText(specifier.imported);
50
+ const local = specifier.local.name;
51
+ internal.mapping.set(local, imported);
52
+ if (!id) {
53
+ id = t.identifier(name);
54
+ }
55
+ internal.importStatement = statementPath;
56
+ }
57
+ }
58
+ statement.specifiers = statement.specifiers.filter(spec => {
59
+ if (!t.isImportSpecifier(spec)) {
60
+ return true;
61
+ }
62
+ else {
63
+ return !ignoreMembers.has(extractText(spec.imported));
64
+ }
65
+ });
66
+ }
67
+ }
68
+ else {
69
+ if (!id) {
70
+ return;
71
+ }
72
+ meshStatement(statementPath, internal);
73
+ }
74
+ }
75
+ if (internal.internalUsed && !internal.global && internal.importStatement) {
76
+ const statementPath = internal.importStatement;
77
+ const statement = statementPath.node;
78
+ statementPath.replaceWith(t.importDeclaration([
79
+ ...statement.specifiers.filter(item => {
80
+ if (t.isImportSpecifier(item) && t.isIdentifier(item.local)) {
81
+ return statementPath.scope.bindings[item.local.name].referenced;
82
+ }
83
+ return true;
84
+ }),
85
+ t.importSpecifier(internal.id, t.identifier("$")),
86
+ ], statement.source));
87
+ }
88
+ }
@@ -0,0 +1,78 @@
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.requiresContext = exports.composeOnly = void 0;
27
+ exports.calls = calls;
28
+ const t = __importStar(require("@babel/types"));
29
+ const internal_js_1 = require("./internal.js");
30
+ exports.composeOnly = [
31
+ "forward",
32
+ "watch",
33
+ "ref",
34
+ "bind",
35
+ "value",
36
+ "arrayModel",
37
+ "mapModel",
38
+ "setModel",
39
+ "reactiveObject",
40
+ ];
41
+ exports.requiresContext = ["awaited", "forward"];
42
+ const requiresContextSet = new Set(exports.requiresContext);
43
+ function calls(node, names, internal) {
44
+ const set = new Set(names);
45
+ const callee = t.isCallExpression(node) ? node.callee : null;
46
+ if (callee) {
47
+ if (t.isIdentifier(callee)) {
48
+ const mapped = internal.mapping.get(callee.name);
49
+ if (mapped && set.has(mapped) && internal.stack.get(callee.name) === undefined) {
50
+ if (requiresContextSet.has(callee.name) && t.isCallExpression(node)) {
51
+ node.arguments.unshift(internal_js_1.ctx);
52
+ }
53
+ return mapped;
54
+ }
55
+ return false;
56
+ }
57
+ // The global object is overrided
58
+ if (internal.stack.get(internal.global) !== undefined) {
59
+ return false;
60
+ }
61
+ const propName = t.isMemberExpression(callee)
62
+ ? t.isIdentifier(callee.property)
63
+ ? callee.property.name
64
+ : t.isStringLiteral(callee.property)
65
+ ? callee.property.value
66
+ : null
67
+ : null;
68
+ if (t.isMemberExpression(callee) && t.isIdentifier(callee.object) && propName) {
69
+ if (callee.object.name === internal.global && set.has(propName)) {
70
+ if (requiresContextSet.has(callee.object.name) && t.isCallExpression(node)) {
71
+ node.arguments.unshift(internal_js_1.ctx);
72
+ }
73
+ return callee.object.name;
74
+ }
75
+ }
76
+ }
77
+ return false;
78
+ }