babel-plugin-vasille 3.1.1 → 3.2.1

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.
package/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  * [Installation](#installation)
12
12
  * [How to use Vasille](#how-to-use-vasille)
13
13
  * [How SAFE is Vasille](#how-safe-is-vasille)
14
- * [How SIMPLE is Vasille](#how-simple-is-vasille)
14
+ * [How INTUITIVE is Vasille](#how-intuitive-is-vasille)
15
15
  * [How POWERFUL is Vasille](#how-powerful-is-vasille)
16
16
  * [Road Map](#road-map)
17
17
 
@@ -44,6 +44,7 @@ $ npx degit vasille-js/example-javascript my-project
44
44
 
45
45
  ### Full documentation:
46
46
  * [Learn `Vasille` in 5 minutes](https://github.com/vasille-js/vasille-js/blob/v3/doc/V3-API.md)
47
+ * [Vasille Router Documentation](https://github.com/vasille-js/vasille-js/blob/v3/doc/Router-API.md)
47
48
 
48
49
  ### Examples
49
50
  * [TypeScript Example](https://github.com/vasille-js/example-typescript)
@@ -60,7 +61,7 @@ The safe of your application is ensured by
60
61
  * `strong typing` makes your javascript/typescript code safe as C++ code.
61
62
  All entities of `vasille` core library are strongly typed, including:
62
63
  * data fields & properties.
63
- * computed properties (function parameters & result).
64
+ * computed properties (function parameters and result).
64
65
  * methods.
65
66
  * events (defined handlers & event emit).
66
67
  * DOM events & DOM operation (attributing, styling, etc.).
@@ -68,7 +69,7 @@ All entities of `vasille` core library are strongly typed, including:
68
69
  * references to children.
69
70
  * No asynchronous code, when the line of code is executed, the DOM and reactive things are already synced.
70
71
 
71
- ## How SIMPLE is Vasille
72
+ ## How INTUITIVE is Vasille
72
73
 
73
74
  There is the "Hello World":
74
75
  ```typescript jsx
@@ -105,7 +106,7 @@ All of these are supported:
105
106
  * [x] Develop the `Vasille Babel Plugin`.
106
107
  * [x] `100%` Test Coverage fot babel plugin.
107
108
  * [x] Add CSS support (define styles in components).
108
- * [ ] Add router.
109
+ * [x] Add router.
109
110
  * [ ] Add SSR (server side rendering).
110
111
  * [ ] Develop tools extension for debugging.
111
112
 
package/lib/bridge.js ADDED
@@ -0,0 +1,173 @@
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.processBridgeCall = processBridgeCall;
37
+ const t = __importStar(require("@babel/types"));
38
+ const expression_1 = require("./expression");
39
+ const lib_1 = require("./lib");
40
+ const mesh_1 = require("./mesh");
41
+ const utils_1 = require("./utils");
42
+ const bridgeConstName = "bridge";
43
+ function processBridgeCall(path, internal, search) {
44
+ const node = path.node;
45
+ if (t.isCallExpression(node) && t.isMemberExpression(node.callee)) {
46
+ const callee = node.callee;
47
+ let propName = null;
48
+ if (t.isIdentifier(callee.object)) {
49
+ const mapped = internal.mapping.get(callee.object.name);
50
+ if (mapped && mapped === bridgeConstName && internal.stack.get(callee.object.name) === undefined) {
51
+ propName = (0, utils_1.stringify)(callee.property);
52
+ }
53
+ }
54
+ if (t.isMemberExpression(callee.object) && t.isIdentifier(callee.object.object)) {
55
+ const rootName = callee.object.object.name;
56
+ if (rootName === internal.global &&
57
+ internal.stack.get(rootName) === undefined &&
58
+ (0, utils_1.stringify)(callee.object.property) === bridgeConstName) {
59
+ propName = (0, utils_1.stringify)(callee.property);
60
+ }
61
+ }
62
+ if (propName) {
63
+ const getArg = (mesh = true) => {
64
+ if (node.arguments.length !== 1 || !t.isExpression(node.arguments[0])) {
65
+ throw path.buildCodeFrameError("Vasille: Expected 1 argument");
66
+ }
67
+ if (mesh) {
68
+ (0, mesh_1.meshExpression)(path.get("arguments")[0], internal);
69
+ }
70
+ return node.arguments[0];
71
+ };
72
+ const callWithArg = (name) => {
73
+ path.replaceWith(t.callExpression(t.memberExpression(internal.id, t.identifier(name)), [getArg()]));
74
+ };
75
+ const callWithOptionalArg = (name) => {
76
+ if (node.arguments.length === 0) {
77
+ path.replaceWith(t.callExpression(t.memberExpression(internal.id, t.identifier(name)), []));
78
+ }
79
+ else {
80
+ callWithArg(name);
81
+ }
82
+ };
83
+ switch (propName) {
84
+ case "ref": {
85
+ callWithArg("r");
86
+ break;
87
+ }
88
+ case "bind": {
89
+ const arg = getArg(false);
90
+ const result = (0, expression_1.checkNode)(path.get("arguments")[0], internal);
91
+ if (result.self) {
92
+ path.replaceWith(result.self);
93
+ }
94
+ else if (result.found.size > 0) {
95
+ const found = result.found;
96
+ path.replaceWith(t.callExpression(t.memberExpression(internal.id, t.identifier("ex")), [
97
+ t.arrowFunctionExpression([...found.keys()].map(expression_1.encodeName), arg),
98
+ t.arrayExpression([...found.values()]),
99
+ ]));
100
+ }
101
+ else {
102
+ callWithArg("r");
103
+ }
104
+ break;
105
+ }
106
+ case "calculate":
107
+ case "watch": {
108
+ const args = (0, lib_1.processCalculateCall)(path, internal);
109
+ path.replaceWith(t.callExpression(t.memberExpression(internal.id, t.identifier("ex")), args));
110
+ break;
111
+ }
112
+ case "arrayModel": {
113
+ callWithOptionalArg("sam");
114
+ break;
115
+ }
116
+ case "setModel": {
117
+ callWithOptionalArg("ssm");
118
+ break;
119
+ }
120
+ case "mapModel": {
121
+ callWithOptionalArg("smm");
122
+ break;
123
+ }
124
+ case "reactiveObject": {
125
+ callWithArg("sro");
126
+ break;
127
+ }
128
+ case "value": {
129
+ if (!search) {
130
+ path.replaceWith(t.memberExpression(getArg(), t.identifier("$")));
131
+ }
132
+ else {
133
+ path.replaceWith(getArg());
134
+ }
135
+ break;
136
+ }
137
+ case "setValue": {
138
+ if (node.arguments.length !== 2 || !t.isExpression(node.arguments[0]) || !t.isExpression(node.arguments[1])) {
139
+ throw path.buildCodeFrameError("Vasille: Expected 2 arguments");
140
+ }
141
+ (0, mesh_1.meshExpression)(path.get("arguments")[0], internal);
142
+ if (search) {
143
+ (0, expression_1.checkExpression)(path.get("arguments")[1], search);
144
+ }
145
+ else {
146
+ (0, mesh_1.meshExpression)(path.get("arguments")[1], internal);
147
+ }
148
+ path.replaceWith(t.assignmentExpression("=", t.memberExpression(node.arguments[0], t.identifier("$")), node.arguments[1]));
149
+ break;
150
+ }
151
+ case "stored": {
152
+ const arg = getArg(false);
153
+ if ((t.isIdentifier(arg) && (0, expression_1.idIsIValue)(path.get("arguments")[0], internal)) ||
154
+ (t.isMemberExpression(arg) && (0, expression_1.memberIsIValue)(arg, internal))) {
155
+ path.replaceWith(t.memberExpression(arg, t.identifier("$")));
156
+ }
157
+ else {
158
+ callWithArg("rv");
159
+ }
160
+ break;
161
+ }
162
+ case "destroy": {
163
+ path.replaceWith(t.callExpression(t.memberExpression(getArg(true), t.identifier("destroy")), []));
164
+ break;
165
+ }
166
+ default:
167
+ throw path.buildCodeFrameError(`Vasille: Unknown bridge method "${propName}"`);
168
+ }
169
+ return propName;
170
+ }
171
+ }
172
+ return false;
173
+ }
package/lib/call.js CHANGED
@@ -1,8 +1,46 @@
1
- import * as t from "@babel/types";
2
- import { ctx } from "./internal.js";
3
- export const composeOnly = [
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 = [
4
41
  "forward",
5
42
  "watch",
43
+ "calculate",
6
44
  "ref",
7
45
  "bind",
8
46
  "value",
@@ -11,8 +49,10 @@ export const composeOnly = [
11
49
  "mapModel",
12
50
  "setModel",
13
51
  "reactiveObject",
52
+ "router",
53
+ "runOnDestroy",
14
54
  ];
15
- export const styleOnly = [
55
+ exports.styleOnly = [
16
56
  "theme",
17
57
  "dark",
18
58
  "mobile",
@@ -22,31 +62,39 @@ export const styleOnly = [
22
62
  "prefersLight",
23
63
  "styleSheet",
24
64
  ];
25
- export const requiresContext = ["awaited"];
26
- const requiresContextSet = new Set(requiresContext);
27
- export function calls(node, names, internal) {
65
+ exports.requiresContext = ["awaited"];
66
+ const requiresContextSet = new Set(exports.requiresContext);
67
+ function checkCall(path, name, internal) {
68
+ const node = path.node;
69
+ if (requiresContextSet.has(name) && t.isCallExpression(node)) {
70
+ if (internal.stateOnly) {
71
+ throw path.buildCodeFrameError(`Vasille: ${name} function can be used only in components`);
72
+ }
73
+ node.arguments.unshift(internal_js_1.ctx);
74
+ }
75
+ if (name === "store") {
76
+ internal.stateOnly = true;
77
+ }
78
+ if (["compose", "view", "mvcView", "mvvmView", "hybridView", "screen"].includes(name)) {
79
+ internal.stateOnly = false;
80
+ }
81
+ return name;
82
+ }
83
+ function calls(path, names, internal) {
84
+ const node = path.node;
28
85
  const set = new Set(names);
29
86
  const callee = t.isCallExpression(node) ? node.callee : null;
30
87
  if (callee) {
31
88
  if (t.isIdentifier(callee)) {
32
89
  const mapped = internal.mapping.get(callee.name);
33
90
  if (mapped && set.has(mapped) && internal.stack.get(callee.name) === undefined) {
34
- if (requiresContextSet.has(callee.name) && t.isCallExpression(node)) {
35
- node.arguments.unshift(ctx);
36
- }
37
- if (mapped === "state") {
38
- internal.stateOnly = true;
39
- }
40
- if (mapped === "compose" || mapped === "extend") {
41
- internal.stateOnly = false;
42
- }
43
- return mapped;
91
+ return checkCall(path, mapped, internal);
44
92
  }
45
93
  return false;
46
94
  }
47
- const global = internal.stack.get(internal.global) === undefined;
48
95
  let propName = null;
49
96
  if (t.isMemberExpression(callee)) {
97
+ /* istanbul ignore else */
50
98
  if (t.isIdentifier(callee.property)) {
51
99
  propName = callee.property.name;
52
100
  }
@@ -54,13 +102,13 @@ export function calls(node, names, internal) {
54
102
  propName = callee.property.value;
55
103
  }
56
104
  }
57
- if (t.isMemberExpression(callee) && t.isIdentifier(callee.object) && propName) {
58
- if (global && callee.object.name === internal.global && set.has(propName)) {
59
- if (requiresContextSet.has(propName) && t.isCallExpression(node)) {
60
- node.arguments.unshift(ctx);
61
- }
62
- return callee.object.name;
63
- }
105
+ if (propName &&
106
+ set.has(propName) &&
107
+ t.isMemberExpression(callee) &&
108
+ t.isIdentifier(callee.object) &&
109
+ callee.object.name === internal.global &&
110
+ internal.stack.get(internal.global) === undefined) {
111
+ return checkCall(path, propName, internal);
64
112
  }
65
113
  }
66
114
  return false;
@@ -1,5 +1,41 @@
1
- import * as t from "@babel/types";
2
- import { calls } from "./call.js";
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.findStyleInNode = findStyleInNode;
37
+ const t = __importStar(require("@babel/types"));
38
+ const call_js_1 = require("./call.js");
3
39
  function tryProcessProp(path, pseudo, media, internal) {
4
40
  if (t.isObjectMethod(path.node)) {
5
41
  throw path.buildCodeFrameError("Vasille: Object methods not supported here");
@@ -11,7 +47,7 @@ function tryProcessProp(path, pseudo, media, internal) {
11
47
  }
12
48
  const mediaDefaults = ["mobile", "tablet", "laptop", "prefersDark", "prefersLight"];
13
49
  function processValue(name, path, pseudo, theme, media, mediaDefault, allowFallback, internal) {
14
- if (calls(path.node, ["theme"], internal)) {
50
+ if ((0, call_js_1.calls)(path, ["theme"], internal)) {
15
51
  const call = path.node;
16
52
  if (theme) {
17
53
  throw path.buildCodeFrameError("Vasille: The theme seems the be defined twice");
@@ -25,14 +61,14 @@ function processValue(name, path, pseudo, theme, media, mediaDefault, allowFallb
25
61
  .buildCodeFrameError("Vasille: Expected string literal");
26
62
  }
27
63
  }
28
- if (calls(path.node, ["dark"], internal)) {
64
+ if ((0, call_js_1.calls)(path, ["dark"], internal)) {
29
65
  if (theme) {
30
66
  throw path.buildCodeFrameError("Vasille: The theme seem the be defined twice");
31
67
  }
32
68
  return processValue(name, path.get("arguments")[0], pseudo, `.dark`, media, mediaDefault, false, internal);
33
69
  }
34
70
  let callee;
35
- if ((callee = calls(path.node, mediaDefaults, internal))) {
71
+ if ((callee = (0, call_js_1.calls)(path, mediaDefaults, internal))) {
36
72
  const index = mediaDefaults.indexOf(callee) + 1;
37
73
  if (mediaDefault.includes(index)) {
38
74
  return processValue(name, path.get("arguments")[0], pseudo, theme, media, mediaDefault, false, internal);
@@ -136,13 +172,13 @@ function processProp(path, pseudo, media, internal) {
136
172
  }
137
173
  return processValue(name, path.get("value"), pseudo, "", media, [], true, internal);
138
174
  }
139
- export function findStyleInNode(path, internal) {
175
+ function findStyleInNode(path, internal) {
140
176
  if (t.isExportNamedDeclaration(path.node)) {
141
177
  return findStyleInNode(path.get("declaration"), internal);
142
178
  }
143
179
  if (t.isVariableDeclaration(path.node) &&
144
180
  path.node.declarations.length === 1 &&
145
- calls(path.node.declarations[0].init, ["styleSheet"], internal)) {
181
+ (0, call_js_1.calls)(path.get("declarations")[0].get("init"), ["styleSheet"], internal)) {
146
182
  const call = path.node.declarations[0].init;
147
183
  const callPath = path
148
184
  .get("declarations")[0]