babel-plugin-vasille 3.1.5 → 4.0.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,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkOrder = checkOrder;
4
+ const call_1 = require("./call");
5
+ const lib_1 = require("./lib");
6
+ const componentOrder = [
7
+ { nodeType: ["VariableDeclaration"] },
8
+ { calls: ["watch"] },
9
+ { nodeType: ["FunctionDeclaration"] },
10
+ { calls: ["beforeMount"] },
11
+ { nodeType: ["JSXElement", "JSXFragment"] },
12
+ { calls: ["afterMount"] },
13
+ { calls: ["beforeDestroy"] },
14
+ { nodeType: ["ReturnStatement"] },
15
+ ];
16
+ function matchStatement(path, target, internal) {
17
+ const testPath = path.isExpressionStatement() ? path.get("expression") : path;
18
+ if (target.nodeType && testPath.node) {
19
+ return target.nodeType.includes(testPath.node.type);
20
+ }
21
+ if (target.calls && testPath.isExpression()) {
22
+ return (0, call_1.calls)(testPath, target.calls, internal);
23
+ }
24
+ return false;
25
+ }
26
+ function checkOrder(paths, internal) {
27
+ let index = 0;
28
+ for (const path of paths) {
29
+ while (index < componentOrder.length && !matchStatement(path, componentOrder[index], internal)) {
30
+ index++;
31
+ }
32
+ if (index === componentOrder.length) {
33
+ (0, lib_1.err)(lib_1.Errors.RulesOfVasille, path, [
34
+ "Malformed component detected, required component structure is:",
35
+ "1. Variable declarations.",
36
+ "2. Watchers/effects (calls of `watch` function).",
37
+ "3. Function declarations.",
38
+ "4. Before mount hint (call of `beforeMount` function)",
39
+ "5. JSX elements and fragments",
40
+ "6. After mount hint (call of `afterMount` function)",
41
+ "7. Before destroy hint (call of `beforeDestroy` function)",
42
+ "8. Return statement",
43
+ "All steps are optional, but the order is strict.",
44
+ "Unlisted statements are not accepted.",
45
+ ].join("\n"), internal);
46
+ }
47
+ }
48
+ }
package/lib/router.js ADDED
@@ -0,0 +1,41 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.routerReplace = routerReplace;
37
+ const t = __importStar(require("@babel/types"));
38
+ const internal_1 = require("./internal");
39
+ function routerReplace(path) {
40
+ path.replaceWith(t.memberExpression(t.memberExpression(internal_1.ctx, t.identifier("runner")), t.identifier("router")));
41
+ }
@@ -33,23 +33,14 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.trProgram = trProgram;
36
+ exports.transformProgram = transformProgram;
37
37
  const t = __importStar(require("@babel/types"));
38
38
  const internal_js_1 = require("./internal.js");
39
39
  const mesh_js_1 = require("./mesh.js");
40
40
  const css_transformer_js_1 = require("./css-transformer.js");
41
41
  const imports = new Map([["vasille-web", "VasilleWeb"]]);
42
42
  const ignoreMembers = new Set([
43
- "value",
44
- "ref",
45
- "bind",
46
- "calculate",
47
- "watch",
48
- "forward",
49
- "arrayModel",
50
- "mapModel",
51
- "reactiveObject",
52
- "setModel",
43
+ "raw",
53
44
  "theme",
54
45
  "dark",
55
46
  "mobile",
@@ -57,88 +48,143 @@ const ignoreMembers = new Set([
57
48
  "laptop",
58
49
  "prefersDark",
59
50
  "prefersLight",
51
+ "bridge",
52
+ "router",
53
+ "beforeMount",
54
+ "afterMount",
55
+ "beforeDestroy",
56
+ "If",
57
+ "ElseIf",
58
+ "Else",
60
59
  ]);
61
60
  function extractText(node) {
62
61
  // no case found for string literal
63
62
  return node.name;
64
63
  }
65
- function trProgram(path, devMode) {
66
- let id;
67
- let stylesConnected = false;
64
+ // Handles import declarations and updates internal state
65
+ function handleImportDeclaration(statementPath, internal, ids, used, stylesConnected) {
66
+ const statement = statementPath.node;
67
+ const name = imports.get(statement.source.value);
68
+ if (!name)
69
+ return;
70
+ internal.prefix = name;
71
+ for (const specifier of statement.specifiers) {
72
+ /* istanbul ignore else */
73
+ if (t.isImportNamespaceSpecifier(specifier)) {
74
+ internal.global = specifier.local.name;
75
+ stylesConnected.value = true;
76
+ }
77
+ else if (t.isImportSpecifier(specifier)) {
78
+ const imported = extractText(specifier.imported);
79
+ const local = specifier.local.name;
80
+ if (imported === "bind" || imported === "calculate" || imported === "watch") {
81
+ ids.expr = local;
82
+ }
83
+ if (imported in ids) {
84
+ ids[imported] = local;
85
+ }
86
+ internal.mapping.set(local, imported);
87
+ if (imported === "styleSheet") {
88
+ stylesConnected.value = true;
89
+ }
90
+ internal.importStatement = statementPath;
91
+ }
92
+ }
93
+ statement.specifiers = statement.specifiers.filter(spec => {
94
+ if (!t.isImportSpecifier(spec))
95
+ return true;
96
+ return !(ignoreMembers.has(extractText(spec.imported)) ||
97
+ (!internal.devMode && extractText(spec.imported) === "Debug"));
98
+ });
99
+ }
100
+ // Handles mesh and style transformation
101
+ function handleStatement(statementPath, internal, stylesConnected) {
102
+ if (!stylesConnected || !(0, css_transformer_js_1.findStyleInNode)(statementPath, internal)) {
103
+ (0, mesh_js_1.meshStatement)(statementPath, internal);
104
+ }
105
+ }
106
+ // Handles import insertion and cleanup
107
+ function updateImports(path, internal, ids, used) {
108
+ if (used.size > 0 && !internal.importStatement && !internal.global) {
109
+ path.get("body")[0].insertBefore(t.importDeclaration([...used].map(name => t.importSpecifier(t.identifier(ids[name]), t.identifier(name))), t.stringLiteral("vasille-web")));
110
+ }
111
+ if (used.size > 0 && !internal.global && internal.importStatement) {
112
+ const statementPath = internal.importStatement;
113
+ const statement = statementPath.node;
114
+ const specifiers = statement.specifiers.filter(item => {
115
+ /* istanbul ignore else */
116
+ if (t.isImportSpecifier(item) && t.isIdentifier(item.local)) {
117
+ return statementPath.scope.bindings[item.local.name].referenced;
118
+ }
119
+ });
120
+ for (const name of used) {
121
+ if (!specifiers.find(specifier => t.isImportSpecifier(specifier) && [name, ids[name]].includes(extractText(specifier.imported)))) {
122
+ specifiers.push(t.importSpecifier(t.identifier(ids[name]), t.identifier(name)));
123
+ }
124
+ }
125
+ statement.specifiers = specifiers;
126
+ }
127
+ }
128
+ // Main transformer function
129
+ function transformProgram(path, filename, opts) {
130
+ const stylesConnected = { value: false };
131
+ const used = new Set();
132
+ const ids = {
133
+ ref: "VasilleRef",
134
+ expr: "VasilleExpr",
135
+ forward: "VasilleForward",
136
+ backward: "VasilleBackward",
137
+ setModel: "VasilleSetModel",
138
+ mapModel: "VasilleMapModel",
139
+ arrayModel: "VasilleArrayModel",
140
+ ensure: "VasilleEnsure",
141
+ match: "VasilleMatch",
142
+ set: "VasilleSet",
143
+ Switch: "VasilleSwitch",
144
+ };
145
+ function call(key, args) {
146
+ used.add(key);
147
+ if (internal.global) {
148
+ return t.callExpression(t.memberExpression(t.identifier(internal.global), t.identifier(key)), args);
149
+ }
150
+ return t.callExpression(t.identifier(ids[key]), args);
151
+ }
68
152
  const internal = {
69
- get id() {
70
- this.internalUsed = true;
71
- return id;
72
- },
73
- set id(expr) {
74
- id = expr;
75
- },
76
153
  stack: new internal_js_1.StackedStates(),
77
154
  mapping: new Map(),
78
155
  global: "",
79
156
  prefix: "Vasille_",
80
157
  importStatement: null,
81
- internalUsed: false,
82
158
  stateOnly: false,
83
- devMode: devMode,
159
+ filename,
160
+ devMode: opts.devMode,
161
+ strictFolders: opts.strictFolders,
162
+ ref: arg => call("ref", arg ? [arg] : []),
163
+ expr: (func, values) => call("expr", [getCtx(), func, values]),
164
+ forward: arg => call("forward", [getCtx(), arg]),
165
+ setModel: arg => call("setModel", arg ? [getCtx(), arg] : [getCtx()]),
166
+ mapModel: arg => call("mapModel", arg ? [getCtx(), arg] : [getCtx()]),
167
+ arrayModel: arg => call("arrayModel", arg ? [getCtx(), arg] : [getCtx()]),
168
+ ensure: arg => call("ensure", [arg]),
169
+ match: (name, arg) => call("match", arg ? [name, arg] : [name]),
170
+ set: (obj, field, value) => call("set", [obj, field, value]),
171
+ Switch: arg => call("Switch", [arg, internal_js_1.ctx]),
84
172
  };
173
+ function getCtx() {
174
+ if (internal.isComposing)
175
+ return internal_js_1.ctx;
176
+ return t.nullLiteral();
177
+ }
85
178
  for (const statementPath of path.get("body")) {
86
179
  const statement = statementPath.node;
87
180
  if (t.isImportDeclaration(statement)) {
88
- const name = imports.get(statement.source.value);
89
- if (name) {
90
- internal.prefix = name;
91
- for (const specifier of statement.specifiers) {
92
- if (t.isImportNamespaceSpecifier(specifier)) {
93
- internal.global = specifier.local.name;
94
- if (statement.source.value === "vasille-web") {
95
- stylesConnected = true;
96
- }
97
- internal.id = t.memberExpression(t.identifier(internal.global), t.identifier("$"));
98
- }
99
- else if (t.isImportSpecifier(specifier)) {
100
- const imported = extractText(specifier.imported);
101
- const local = specifier.local.name;
102
- internal.mapping.set(local, imported);
103
- if (imported === "styleSheet") {
104
- stylesConnected = true;
105
- }
106
- if (!id) {
107
- internal.id = t.identifier(name);
108
- }
109
- internal.importStatement = statementPath;
110
- }
111
- }
112
- statement.specifiers = statement.specifiers.filter(spec => {
113
- if (!t.isImportSpecifier(spec)) {
114
- return true;
115
- }
116
- else {
117
- return !(ignoreMembers.has(extractText(spec.imported)) ||
118
- (!internal.devMode && extractText(spec.imported) === "Debug"));
119
- }
120
- });
121
- }
181
+ handleImportDeclaration(statementPath, internal, ids, used, stylesConnected);
122
182
  }
123
183
  else {
124
- if (!id) {
125
- return;
126
- }
127
- else if (!stylesConnected || !(0, css_transformer_js_1.findStyleInNode)(statementPath, internal)) {
128
- (0, mesh_js_1.meshStatement)(statementPath, internal);
129
- }
184
+ handleStatement(statementPath, internal, stylesConnected.value);
130
185
  }
131
186
  }
132
- if (internal.internalUsed && !internal.global && internal.importStatement) {
133
- const statementPath = internal.importStatement;
134
- const statement = statementPath.node;
135
- statementPath.replaceWith(t.importDeclaration([
136
- ...statement.specifiers.filter(item => {
137
- if (t.isImportSpecifier(item) && t.isIdentifier(item.local)) {
138
- return statementPath.scope.bindings[item.local.name].referenced;
139
- }
140
- }),
141
- t.importSpecifier(internal.id, t.identifier("$")),
142
- ], statement.source));
143
- }
187
+ updateImports(path, internal, ids, used);
188
+ if (internal.firstError)
189
+ throw internal.firstError;
144
190
  }
@@ -33,45 +33,18 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.ctx = exports.StackedStates = exports.VariableScope = void 0;
36
+ exports.stringify = stringify;
37
37
  const t = __importStar(require("@babel/types"));
38
- var VariableScope;
39
- (function (VariableScope) {
40
- VariableScope[VariableScope["Any"] = 0] = "Any";
41
- VariableScope[VariableScope["Local"] = 1] = "Local";
42
- VariableScope[VariableScope["Global"] = 2] = "Global";
43
- })(VariableScope || (exports.VariableScope = VariableScope = {}));
44
- class StackedStates {
45
- constructor() {
46
- this.maps = [];
47
- this.localIndex = -1;
48
- this.push();
38
+ function stringify(node) {
39
+ let name = "";
40
+ if (t.isStringLiteral(node)) {
41
+ name = node.value;
49
42
  }
50
- fixLocalIndex() {
51
- this.localIndex = this.maps.length;
43
+ if (t.isPrivateName(node)) {
44
+ name = node.id.name;
52
45
  }
53
- resetLocalIndex() {
54
- this.localIndex = -1;
55
- }
56
- push() {
57
- this.maps.push(new Map());
58
- }
59
- pop() {
60
- this.maps.pop();
61
- }
62
- get(name, scope) {
63
- for (let i = (this.localIndex === -1 || scope !== VariableScope.Global
64
- ? this.maps.length
65
- : Math.min(this.maps.length, this.localIndex)) - 1; i >= (this.localIndex === -1 || scope !== VariableScope.Local ? 0 : this.localIndex); i--) {
66
- if (this.maps[i].has(name)) {
67
- return this.maps[i].get(name);
68
- }
69
- }
70
- return undefined;
71
- }
72
- set(name, state) {
73
- this.maps[this.maps.length - 1].set(name, state);
46
+ if (t.isIdentifier(node)) {
47
+ name = node.name;
74
48
  }
49
+ return name;
75
50
  }
76
- exports.StackedStates = StackedStates;
77
- exports.ctx = t.identifier("Vasille");
package/package.json CHANGED
@@ -1,20 +1,19 @@
1
1
  {
2
2
  "name": "babel-plugin-vasille",
3
- "version": "3.1.5",
3
+ "version": "4.0.0",
4
4
  "description": "Convert Vasille Meta Language code to pure JavaScript",
5
- "main": "lib-node/index.js",
5
+ "main": "lib/index.js",
6
6
  "type": "commonjs",
7
7
  "exports": {
8
- "import": "./lib-node/index.js",
8
+ "import": "./lib/index.js",
9
9
  "browser": "./lib/index.js",
10
- "node": "./lib-node/index.js",
11
- "require": "./lib-node/index.js"
10
+ "node": "./lib/index.js",
11
+ "require": "./lib/index.js"
12
12
  },
13
13
  "scripts": {
14
14
  "prepack": "cp -f ../README.md ./README.md",
15
- "prettier": "prettier src test/**/*.ts* --write",
15
+ "prettier": "prettier src test/**/*.ts* test/*.ts --write",
16
16
  "build": "tsc --build tsconfig-build.json",
17
- "build-node": "tsc --build tsconfig-build-node.json",
18
17
  "tsc-tests": "tsc --build test/tsconfig.json",
19
18
  "test": "jest",
20
19
  "test-coverage": "jest --coverage"
@@ -40,17 +39,16 @@
40
39
  "@types/babel__core": "^7.20.5",
41
40
  "@types/jest": "^30.0.0",
42
41
  "@types/jsdom": "^21.1.7",
43
- "@types/node": "^24.1.0",
42
+ "@types/node": "^24.2.1",
44
43
  "cross-env": "^10.0.0",
45
44
  "jest": "^30.0.5",
46
45
  "jsdom": "^26.1.0",
47
46
  "path": "^0.12.7",
48
47
  "prettier": "^3.6.2",
49
- "ts-jest": "^29.4.0",
50
- "typescript": "^5.8.3",
51
- "vasille": "^3.1.0",
52
- "vasille-css": "^3.1.0",
53
- "vasille-dx": "^3.1.3",
54
- "vasille-web": "^3.1.5"
48
+ "ts-jest": "^29.4.1",
49
+ "typescript": "^5.9.2",
50
+ "vasille": "^4.0.0",
51
+ "vasille-css": "^4.0.0",
52
+ "vasille-web": "^4.0.0"
55
53
  }
56
54
  }
package/lib-node/call.js DELETED
@@ -1,104 +0,0 @@
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 () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.requiresContext = exports.styleOnly = exports.composeOnly = void 0;
37
- exports.calls = calls;
38
- const t = __importStar(require("@babel/types"));
39
- const internal_js_1 = require("./internal.js");
40
- exports.composeOnly = [
41
- "forward",
42
- "watch",
43
- "ref",
44
- "bind",
45
- "value",
46
- "awaited",
47
- "arrayModel",
48
- "mapModel",
49
- "setModel",
50
- "reactiveObject",
51
- ];
52
- exports.styleOnly = [
53
- "theme",
54
- "dark",
55
- "mobile",
56
- "tablet",
57
- "laptop",
58
- "prefersDark",
59
- "prefersLight",
60
- "styleSheet",
61
- ];
62
- exports.requiresContext = ["awaited"];
63
- const requiresContextSet = new Set(exports.requiresContext);
64
- function calls(node, names, internal) {
65
- const set = new Set(names);
66
- const callee = t.isCallExpression(node) ? node.callee : null;
67
- if (callee) {
68
- if (t.isIdentifier(callee)) {
69
- const mapped = internal.mapping.get(callee.name);
70
- if (mapped && set.has(mapped) && internal.stack.get(callee.name) === undefined) {
71
- if (requiresContextSet.has(callee.name) && t.isCallExpression(node)) {
72
- node.arguments.unshift(internal_js_1.ctx);
73
- }
74
- if (mapped === "store") {
75
- internal.stateOnly = true;
76
- }
77
- if (mapped === "compose") {
78
- internal.stateOnly = false;
79
- }
80
- return mapped;
81
- }
82
- return false;
83
- }
84
- const global = internal.stack.get(internal.global) === undefined;
85
- let propName = null;
86
- if (t.isMemberExpression(callee)) {
87
- if (t.isIdentifier(callee.property)) {
88
- propName = callee.property.name;
89
- }
90
- else if (t.isStringLiteral(callee.property)) {
91
- propName = callee.property.value;
92
- }
93
- }
94
- if (t.isMemberExpression(callee) && t.isIdentifier(callee.object) && propName) {
95
- if (global && callee.object.name === internal.global && set.has(propName)) {
96
- if (requiresContextSet.has(propName) && t.isCallExpression(node)) {
97
- node.arguments.unshift(internal_js_1.ctx);
98
- }
99
- return callee.object.name;
100
- }
101
- }
102
- }
103
- return false;
104
- }