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.
@@ -48,42 +48,37 @@ exports.checkStatements = checkStatements;
48
48
  exports.checkStatement = checkStatement;
49
49
  exports.checkFunction = checkFunction;
50
50
  const t = __importStar(require("@babel/types"));
51
+ const bridge_1 = require("./bridge");
51
52
  const call_js_1 = require("./call.js");
52
53
  const internal_js_1 = require("./internal.js");
54
+ const router_1 = require("./router");
55
+ const utils_1 = require("./utils");
53
56
  function encodeName(name) {
54
- return t.identifier(`Vasille_${name}`);
57
+ return insertName(name);
58
+ }
59
+ function insertName(name, search) {
60
+ const id = t.identifier(`Vasille_${name}`);
61
+ search === null || search === void 0 ? void 0 : search.inserted.add(id);
62
+ return id;
55
63
  }
56
64
  function addIdentifier(path, search) {
57
65
  if (!search.found.has(path.node.name)) {
58
66
  search.found.set(path.node.name, path.node);
59
67
  }
60
- path.replaceWith(encodeName(path.node.name));
61
- }
62
- function stringify(node) {
63
- let name = "";
64
- if (t.isStringLiteral(node)) {
65
- name = node.value;
66
- }
67
- if (t.isPrivateName(node)) {
68
- name = node.id.name;
69
- }
70
- if (t.isIdentifier(node)) {
71
- name = node.name;
72
- }
73
- return name;
68
+ path.replaceWith(insertName(path.node.name, search));
74
69
  }
75
70
  function extractMemberName(path, search) {
76
71
  const names = [];
77
72
  let it = path.node;
78
73
  while (t.isMemberExpression(it)) {
79
- const name = stringify(it.property);
74
+ const name = (0, utils_1.stringify)(it.property);
80
75
  if (name === "$" && it !== path.node) {
81
76
  throw path.buildCodeFrameError("Vasille: The reactive/observable value is nested");
82
77
  }
83
78
  it = it.object;
84
79
  names.push(name);
85
80
  }
86
- names.push(stringify(it));
81
+ names.push((0, utils_1.stringify)(it));
87
82
  if (t.isIdentifier(it) &&
88
83
  search.stack.get(it.name, internal_js_1.VariableScope.Local) === 1 /* VariableState.Ignored */) {
89
84
  throw path.buildCodeFrameError("Vasille: This node cannot be processed, the root of expression is a local variable");
@@ -92,17 +87,18 @@ function extractMemberName(path, search) {
92
87
  }
93
88
  function addMemberExpr(path, search) {
94
89
  const name = extractMemberName(path, search);
90
+ /* istanbul ignore else */
95
91
  if (!search.found.has(name)) {
96
92
  search.found.set(name, path.node);
97
93
  }
98
- path.replaceWith(encodeName(name));
94
+ path.replaceWith(insertName(name, search));
99
95
  }
100
96
  function addExternalIValue(path, search) {
101
97
  const name = extractMemberName(path, search);
102
98
  if (!search.found.has(name)) {
103
99
  search.found.set(name, path.node.object);
104
100
  }
105
- path.replaceWith(encodeName(name));
101
+ path.replaceWith(insertName(name, search));
106
102
  }
107
103
  function meshIdentifier(path, internal) {
108
104
  if (idIsIValue(path, internal)) {
@@ -149,6 +145,7 @@ function meshMember(path, internal) {
149
145
  }
150
146
  function meshLValue(path, internal) {
151
147
  const node = path.node;
148
+ /* istanbul ignore else */
152
149
  if (t.isIdentifier(node)) {
153
150
  meshIdentifier(path, internal);
154
151
  }
@@ -157,6 +154,7 @@ function meshLValue(path, internal) {
157
154
  }
158
155
  else if (t.isArrayPattern(node)) {
159
156
  for (const item of path.get("elements")) {
157
+ /* istanbul ignore else */
160
158
  if (t.isOptionalMemberExpression(item.node) || t.isLVal(item.node)) {
161
159
  meshLValue(item, internal);
162
160
  }
@@ -171,6 +169,7 @@ function checkNode(path, internal) {
171
169
  external: internal,
172
170
  found: new Map(),
173
171
  self: null,
172
+ inserted: new Set(),
174
173
  stack: internal.stack,
175
174
  };
176
175
  if (t.isIdentifier(path.node)) {
@@ -191,6 +190,7 @@ function checkNode(path, internal) {
191
190
  }
192
191
  internal.stack.fixLocalIndex();
193
192
  internal.stack.push();
193
+ /* istanbul ignore else */
194
194
  if (t.isExpression(path.node)) {
195
195
  checkExpression(path, search);
196
196
  }
@@ -200,6 +200,7 @@ function checkNode(path, internal) {
200
200
  }
201
201
  function checkOrIgnoreAllExpressions(nodePaths, search) {
202
202
  for (const path of nodePaths) {
203
+ /* istanbul ignore else */
203
204
  if (t.isExpression(path.node)) {
204
205
  checkExpression(path, search);
205
206
  }
@@ -212,6 +213,7 @@ function checkAllExpressions(nodePaths, search) {
212
213
  }
213
214
  function checkAllUnknown(paths, internal) {
214
215
  for (const path of paths) {
216
+ /* istanbul ignore else */
215
217
  if (t.isSpreadElement(path.node)) {
216
218
  checkExpression(path.get("argument"), internal);
217
219
  }
@@ -221,6 +223,7 @@ function checkAllUnknown(paths, internal) {
221
223
  }
222
224
  }
223
225
  function checkOrIgnoreExpression(path, search) {
226
+ /* istanbul ignore else */
224
227
  if (t.isExpression(path.node)) {
225
228
  checkExpression(path, search);
226
229
  }
@@ -241,6 +244,7 @@ function checkExpression(nodePath, search) {
241
244
  break;
242
245
  }
243
246
  case "Identifier": {
247
+ /* istanbul ignore else */
244
248
  if (expr && t.isIdentifier(expr)) {
245
249
  if (idIsIValue(nodePath, search.external, internal_js_1.VariableScope.Global) &&
246
250
  !idIsLocal(nodePath, search.external)) {
@@ -256,11 +260,27 @@ function checkExpression(nodePath, search) {
256
260
  }
257
261
  case "CallExpression": {
258
262
  const path = nodePath;
259
- if ((0, call_js_1.calls)(path.node, call_js_1.composeOnly, search.external)) {
260
- throw path.buildCodeFrameError("Vasille: Usage of hints is restricted here");
263
+ const bridge = (0, bridge_1.processBridgeCall)(path, search.external, search);
264
+ if (bridge) {
265
+ if (bridge === "value") {
266
+ addMemberExpr(nodePath, search);
267
+ }
268
+ }
269
+ else if ((0, call_js_1.calls)(path, ["router"], search.external)) {
270
+ if (!search.external.stateOnly) {
271
+ (0, router_1.routerReplace)(path);
272
+ }
273
+ else {
274
+ throw path.buildCodeFrameError("Vasille: The router is not available in stores");
275
+ }
276
+ }
277
+ else {
278
+ if ((0, call_js_1.calls)(path, call_js_1.composeOnly, search.external)) {
279
+ throw path.buildCodeFrameError("Vasille: Usage of hints is restricted here");
280
+ }
281
+ checkOrIgnoreExpression(path.get("callee"), search);
282
+ checkAllUnknown(path.get("arguments"), search);
261
283
  }
262
- checkOrIgnoreExpression(path.get("callee"), search);
263
- checkAllUnknown(path.get("arguments"), search);
264
284
  break;
265
285
  }
266
286
  case "OptionalCallExpression": {
@@ -329,6 +349,7 @@ function checkExpression(nodePath, search) {
329
349
  case "UpdateExpression": {
330
350
  const path = nodePath;
331
351
  const arg = path.node.argument;
352
+ /* istanbul ignore else */
332
353
  if (t.isLVal(arg)) {
333
354
  meshLValue(path.get("argument"), search.external);
334
355
  }
@@ -407,11 +428,13 @@ function checkStatements(paths, search) {
407
428
  }
408
429
  }
409
430
  function ignoreLocals(val, search) {
431
+ /* istanbul ignore else */
410
432
  if (t.isIdentifier(val)) {
411
433
  search.stack.set(val.name, 1 /* VariableState.Ignored */);
412
434
  }
413
435
  else if (t.isObjectPattern(val)) {
414
436
  for (const prop of val.properties) {
437
+ /* istanbul ignore else */
415
438
  if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
416
439
  search.stack.set(prop.value.name, 1 /* VariableState.Ignored */);
417
440
  }
@@ -425,6 +448,7 @@ function ignoreLocals(val, search) {
425
448
  }
426
449
  else if (t.isArrayPattern(val)) {
427
450
  for (const element of val.elements) {
451
+ /* istanbul ignore else */
428
452
  if (element && !t.isVoidPattern(element)) {
429
453
  ignoreLocals(element, search);
430
454
  }
@@ -432,6 +456,7 @@ function ignoreLocals(val, search) {
432
456
  }
433
457
  else if (t.isVariableDeclaration(val)) {
434
458
  for (const declarator of val.declarations) {
459
+ /* istanbul ignore else */
435
460
  if (!t.isVoidPattern(declarator.id)) {
436
461
  ignoreLocals(declarator.id, search);
437
462
  }
@@ -478,6 +503,7 @@ function checkStatement(path, search) {
478
503
  case "ForStatement": {
479
504
  const _path = path;
480
505
  const node = _path.node;
506
+ /* istanbul ignore else */
481
507
  if (node.init) {
482
508
  if (t.isExpression(node.init)) {
483
509
  checkExpression(_path.get("init"), search);
@@ -535,6 +561,7 @@ function checkStatement(path, search) {
535
561
  case "TryStatement":
536
562
  const handlerPath = path.get("handler");
537
563
  checkStatement(path.get("block"), search);
564
+ /* istanbul ignore else */
538
565
  if (handlerPath.node) {
539
566
  checkStatement(handlerPath.get("body"), search);
540
567
  }
package/lib-node/jsx.js CHANGED
@@ -60,6 +60,7 @@ function transformJsxArray(paths, internal) {
60
60
  .replace(/\s*\n\s*/gm, "\n");
61
61
  const call = t.callExpression(t.memberExpression(internal_js_1.ctx, t.identifier("text")), [t.stringLiteral(fixed)]);
62
62
  call.loc = path.node.loc;
63
+ /* istanbul ignore else */
63
64
  if (call.loc) {
64
65
  for (const char of path.node.value) {
65
66
  if (!/\s/.test(char)) {
@@ -96,7 +97,7 @@ function transformJsxExpressionContainer(path, internal, acceptSlots, isInternal
96
97
  if (acceptSlots &&
97
98
  (t.isFunctionExpression(expression) || t.isArrowFunctionExpression(expression)) &&
98
99
  (0, jsx_detect_js_1.bodyHasJsx)(expression.body)) {
99
- (0, mesh_js_1.compose)(path.get("expression"), internal, isInternalSlot);
100
+ (0, mesh_js_1.compose)(path.get("expression"), internal, isInternalSlot, "slot");
100
101
  if (!isInternalSlot) {
101
102
  if (expression.params.length < 1) {
102
103
  expression.params.push(t.identifier(`_${internal.prefix}`));
@@ -112,11 +113,12 @@ function transformJsxExpressionContainer(path, internal, acceptSlots, isInternal
112
113
  else if (isInternalSlot && (t.isFunctionExpression(expression) || t.isArrowFunctionExpression(expression))) {
113
114
  expression.params.unshift(internal_js_1.ctx);
114
115
  }
115
- let call = (0, lib_js_1.exprCall)(path.get("expression"), expression, internal);
116
+ const exprPath = path.get("expression");
117
+ let call = (0, lib_js_1.exprCall)(exprPath, expression, internal);
116
118
  if (!call && t.isIdentifier(expression) && internal.stack.get(expression.name) === 3 /* VariableState.ReactiveObject */) {
117
119
  call = t.callExpression(t.memberExpression(internal.id, t.identifier("rop")), [expression]);
118
120
  }
119
- const result = call !== null && call !== void 0 ? call : expression;
121
+ const result = call !== null && call !== void 0 ? call : exprPath.node;
120
122
  result.loc = loc;
121
123
  return result;
122
124
  }
@@ -136,7 +138,7 @@ function idToProp(id, value, from) {
136
138
  return t.objectProperty(expr, value);
137
139
  }
138
140
  function transformJsxElement(path, internal) {
139
- var _a, _b, _c, _d, _e;
141
+ var _a, _b, _c, _d, _e, _f, _g;
140
142
  const name = path.node.openingElement.name;
141
143
  if (t.isJSXIdentifier(name) && name.name[0].toLowerCase() === name.name[0]) {
142
144
  const opening = path.get("openingElement");
@@ -152,10 +154,12 @@ function transformJsxElement(path, internal) {
152
154
  const attr = attrPath.node;
153
155
  if (t.isJSXAttribute(attr)) {
154
156
  const name = attr.name;
157
+ /* istanbul ignore else */
155
158
  if (t.isJSXIdentifier(name)) {
156
159
  if (name.name.startsWith("on")) {
157
160
  if (t.isJSXExpressionContainer(attr.value) && t.isExpression(attr.value.expression)) {
158
161
  const path = attrPath.get("value");
162
+ /* istanbul ignore else */
159
163
  if (t.isExpression(path.node.expression)) {
160
164
  (0, mesh_js_1.meshExpression)(path.get("expression"), internal);
161
165
  }
@@ -169,6 +173,7 @@ function transformJsxElement(path, internal) {
169
173
  }
170
174
  else if (name.name === "class") {
171
175
  // class={[..]}
176
+ /* istanbul ignore else */
172
177
  if (t.isJSXExpressionContainer(attr.value) && t.isArrayExpression(attr.value.expression)) {
173
178
  const valuePath = attrPath.get("value");
174
179
  const arrayExprPath = valuePath.get("expression");
@@ -233,8 +238,9 @@ function transformJsxElement(path, internal) {
233
238
  }
234
239
  }
235
240
  // class={name}
236
- else if (t.isJSXExpressionContainer(attr.value) && t.isIdentifier(attr.value.expression)) {
237
- attrs.push(t.objectProperty(t.identifier("class"), attr.value.expression));
241
+ else if (t.isJSXExpressionContainer(attr.value) && t.isExpression(attr.value.expression)) {
242
+ const expr = (_b = (0, lib_js_1.exprCall)(attrPath.get("value"), attr.value.expression, internal)) !== null && _b !== void 0 ? _b : attr.value.expression;
243
+ attrs.push(t.objectProperty(t.identifier("class"), expr));
238
244
  }
239
245
  // class="a b"
240
246
  else if (t.isStringLiteral(attr.value)) {
@@ -243,6 +249,7 @@ function transformJsxElement(path, internal) {
243
249
  }
244
250
  else if (name.name === "style") {
245
251
  // style={{..}}
252
+ /* istanbul ignore else */
246
253
  if (t.isJSXExpressionContainer(attr.value) && t.isObjectExpression(attr.value.expression)) {
247
254
  const valuePath = attrPath.get("value");
248
255
  const objectPath = valuePath.get("expression");
@@ -250,7 +257,7 @@ function transformJsxElement(path, internal) {
250
257
  // style={{a: b}}
251
258
  if (t.isObjectProperty(propPath.node)) {
252
259
  const prop = propPath;
253
- const value = (_b = (0, lib_js_1.exprCall)(prop.get("value"), prop.node.value, internal)) !== null && _b !== void 0 ? _b : prop.node.value;
260
+ const value = (_c = (0, lib_js_1.exprCall)(prop.get("value"), prop.node.value, internal)) !== null && _c !== void 0 ? _c : prop.node.value;
254
261
  if (t.isExpression(prop.node.key) && !t.isIdentifier(prop.node.key)) {
255
262
  (0, mesh_js_1.meshExpression)(prop.get("key"), internal);
256
263
  }
@@ -309,10 +316,15 @@ function transformJsxElement(path, internal) {
309
316
  console.warn(attrPath.buildCodeFrameError("Vasille: This will slow down your application"));
310
317
  }
311
318
  }
319
+ else if (t.isJSXExpressionContainer(attr.value) && t.isExpression(attr.value.expression)) {
320
+ const expr = (_d = (0, lib_js_1.exprCall)(attrPath.get("value"), attr.value.expression, internal)) !== null && _d !== void 0 ? _d : attr.value.expression;
321
+ attrs.push(t.objectProperty(t.identifier("style"), expr));
322
+ }
312
323
  }
313
324
  else {
325
+ /* istanbul ignore else */
314
326
  if (!attr.value || t.isJSXExpressionContainer(attr.value)) {
315
- attrs.push(idToProp(name, t.isExpression((_c = attr.value) === null || _c === void 0 ? void 0 : _c.expression) ? attr.value.expression : t.booleanLiteral(true)));
327
+ attrs.push(idToProp(name, t.isExpression((_e = attr.value) === null || _e === void 0 ? void 0 : _e.expression) ? attr.value.expression : t.booleanLiteral(true)));
316
328
  }
317
329
  else if (t.isStringLiteral(attr.value)) {
318
330
  attrs.push(idToProp(name, attr.value));
@@ -321,11 +333,12 @@ function transformJsxElement(path, internal) {
321
333
  }
322
334
  if (t.isJSXNamespacedName(name)) {
323
335
  if (name.namespace.name === "bind") {
336
+ /* istanbul ignore else */
324
337
  if (t.isJSXExpressionContainer(attr.value) || !attr.value) {
325
- const value = t.isExpression((_d = attr.value) === null || _d === void 0 ? void 0 : _d.expression)
338
+ const value = t.isExpression((_f = attr.value) === null || _f === void 0 ? void 0 : _f.expression)
326
339
  ? (0, lib_js_1.exprCall)(attrPath.get("value").get("expression"), attr.value.expression, internal)
327
340
  : undefined;
328
- bind.push(idToProp(name.name, value !== null && value !== void 0 ? value : (t.isExpression((_e = attr.value) === null || _e === void 0 ? void 0 : _e.expression) ? attr.value.expression : t.booleanLiteral(true))));
341
+ bind.push(idToProp(name.name, value !== null && value !== void 0 ? value : (t.isExpression((_g = attr.value) === null || _g === void 0 ? void 0 : _g.expression) ? attr.value.expression : t.booleanLiteral(true))));
329
342
  }
330
343
  else if (t.isStringLiteral(attr.value)) {
331
344
  bind.push(idToProp(name.name, attr.value));
@@ -385,6 +398,7 @@ function transformJsxElement(path, internal) {
385
398
  // <A prop=../>
386
399
  if (t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name)) {
387
400
  // <A prop=".."/>
401
+ /* istanbul ignore else */
388
402
  if (t.isStringLiteral(attr.value)) {
389
403
  props.push(idToProp(attr.name, attr.value));
390
404
  }
@@ -431,7 +445,7 @@ function transformJsxElement(path, internal) {
431
445
  run = t.arrowFunctionExpression(params, t.blockStatement(statements));
432
446
  }
433
447
  }
434
- const call = t.callExpression(t.identifier(name.name), [internal_js_1.ctx, t.objectExpression(props), ...(run ? [run] : [])]);
448
+ const call = t.callExpression(t.identifier(name.name), [t.objectExpression(props), internal_js_1.ctx, ...(run ? [run] : [])]);
435
449
  call.loc = path.node.loc;
436
450
  return t.expressionStatement(call);
437
451
  }
package/lib-node/lib.js CHANGED
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.named = named;
37
+ exports.processCalculateCall = processCalculateCall;
37
38
  exports.parseCalculateCall = parseCalculateCall;
38
39
  exports.exprCall = exprCall;
39
40
  exports.forwardOnlyExpr = forwardOnlyExpr;
@@ -56,23 +57,24 @@ function named(call, name, internal, argPos) {
56
57
  }
57
58
  return call;
58
59
  }
59
- function parseCalculateCall(path, internal) {
60
- if (t.isCallExpression(path.node) && (0, call_js_1.calls)(path.node, ["calculate", "watch"], internal)) {
61
- const call = path.node.arguments[0];
62
- if (path.node.arguments.length !== 1) {
63
- throw path.buildCodeFrameError("Vasille: Incorrect number of arguments");
64
- }
65
- if (t.isFunctionExpression(call) || t.isArrowFunctionExpression(call)) {
66
- if (call.params.length > 0) {
67
- throw path.buildCodeFrameError("Vasille: Argument of calculate cannot have parameters");
68
- }
69
- const exprData = (0, expression_js_1.checkNode)(path.get("arguments")[0], internal);
70
- call.params = [...exprData.found.keys()].map(name => (0, expression_js_1.encodeName)(name));
71
- return [call, t.arrayExpression([...exprData.found.values()])];
72
- }
73
- else {
74
- throw path.buildCodeFrameError("Vasille: Argument of calculate must be a function");
60
+ function processCalculateCall(path, internal) {
61
+ const call = path.node.arguments[0];
62
+ if (path.node.arguments.length !== 1) {
63
+ throw path.buildCodeFrameError("Vasille: Incorrect number of arguments");
64
+ }
65
+ if (t.isFunctionExpression(call) || t.isArrowFunctionExpression(call)) {
66
+ if (call.params.length > 0) {
67
+ throw path.buildCodeFrameError("Vasille: Argument of calculate cannot have parameters");
75
68
  }
69
+ const exprData = (0, expression_js_1.checkNode)(path.get("arguments")[0], internal);
70
+ call.params = [...exprData.found.keys()].map(name => (0, expression_js_1.encodeName)(name));
71
+ return [call, t.arrayExpression([...exprData.found.values()])];
72
+ }
73
+ throw path.buildCodeFrameError("Vasille: Argument of calculate must be a function");
74
+ }
75
+ function parseCalculateCall(path, internal) {
76
+ if (t.isCallExpression(path.node) && (0, call_js_1.calls)(path, ["calculate", "watch"], internal)) {
77
+ return processCalculateCall(path, internal);
76
78
  }
77
79
  return null;
78
80
  }
@@ -84,10 +86,11 @@ function exprCall(path, expr, internal, name) {
84
86
  : t.memberExpression(internal_js_1.ctx, t.identifier("expr")), calculateCall), name, internal);
85
87
  }
86
88
  if (t.isCallExpression(expr) &&
87
- (0, call_js_1.calls)(expr, ["forward"], internal) &&
89
+ (0, call_js_1.calls)(path, ["forward"], internal) &&
88
90
  expr.arguments.length === 1 &&
89
91
  t.isExpression(expr.arguments[0])) {
90
92
  const data = exprCall(path.get("arguments")[0], expr.arguments[0], internal);
93
+ /* istanbul ignore else */
91
94
  if (data && !t.isCallExpression(data)) {
92
95
  return t.callExpression(t.memberExpression(internal.id, t.identifier("fo")), [data]);
93
96
  }
@@ -96,12 +99,15 @@ function exprCall(path, expr, internal, name) {
96
99
  if (exprData.self) {
97
100
  return exprData.self;
98
101
  }
99
- const names = [...exprData.found.keys()].map(name => (0, expression_js_1.encodeName)(name));
102
+ const names = [...exprData.found.keys()].map(expression_js_1.encodeName);
100
103
  const dependencies = t.arrayExpression([...exprData.found.values()]);
101
- if (names.length > 0 && expr) {
104
+ if (expr !== path.node && names.length === 1 && t.isIdentifier(path.node) && path.node.name === names[0].name) {
105
+ return [...exprData.found.values()][0];
106
+ }
107
+ if (names.length > 0 && path.node) {
102
108
  return named(t.callExpression(internal.stateOnly
103
109
  ? t.memberExpression(internal.id, t.identifier("ex"))
104
- : t.memberExpression(internal_js_1.ctx, t.identifier("expr")), [t.arrowFunctionExpression(names, expr), dependencies]), name, internal);
110
+ : t.memberExpression(internal_js_1.ctx, t.identifier("expr")), [t.arrowFunctionExpression(names, path.node), dependencies]), name, internal);
105
111
  }
106
112
  return null;
107
113
  }