@builder.io/mitosis 0.4.0 → 0.4.2

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.
Files changed (196) hide show
  1. package/dist/src/constants/media-sizes.js +4 -5
  2. package/dist/src/generators/alpine/generate.js +114 -132
  3. package/dist/src/generators/alpine/render-mount-hook.js +8 -5
  4. package/dist/src/generators/alpine/render-update-hooks.js +13 -13
  5. package/dist/src/generators/angular/helpers.js +19 -9
  6. package/dist/src/generators/angular/index.js +599 -584
  7. package/dist/src/generators/builder.js +200 -165
  8. package/dist/src/generators/context/angular.js +32 -26
  9. package/dist/src/generators/context/helpers/context-with-symbol-key.js +25 -21
  10. package/dist/src/generators/context/qwik.js +21 -21
  11. package/dist/src/generators/context/react.js +22 -22
  12. package/dist/src/generators/context/rsc.js +20 -22
  13. package/dist/src/generators/context/solid.js +22 -22
  14. package/dist/src/generators/context/svelte.js +33 -28
  15. package/dist/src/generators/context/vue.js +1 -1
  16. package/dist/src/generators/helpers/context.js +6 -13
  17. package/dist/src/generators/helpers/functions.js +8 -8
  18. package/dist/src/generators/helpers/on-mount.js +9 -5
  19. package/dist/src/generators/helpers/rsc.js +7 -9
  20. package/dist/src/generators/html.js +995 -534
  21. package/dist/src/generators/liquid.js +81 -79
  22. package/dist/src/generators/lit/collect-class-string.js +10 -12
  23. package/dist/src/generators/lit/generate.js +204 -159
  24. package/dist/src/generators/marko/generate.js +193 -183
  25. package/dist/src/generators/minify.js +5 -9
  26. package/dist/src/generators/mitosis.js +135 -134
  27. package/dist/src/generators/qwik/component-generator.js +159 -154
  28. package/dist/src/generators/qwik/component.js +51 -81
  29. package/dist/src/generators/qwik/directives.js +68 -84
  30. package/dist/src/generators/qwik/helpers/add-prevent-default.js +6 -7
  31. package/dist/src/generators/qwik/helpers/convert-method-to-function.js +38 -38
  32. package/dist/src/generators/qwik/helpers/handlers.js +16 -25
  33. package/dist/src/generators/qwik/helpers/stable-inject.js +7 -8
  34. package/dist/src/generators/qwik/helpers/stable-serialize.js +7 -8
  35. package/dist/src/generators/qwik/helpers/state.js +36 -43
  36. package/dist/src/generators/qwik/helpers/styles.js +19 -40
  37. package/dist/src/generators/qwik/jsx.js +63 -75
  38. package/dist/src/generators/qwik/src-generator.js +134 -167
  39. package/dist/src/generators/react/blocks.js +122 -127
  40. package/dist/src/generators/react/generator.js +314 -259
  41. package/dist/src/generators/react/helpers.js +23 -28
  42. package/dist/src/generators/react/state.js +34 -51
  43. package/dist/src/generators/react-native/index.js +72 -84
  44. package/dist/src/generators/react-native/sanitize-react-native-block-styles.js +14 -26
  45. package/dist/src/generators/rsc.js +30 -36
  46. package/dist/src/generators/solid/blocks.js +46 -43
  47. package/dist/src/generators/solid/helpers/styles.js +11 -11
  48. package/dist/src/generators/solid/index.js +199 -190
  49. package/dist/src/generators/solid/state/helpers.js +76 -90
  50. package/dist/src/generators/solid/state/signals.js +30 -37
  51. package/dist/src/generators/solid/state/state.js +35 -48
  52. package/dist/src/generators/solid/state/store.js +40 -46
  53. package/dist/src/generators/stencil/collect-class-string.js +10 -12
  54. package/dist/src/generators/stencil/generate.js +158 -145
  55. package/dist/src/generators/svelte/blocks.js +146 -144
  56. package/dist/src/generators/svelte/helpers.js +10 -17
  57. package/dist/src/generators/svelte/svelte.js +321 -287
  58. package/dist/src/generators/swift-ui.js +162 -101
  59. package/dist/src/generators/taro.js +32 -36
  60. package/dist/src/generators/template.js +71 -74
  61. package/dist/src/generators/vue/blocks.js +116 -107
  62. package/dist/src/generators/vue/compositionApi.js +67 -50
  63. package/dist/src/generators/vue/helpers.js +71 -96
  64. package/dist/src/generators/vue/optionsApi.js +134 -105
  65. package/dist/src/generators/vue/vue.js +210 -207
  66. package/dist/src/helpers/babel-transform.js +40 -63
  67. package/dist/src/helpers/bindings.js +4 -12
  68. package/dist/src/helpers/camel-case.js +4 -5
  69. package/dist/src/helpers/capitalize.js +1 -1
  70. package/dist/src/helpers/component-file-extensions.js +23 -35
  71. package/dist/src/helpers/create-mitosis-component.js +22 -26
  72. package/dist/src/helpers/create-mitosis-context.js +5 -12
  73. package/dist/src/helpers/create-mitosis-node.js +10 -12
  74. package/dist/src/helpers/dash-case.js +2 -2
  75. package/dist/src/helpers/dedent.js +18 -22
  76. package/dist/src/helpers/event-handlers.js +1 -1
  77. package/dist/src/helpers/fast-clone.js +1 -1
  78. package/dist/src/helpers/filter-empty-text-nodes.js +2 -2
  79. package/dist/src/helpers/generic-format.js +7 -8
  80. package/dist/src/helpers/get-bindings.js +4 -4
  81. package/dist/src/helpers/get-components-used.js +3 -3
  82. package/dist/src/helpers/get-components.js +4 -4
  83. package/dist/src/helpers/get-custom-imports.js +10 -12
  84. package/dist/src/helpers/get-prop-functions.js +7 -8
  85. package/dist/src/helpers/get-props-ref.js +8 -8
  86. package/dist/src/helpers/get-props.js +12 -13
  87. package/dist/src/helpers/get-refs.js +4 -4
  88. package/dist/src/helpers/get-state-object-string.js +42 -61
  89. package/dist/src/helpers/get-state-used.js +7 -8
  90. package/dist/src/helpers/get-styles.js +6 -6
  91. package/dist/src/helpers/getters-to-functions.js +8 -12
  92. package/dist/src/helpers/handle-missing-state.js +3 -3
  93. package/dist/src/helpers/has-bindings-text.js +6 -6
  94. package/dist/src/helpers/has-component.js +4 -4
  95. package/dist/src/helpers/has-props.js +3 -3
  96. package/dist/src/helpers/has-stateful-dom.js +4 -4
  97. package/dist/src/helpers/has.js +3 -3
  98. package/dist/src/helpers/indent.js +2 -3
  99. package/dist/src/helpers/is-children.js +4 -5
  100. package/dist/src/helpers/is-component.js +1 -1
  101. package/dist/src/helpers/is-html-attribute.js +2 -11
  102. package/dist/src/helpers/is-mitosis-node.js +1 -1
  103. package/dist/src/helpers/is-root-text-node.js +1 -1
  104. package/dist/src/helpers/is-upper-case.js +1 -1
  105. package/dist/src/helpers/is-valid-attribute-name.js +1 -1
  106. package/dist/src/helpers/json.js +2 -2
  107. package/dist/src/helpers/map-refs.js +29 -40
  108. package/dist/src/helpers/merge-options.js +20 -31
  109. package/dist/src/helpers/mitosis-imports.js +5 -6
  110. package/dist/src/helpers/nodes/for.js +2 -3
  111. package/dist/src/helpers/nullable.js +1 -3
  112. package/dist/src/helpers/on-event.js +32 -36
  113. package/dist/src/helpers/output.js +4 -5
  114. package/dist/src/helpers/parse-node.js +13 -5
  115. package/dist/src/helpers/parsers.js +10 -14
  116. package/dist/src/helpers/patterns.js +6 -8
  117. package/dist/src/helpers/plugins/process-code/index.js +114 -120
  118. package/dist/src/helpers/plugins/process-signals.js +114 -125
  119. package/dist/src/helpers/plugins/process-target-blocks.js +18 -20
  120. package/dist/src/helpers/process-http-requests.js +8 -4
  121. package/dist/src/helpers/remove-surrounding-block.js +2 -2
  122. package/dist/src/helpers/render-imports.js +99 -111
  123. package/dist/src/helpers/replace-identifiers.js +40 -49
  124. package/dist/src/helpers/replace-new-lines-in-strings.js +3 -3
  125. package/dist/src/helpers/signals/signals.js +27 -35
  126. package/dist/src/helpers/slots.js +11 -20
  127. package/dist/src/helpers/state.js +1 -3
  128. package/dist/src/helpers/strip-meta-properties.js +6 -6
  129. package/dist/src/helpers/strip-state-and-props-refs.js +21 -32
  130. package/dist/src/helpers/styles/collect-css.js +39 -44
  131. package/dist/src/helpers/styles/collect-styled-components.js +30 -28
  132. package/dist/src/helpers/styles/helpers.js +23 -29
  133. package/dist/src/helpers/trace-reference-to-module-path.js +3 -4
  134. package/dist/src/helpers/transform-state-setters.js +16 -19
  135. package/dist/src/helpers/traverse-nodes.js +2 -2
  136. package/dist/src/helpers/try-prettier-format.js +3 -3
  137. package/dist/src/helpers/typescript-project.js +25 -25
  138. package/dist/src/helpers/typescript.js +1 -3
  139. package/dist/src/modules/plugins.js +20 -28
  140. package/dist/src/parsers/angular.js +49 -57
  141. package/dist/src/parsers/builder/builder.js +448 -343
  142. package/dist/src/parsers/builder/helpers.js +10 -10
  143. package/dist/src/parsers/context.js +15 -17
  144. package/dist/src/parsers/jsx/ast.js +11 -11
  145. package/dist/src/parsers/jsx/component-types.js +22 -32
  146. package/dist/src/parsers/jsx/context.js +12 -12
  147. package/dist/src/parsers/jsx/element-parser.js +43 -54
  148. package/dist/src/parsers/jsx/exports.js +12 -14
  149. package/dist/src/parsers/jsx/function-parser.js +108 -119
  150. package/dist/src/parsers/jsx/helpers.js +11 -13
  151. package/dist/src/parsers/jsx/hooks/helpers.js +14 -16
  152. package/dist/src/parsers/jsx/hooks/index.js +34 -44
  153. package/dist/src/parsers/jsx/hooks/use-target.js +27 -27
  154. package/dist/src/parsers/jsx/imports.js +7 -17
  155. package/dist/src/parsers/jsx/jsx.js +92 -91
  156. package/dist/src/parsers/jsx/props-types.js +8 -10
  157. package/dist/src/parsers/jsx/props.js +15 -23
  158. package/dist/src/parsers/jsx/signals.js +28 -29
  159. package/dist/src/parsers/jsx/state.d.ts +2 -4
  160. package/dist/src/parsers/jsx/state.js +59 -93
  161. package/dist/src/parsers/svelte/css/index.js +1 -1
  162. package/dist/src/parsers/svelte/helpers/bindings.js +18 -19
  163. package/dist/src/parsers/svelte/helpers/children.js +5 -6
  164. package/dist/src/parsers/svelte/helpers/expressions.js +7 -9
  165. package/dist/src/parsers/svelte/helpers/hooks.js +2 -2
  166. package/dist/src/parsers/svelte/helpers/post-process.js +49 -70
  167. package/dist/src/parsers/svelte/helpers/string.js +5 -5
  168. package/dist/src/parsers/svelte/html/actions.js +17 -10
  169. package/dist/src/parsers/svelte/html/each.js +11 -16
  170. package/dist/src/parsers/svelte/html/element.js +68 -78
  171. package/dist/src/parsers/svelte/html/fragment.js +3 -3
  172. package/dist/src/parsers/svelte/html/if-else.js +11 -18
  173. package/dist/src/parsers/svelte/html/index.js +14 -14
  174. package/dist/src/parsers/svelte/html/mustache-tag.js +5 -5
  175. package/dist/src/parsers/svelte/html/slot.js +5 -5
  176. package/dist/src/parsers/svelte/html/text.js +7 -14
  177. package/dist/src/parsers/svelte/index.js +23 -72
  178. package/dist/src/parsers/svelte/instance/context.js +17 -17
  179. package/dist/src/parsers/svelte/instance/expressions.js +2 -2
  180. package/dist/src/parsers/svelte/instance/functions.js +35 -30
  181. package/dist/src/parsers/svelte/instance/hooks.js +4 -5
  182. package/dist/src/parsers/svelte/instance/imports.js +11 -21
  183. package/dist/src/parsers/svelte/instance/index.js +22 -22
  184. package/dist/src/parsers/svelte/instance/properties.js +11 -23
  185. package/dist/src/parsers/svelte/instance/reactive.js +11 -11
  186. package/dist/src/parsers/svelte/instance/references.js +13 -13
  187. package/dist/src/parsers/svelte/instance/statements.js +3 -3
  188. package/dist/src/parsers/svelte/module/index.js +14 -26
  189. package/dist/src/parsers/svelte/typescript/index.js +27 -36
  190. package/dist/src/plugins/compile-away-builder-components.js +223 -156
  191. package/dist/src/plugins/compile-away-components.js +13 -21
  192. package/dist/src/plugins/map-styles.js +7 -7
  193. package/dist/src/symbols/symbol-processor.js +44 -62
  194. package/dist/src/targets.js +19 -19
  195. package/dist/src/types/mitosis-node.js +1 -1
  196. package/package.json +1 -1
@@ -1,34 +1,22 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.parseProperties = void 0;
15
4
  function parseProperties(json, node) {
16
- var _a;
17
- var _b, _c;
18
- var declarations = (_b = node.declaration) === null || _b === void 0 ? void 0 : _b.declarations;
5
+ var _a, _b;
6
+ const declarations = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.declarations;
19
7
  if (declarations === null || declarations === void 0 ? void 0 : declarations.length) {
20
- var declaration = declarations[0];
21
- var property = declaration.id.name;
22
- var value = (_c = declaration.init) === null || _c === void 0 ? void 0 : _c.value;
23
- var propertyObject = (_a = {},
24
- _a[property] = {
8
+ const declaration = declarations[0];
9
+ const property = declaration.id.name;
10
+ const value = (_b = declaration.init) === null || _b === void 0 ? void 0 : _b.value;
11
+ const propertyObject = {
12
+ [property]: {
25
13
  default: value,
26
14
  },
27
- _a);
28
- json.props = __assign(__assign({}, json.props), propertyObject);
15
+ };
16
+ json.props = { ...json.props, ...propertyObject };
29
17
  json.defaultProps = Object.fromEntries(Object.keys(json.props)
30
- .filter(function (key) { return json.props[key].default; })
31
- .map(function (key) { return [key, json.props[key].default]; }));
18
+ .filter((key) => json.props[key].default)
19
+ .map((key) => [key, json.props[key].default]));
32
20
  }
33
21
  }
34
22
  exports.parseProperties = parseProperties;
@@ -1,22 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseReactive = void 0;
4
- var astring_1 = require("astring");
4
+ const astring_1 = require("astring");
5
5
  function parseReactive(json, node) {
6
- var body = node.body;
7
- var expression = body === null || body === void 0 ? void 0 : body.expression;
6
+ const body = node.body;
7
+ const expression = body === null || body === void 0 ? void 0 : body.expression;
8
8
  if (!expression) {
9
- var wrap = node.body.type !== 'BlockStatement';
10
- var name_1 = "reactive".concat(Object.values(json.state).filter(function (index) { return (index === null || index === void 0 ? void 0 : index.type) === 'getter'; }).length);
11
- json.state[name_1] = {
12
- code: "get ".concat(name_1, "() ").concat(wrap ? '{' : '').concat((0, astring_1.generate)(node.body)).concat(wrap ? '}' : ''),
9
+ const wrap = node.body.type !== 'BlockStatement';
10
+ const name = `reactive${Object.values(json.state).filter((index) => (index === null || index === void 0 ? void 0 : index.type) === 'getter').length}`;
11
+ json.state[name] = {
12
+ code: `get ${name}() ${wrap ? '{' : ''}${(0, astring_1.generate)(node.body)}${wrap ? '}' : ''}`,
13
13
  type: 'getter',
14
14
  };
15
15
  }
16
16
  else if (expression.type === 'AssignmentExpression') {
17
- var name_2 = expression.left.name;
18
- json.state[name_2] = {
19
- code: "get ".concat(name_2, "() {\n return ").concat((0, astring_1.generate)(expression.right), "}"),
17
+ const { name } = expression.left;
18
+ json.state[name] = {
19
+ code: `get ${name}() {\n return ${(0, astring_1.generate)(expression.right)}}`,
20
20
  type: 'getter',
21
21
  };
22
22
  }
@@ -25,7 +25,7 @@ function parseReactive(json, node) {
25
25
  json.hooks.onUpdate = json.hooks.onUpdate || [];
26
26
  json.hooks.onUpdate.push({
27
27
  code: (0, astring_1.generate)(node.body),
28
- deps: "[".concat(expression.arguments.map(function (arg) { return (0, astring_1.generate)(arg); }), "]"),
28
+ deps: `[${expression.arguments.map((arg) => (0, astring_1.generate)(arg))}]`,
29
29
  });
30
30
  }
31
31
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseReferences = exports.getParsedValue = void 0;
4
- var astring_1 = require("astring");
5
- var lodash_1 = require("lodash");
6
- var expressions_1 = require("../helpers/expressions");
4
+ const astring_1 = require("astring");
5
+ const lodash_1 = require("lodash");
6
+ const expressions_1 = require("../helpers/expressions");
7
7
  function getParsedValue(json, element) {
8
8
  switch (element.type) {
9
9
  case 'Identifier': {
@@ -23,23 +23,23 @@ function isPropertyOrStateReference(index) {
23
23
  }
24
24
  function parseReferences(json, node) {
25
25
  var _a, _b, _c;
26
- var declaration = node.declarations[0];
27
- var code;
28
- var type = 'property';
26
+ const declaration = node.declarations[0];
27
+ let code;
28
+ let type = 'property';
29
29
  switch ((_a = declaration === null || declaration === void 0 ? void 0 : declaration.init) === null || _a === void 0 ? void 0 : _a.type) {
30
30
  case 'ArrayExpression': {
31
- code = declaration.init.elements.map(function (element) {
31
+ code = declaration.init.elements.map((element) => {
32
32
  return getParsedValue(json, element);
33
33
  });
34
- if ((0, lodash_1.some)(code, function (c) { return isPropertyOrStateReference(c); })) {
35
- var name_1 = declaration.id.name;
36
- json.state[name_1] = {
37
- code: "get ".concat(name_1, "() { return [").concat(code.map(function (c) {
34
+ if ((0, lodash_1.some)(code, (c) => isPropertyOrStateReference(c))) {
35
+ const name = declaration.id.name;
36
+ json.state[name] = {
37
+ code: `get ${name}() { return [${code.map((c) => {
38
38
  if (isPropertyOrStateReference(c)) {
39
39
  return c;
40
40
  }
41
41
  return JSON.stringify(c);
42
- }), "]}"),
42
+ })}]}`,
43
43
  type: 'getter',
44
44
  };
45
45
  return;
@@ -62,7 +62,7 @@ function parseReferences(json, node) {
62
62
  }
63
63
  json.state[declaration.id.name] = {
64
64
  code: JSON.stringify(code),
65
- type: type,
65
+ type,
66
66
  };
67
67
  }
68
68
  exports.parseReferences = parseReferences;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseStatementAtProgramLevel = void 0;
4
- var astring_1 = require("astring");
5
- var hooks_1 = require("../helpers/hooks");
4
+ const astring_1 = require("astring");
5
+ const hooks_1 = require("../helpers/hooks");
6
6
  function parseStatementAtProgramLevel(json, node) {
7
- var statement = (0, astring_1.generate)(node);
7
+ const statement = (0, astring_1.generate)(node);
8
8
  (0, hooks_1.addToOnInitHook)(json, statement);
9
9
  }
10
10
  exports.parseStatementAtProgramLevel = parseStatementAtProgramLevel;
@@ -1,40 +1,28 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.parseModule = void 0;
15
- var astring_1 = require("astring");
16
- var compiler_1 = require("svelte/compiler");
4
+ const astring_1 = require("astring");
5
+ const compiler_1 = require("svelte/compiler");
17
6
  function handleExportNamedDeclaration(json, node) {
18
- var _a;
19
- var _b, _c, _d;
20
- var declarations = (_b = node.declaration) === null || _b === void 0 ? void 0 : _b.declarations;
7
+ var _a, _b, _c;
8
+ const declarations = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.declarations;
21
9
  if (declarations === null || declarations === void 0 ? void 0 : declarations.length) {
22
- var declaration = declarations[0];
23
- var property = declaration.id.name;
24
- var isFunction = ((_c = declaration.init) === null || _c === void 0 ? void 0 : _c.type) === 'FunctionExpression' ||
25
- ((_d = declaration.init) === null || _d === void 0 ? void 0 : _d.type) === 'ArrowFunctionExpression';
26
- var exportObject = (_a = {},
27
- _a[property] = {
10
+ const declaration = declarations[0];
11
+ const property = declaration.id.name;
12
+ const isFunction = ((_b = declaration.init) === null || _b === void 0 ? void 0 : _b.type) === 'FunctionExpression' ||
13
+ ((_c = declaration.init) === null || _c === void 0 ? void 0 : _c.type) === 'ArrowFunctionExpression';
14
+ const exportObject = {
15
+ [property]: {
28
16
  code: (0, astring_1.generate)(node),
29
- isFunction: isFunction,
17
+ isFunction,
30
18
  },
31
- _a);
32
- json.exports = __assign(__assign({}, json.exports), exportObject);
19
+ };
20
+ json.exports = { ...json.exports, ...exportObject };
33
21
  }
34
22
  }
35
23
  function parseModule(ast, json) {
36
24
  (0, compiler_1.walk)(ast.module, {
37
- enter: function (node) {
25
+ enter(node) {
38
26
  switch (node.type) {
39
27
  case 'ExportNamedDeclaration':
40
28
  handleExportNamedDeclaration(json, node);
@@ -22,35 +22,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
26
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
27
- if (ar || !(i in from)) {
28
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
29
- ar[i] = from[i];
30
- }
31
- }
32
- return to.concat(ar || Array.prototype.slice.call(from));
33
- };
34
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
35
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
27
  };
37
28
  Object.defineProperty(exports, "__esModule", { value: true });
38
29
  exports.collectTypes = exports.parseAttributes = exports.createTagRegex = exports.isTypeScriptComponent = void 0;
39
- var babel = __importStar(require("@babel/core"));
40
- var generator_1 = __importDefault(require("@babel/generator"));
41
- var parser = __importStar(require("@babel/parser"));
42
- var types = __importStar(require("@babel/types"));
43
- var lodash_1 = require("lodash");
30
+ const babel = __importStar(require("@babel/core"));
31
+ const generator_1 = __importDefault(require("@babel/generator"));
32
+ const parser = __importStar(require("@babel/parser"));
33
+ const types = __importStar(require("@babel/types"));
34
+ const lodash_1 = require("lodash");
44
35
  function isTypeScriptComponent(string_) {
45
- var regex = createTagRegex('script', 'gi');
46
- var match = regex.exec(string_);
47
- var lang = parseAttributes(((match === null || match === void 0 ? void 0 : match.length) && match[1]) || '').lang;
36
+ const regex = createTagRegex('script', 'gi');
37
+ const match = regex.exec(string_);
38
+ const { lang } = parseAttributes(((match === null || match === void 0 ? void 0 : match.length) && match[1]) || '');
48
39
  return lang === 'ts';
49
40
  }
50
41
  exports.isTypeScriptComponent = isTypeScriptComponent;
51
42
  /** Create a tag matching regexp. */
52
43
  function createTagRegex(tagName, flags) {
53
- return new RegExp("/<!--[^]*?-->|<".concat(tagName, "(\\s[^]*?)?(?:>([^]*?)<\\/").concat(tagName, ">|\\/>)"), flags);
44
+ return new RegExp(`/<!--[^]*?-->|<${tagName}(\\s[^]*?)?(?:>([^]*?)<\\/${tagName}>|\\/>)`, flags);
54
45
  }
55
46
  exports.createTagRegex = createTagRegex;
56
47
  /** Transform an attribute string into a key-value object */
@@ -58,8 +49,8 @@ function parseAttributes(attributesString) {
58
49
  return attributesString
59
50
  .split(/\s+/)
60
51
  .filter(Boolean)
61
- .reduce(function (accumulator, attribute) {
62
- var _a = attribute.split('='), name = _a[0], value = _a[1];
52
+ .reduce((accumulator, attribute) => {
53
+ const [name, value] = attribute.split('=');
63
54
  // istanbul ignore next
64
55
  accumulator[name] = value ? value.replace(/["']/g, '') : true;
65
56
  return accumulator;
@@ -67,10 +58,10 @@ function parseAttributes(attributesString) {
67
58
  }
68
59
  exports.parseAttributes = parseAttributes;
69
60
  function getScriptContent(markup, module) {
70
- var regex = createTagRegex('script', 'gi');
71
- var match;
61
+ const regex = createTagRegex('script', 'gi');
62
+ let match;
72
63
  while ((match = regex.exec(markup)) !== null) {
73
- var context = parseAttributes(match[1] || '').context;
64
+ const { context } = parseAttributes(match[1] || '');
74
65
  if ((context !== 'module' && !module) || (context === 'module' && module)) {
75
66
  return match[2];
76
67
  }
@@ -79,26 +70,26 @@ function getScriptContent(markup, module) {
79
70
  }
80
71
  function collectTypes(string_, json) {
81
72
  var _a;
82
- var module = getScriptContent(string_, true); // module
83
- var instance = getScriptContent(string_, false); // instance
73
+ const module = getScriptContent(string_, true); // module
74
+ const instance = getScriptContent(string_, false); // instance
84
75
  function traverse(script_) {
85
- var ast = parser.parse(script_, {
76
+ const ast = parser.parse(script_, {
86
77
  sourceType: 'module',
87
78
  plugins: ['typescript'],
88
79
  });
89
80
  babel.traverse(ast, {
90
- enter: function (path) {
81
+ enter(path) {
91
82
  var _a;
92
83
  // alias or interface (e.g. type Props = { } or interface Props {} )
93
84
  if (types.isTSTypeAliasDeclaration(path.node) ||
94
85
  types.isTSInterfaceDeclaration(path.node)) {
95
- json.types = __spreadArray(__spreadArray([], ((_a = json.types) !== null && _a !== void 0 ? _a : []), true), [(0, generator_1.default)(path.node).code], false);
86
+ json.types = [...((_a = json.types) !== null && _a !== void 0 ? _a : []), (0, generator_1.default)(path.node).code];
96
87
  path.skip();
97
88
  }
98
89
  else if (types.isTSTypeAnnotation(path.node)) {
99
90
  // add to actual ref
100
- var reference = (0, generator_1.default)(path.parent).code;
101
- var type = (0, generator_1.default)(path.node.typeAnnotation).code;
91
+ const reference = (0, generator_1.default)(path.parent).code;
92
+ const type = (0, generator_1.default)(path.node.typeAnnotation).code;
102
93
  // add to ref
103
94
  if (Object.prototype.hasOwnProperty.call(json.refs, reference)) {
104
95
  json.refs[reference].typeParameter = type;
@@ -115,15 +106,15 @@ function collectTypes(string_, json) {
115
106
  traverse(module);
116
107
  traverse(instance);
117
108
  // add prop type declaration to json.types and set the propsTypeRef
118
- if ((0, lodash_1.some)(json.props, function (property) { return !!property.type; })) {
119
- var propertyTypeDeclaration = "type Props = {";
120
- propertyTypeDeclaration += Object.keys((0, lodash_1.pickBy)(json.props, function (property) { return !!property.type; }))
121
- .map(function (key) {
122
- return "".concat(key, ": ").concat(json.props[key].type, ";");
109
+ if ((0, lodash_1.some)(json.props, (property) => !!property.type)) {
110
+ let propertyTypeDeclaration = `type Props = {`;
111
+ propertyTypeDeclaration += Object.keys((0, lodash_1.pickBy)(json.props, (property) => !!property.type))
112
+ .map((key) => {
113
+ return `${key}: ${json.props[key].type};`;
123
114
  })
124
115
  .join('\n');
125
116
  propertyTypeDeclaration += '}';
126
- json.types = __spreadArray(__spreadArray([], ((_a = json.types) !== null && _a !== void 0 ? _a : []), true), [propertyTypeDeclaration], false);
117
+ json.types = [...((_a = json.types) !== null && _a !== void 0 ? _a : []), propertyTypeDeclaration];
127
118
  json.propsTypeRef = 'Props';
128
119
  }
129
120
  }