babel-plugin-vasille 4.1.5 → 4.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
@@ -35,6 +35,7 @@ $ npm create vasille
35
35
  ### Full documentation:
36
36
  * [Learn `Vasille` in 5 minutes](https://github.com/vasille-js/vasille-js/blob/v4/doc/V4-API.md)
37
37
  * [Vasille Router Documentation](https://github.com/vasille-js/vasille-js/blob/v4/doc/Router-API.md)
38
+ * [Vasille Compostion function](https://github.com/vasille-js/vasille-js/blob/v4/doc/Compositions.md)
38
39
 
39
40
  ### Examples
40
41
  * [TypeScript Example](https://github.com/vasille-js/example-typescript)
@@ -103,6 +104,10 @@ All of these are supported:
103
104
 
104
105
  ## Change log
105
106
 
107
+ ### 4.2.0
108
+
109
+ Add support for inlined conditions in JSX, binary `&&` and ternary `?:` operator.
110
+
106
111
  ### 4.1.0
107
112
 
108
113
  Added SSG (static site generation) as build option `vasille-web build static`.
package/lib/jsx.js CHANGED
@@ -86,10 +86,19 @@ function transformJsxArray(paths, internal) {
86
86
  }
87
87
  }
88
88
  else if (path.isJSXExpressionContainer()) {
89
- const value = transformJsxExpressionContainer(path, internal, false, false, true, true);
90
- const call = t.callExpression(t.memberExpression(internal_js_1.ctx, t.identifier("text")), [value]);
91
- call.loc = value.loc;
92
- result.push(t.expressionStatement(call));
89
+ const conditionalJsx = tryForConditionalJsx(path, internal);
90
+ if (conditionalJsx.length) {
91
+ result.push(...conditionalJsx);
92
+ }
93
+ else {
94
+ const value = transformJsxExpressionContainer(path, internal, false, false, true, true);
95
+ /* istanbul ignore else */
96
+ if (!t.isJSXEmptyExpression(value)) {
97
+ const call = t.callExpression(t.memberExpression(internal_js_1.ctx, t.identifier("text")), [value]);
98
+ call.loc = value.loc;
99
+ result.push(t.expressionStatement(call));
100
+ }
101
+ }
93
102
  }
94
103
  else {
95
104
  (0, lib_js_1.err)(lib_js_1.Errors.TokenNotSupported, path, "Spread child is not supported", internal);
@@ -98,6 +107,77 @@ function transformJsxArray(paths, internal) {
98
107
  result.push(...processConditions(conditions, internal));
99
108
  return result;
100
109
  }
110
+ function statementsToFunction(arr) {
111
+ if (arr.length == 1 && t.isExpressionStatement(arr[0])) {
112
+ return t.arrowFunctionExpression([], arr[0].expression);
113
+ }
114
+ else {
115
+ return t.arrowFunctionExpression([], t.blockStatement(arr));
116
+ }
117
+ }
118
+ function checkIfExpressionIsConditionalJsx(expr) {
119
+ return ((t.isLogicalExpression(expr) &&
120
+ expr.operator === "&&" &&
121
+ !t.isJSX(expr.left) &&
122
+ (t.isJSX(expr.right) || checkIfExpressionIsConditionalJsx(expr.right))) ||
123
+ (t.isConditionalExpression(expr) &&
124
+ !t.isJSX(expr.test) &&
125
+ (t.isJSX(expr.consequent) || checkIfExpressionIsConditionalJsx(expr.consequent)) &&
126
+ (t.isJSX(expr.alternate) || checkIfExpressionIsConditionalJsx(expr.alternate))));
127
+ }
128
+ function processReactiveCondition(path, internal) {
129
+ (0, lib_js_1.exprCall)(path, path.node, internal, {});
130
+ return path.node;
131
+ }
132
+ function addConditionToCollection(condition, exprPath, internal, conditions) {
133
+ if (exprPath.isJSXElement() || exprPath.isJSXFragment()) {
134
+ const local = { cases: null };
135
+ conditions.push({
136
+ condition: processReactiveCondition(condition, internal),
137
+ slot: statementsToFunction([...transformJsx(exprPath, local, internal), ...processConditions(local, internal)]),
138
+ });
139
+ }
140
+ else {
141
+ const local = [];
142
+ const _default = processConditionalJsxExpression(exprPath, internal, local);
143
+ /* istanbul ignore else */
144
+ if (local.length) {
145
+ conditions.push({
146
+ condition: processReactiveCondition(condition, internal),
147
+ slot: statementsToFunction(processConditions({ cases: local }, internal, _default)),
148
+ });
149
+ }
150
+ }
151
+ }
152
+ function processConditionalJsxExpression(expr, internal, conditions) {
153
+ if (expr.isLogicalExpression()) {
154
+ addConditionToCollection(expr.get("left"), expr.get("right"), internal, conditions);
155
+ return undefined;
156
+ }
157
+ /* istanbul ignore else */
158
+ if (expr.isConditionalExpression()) {
159
+ const consent = expr.get("consequent");
160
+ const alternate = expr.get("alternate");
161
+ addConditionToCollection(expr.get("test"), consent, internal, conditions);
162
+ if (alternate.isJSXFragment() || alternate.isJSXElement()) {
163
+ const local = { cases: null };
164
+ return statementsToFunction([...transformJsx(alternate, local, internal), ...processConditions(local, internal)]);
165
+ }
166
+ return processConditionalJsxExpression(alternate, internal, conditions);
167
+ }
168
+ }
169
+ function tryForConditionalJsx(path, internal) {
170
+ const expr = path.get("expression");
171
+ if (expr.isExpression() && checkIfExpressionIsConditionalJsx(expr.node)) {
172
+ const conditions = [];
173
+ const _default = processConditionalJsxExpression(expr, internal, conditions);
174
+ /* istanbul ignore else */
175
+ if (_default || conditions.length) {
176
+ return processConditions({ cases: conditions }, internal, _default);
177
+ }
178
+ }
179
+ return [];
180
+ }
101
181
  function transformJsxExpressionContainer(path, internal, acceptSlots, isInternalSlot, acceptsReactive, acceptsRaw) {
102
182
  const expression = path.get("expression");
103
183
  const loc = expression.node.loc;
package/lib/mesh.js CHANGED
@@ -533,18 +533,20 @@ function procedureProcessObjectExpression(path, internal, state, prefix) {
533
533
  if (valuePath.isObjectExpression()) {
534
534
  procedureProcessObjectExpression(valuePath, internal, state, `${prefix}${name}.`);
535
535
  }
536
- else {
537
- meshExpression(valuePath, internal);
538
- }
539
536
  if (name.startsWith("$")) {
540
537
  if (internal.isComposing && !internal.isFunctionParsing) {
538
+ meshExpression(valuePath, internal);
541
539
  valuePath.replaceWith(internal.ref(valuePath.node));
542
540
  state[name] = 1;
543
541
  }
544
- else {
542
+ else if (!((valuePath.isIdentifier() && (0, expression_js_1.idIsIValue)(valuePath)) ||
543
+ (valuePath.isMemberExpression() && (0, expression_js_1.memberIsIValue)(valuePath.node)))) {
545
544
  (0, lib_js_1.err)(lib_js_1.Errors.RulesOfVasille, prop.get("key"), "This property is not a reactive", internal);
546
545
  }
547
546
  }
547
+ else {
548
+ meshExpression(valuePath, internal);
549
+ }
548
550
  }
549
551
  }
550
552
  // the property name is unknown in compile time
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-vasille",
3
- "version": "4.1.5",
3
+ "version": "4.2.1",
4
4
  "description": "Convert Vasille Meta Language code to pure JavaScript",
5
5
  "main": "lib/index.js",
6
6
  "type": "commonjs",