@builder.io/mitosis 0.5.18 → 0.5.19

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.
@@ -1,4 +1,27 @@
1
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
@@ -35,6 +58,7 @@ const traverse_nodes_1 = require("../../helpers/traverse-nodes");
35
58
  const plugins_1 = require("../../modules/plugins");
36
59
  const symbol_processor_1 = require("../../symbols/symbol-processor");
37
60
  const mitosis_node_1 = require("../../types/mitosis-node");
61
+ const babel = __importStar(require("@babel/core"));
38
62
  const function_1 = require("fp-ts/lib/function");
39
63
  const lodash_1 = require("lodash");
40
64
  const legacy_1 = __importDefault(require("neotraverse/legacy"));
@@ -43,6 +67,7 @@ const is_children_1 = __importDefault(require("../../helpers/is-children"));
43
67
  const on_mount_1 = require("../helpers/on-mount");
44
68
  const helpers_2 = require("./helpers");
45
69
  const types_1 = require("./types");
70
+ const { types } = babel;
46
71
  const mappers = {
47
72
  Fragment: (root, json, options, blockOptions) => {
48
73
  return `<ng-container>${json.children
@@ -438,12 +463,31 @@ const traverseToGetAllDynamicComponents = (json, options, blockOptions) => {
438
463
  dynamicTemplate,
439
464
  };
440
465
  };
441
- const processAngularCode = ({ contextVars, outputVars, domRefs, stateVars, replaceWith, }) => (code) => (0, function_1.pipe)((0, strip_state_and_props_refs_1.DO_NOT_USE_VARS_TRANSFORMS)(code, {
466
+ /**
467
+ * Prefixes state identifiers with this.
468
+ * e.g. state.foo --> this.foo
469
+ */
470
+ const prefixState = (code) => {
471
+ return (0, replace_identifiers_1.replaceNodes)({
472
+ code,
473
+ nodeMaps: [
474
+ {
475
+ from: types.identifier('state'),
476
+ to: types.thisExpression(),
477
+ },
478
+ ],
479
+ }).trim();
480
+ };
481
+ const processAngularCode = ({ contextVars, outputVars, domRefs, replaceWith, }) => (code) => (0, function_1.pipe)((0, strip_state_and_props_refs_1.DO_NOT_USE_VARS_TRANSFORMS)(code, {
442
482
  contextVars,
443
483
  domRefs,
444
484
  outputVars,
445
- stateVars,
446
- }), (newCode) => (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, { replaceWith }));
485
+ }),
486
+ /**
487
+ * Only prefix state that is in the Angular class component.
488
+ * Do not prefix state referenced in the template
489
+ */
490
+ replaceWith === 'this' ? prefixState : (x) => x, (newCode) => (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, { replaceWith }));
447
491
  const isASimpleProperty = (code) => {
448
492
  const expressions = ['==', '===', '!=', '!==', '<', '>', '<=', '>='];
449
493
  const invalidChars = ['{', '}', '(', ')', 'typeof'];
@@ -578,7 +622,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
578
622
  // TODO: Why is 'outputs' used here and shouldn't it be typed in packages/core/src/types/metadata.ts
579
623
  const metaOutputVars = (useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.outputs) || [];
580
624
  const outputVars = (0, lodash_1.uniq)([...metaOutputVars, ...(0, get_prop_functions_1.getPropFunctions)(json)]);
581
- const stateVars = Object.keys((json === null || json === void 0 ? void 0 : json.state) || {});
582
625
  const options = (0, merge_options_1.initializeOptions)({
583
626
  target: 'angular',
584
627
  component: _component,
@@ -595,7 +638,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
595
638
  contextVars,
596
639
  outputVars,
597
640
  domRefs: Array.from((0, get_refs_1.getRefs)(json)),
598
- stateVars,
599
641
  }), (code) => {
600
642
  const allMethodNames = Object.entries(json.state)
601
643
  .filter(([_, value]) => (value === null || value === void 0 ? void 0 : value.type) === 'function' || (value === null || value === void 0 ? void 0 : value.type) === 'method')
@@ -731,7 +773,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
731
773
  contextVars,
732
774
  outputVars,
733
775
  domRefs: Array.from(domRefs),
734
- stateVars,
735
776
  })(value);
736
777
  },
737
778
  });
@@ -851,7 +892,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
851
892
  contextVars,
852
893
  outputVars,
853
894
  domRefs: Array.from(domRefs),
854
- stateVars,
855
895
  })(argument)}`
856
896
  : ''};`;
857
897
  })
@@ -5,5 +5,5 @@ declare const types: typeof babel.types;
5
5
  /**
6
6
  * Parses function declarations within the Mitosis copmonent's body to JSON
7
7
  */
8
- export declare const componentFunctionToJson: (node: babel.types.FunctionDeclaration, context: Context) => JSONOrNode;
8
+ export declare const componentFunctionToJson: (node: babel.types.FunctionDeclaration, context: Context, stateToScope: string[]) => JSONOrNode;
9
9
  export {};
@@ -43,7 +43,7 @@ const { types } = babel;
43
43
  /**
44
44
  * Parses function declarations within the Mitosis copmonent's body to JSON
45
45
  */
46
- const componentFunctionToJson = (node, context) => {
46
+ const componentFunctionToJson = (node, context, stateToScope) => {
47
47
  var _a;
48
48
  const hooks = {
49
49
  onMount: [],
@@ -206,6 +206,7 @@ const componentFunctionToJson = (node, context) => {
206
206
  code: (0, generator_1.default)(item).code,
207
207
  type: 'function',
208
208
  };
209
+ stateToScope.push(item.id.name);
209
210
  }
210
211
  }
211
212
  if (types.isVariableDeclaration(item)) {
@@ -247,6 +248,7 @@ const componentFunctionToJson = (node, context) => {
247
248
  propertyType,
248
249
  };
249
250
  }
251
+ stateToScope.push(varName);
250
252
  // Typescript Parameter
251
253
  if (types.isTSTypeParameterInstantiation(init.typeParameters)) {
252
254
  state[varName].typeParameter = (0, generator_1.default)(init.typeParameters.params[0]).code;
@@ -84,6 +84,7 @@ function parseJsx(jsx, _options = {}) {
84
84
  babelrc: false,
85
85
  presets: [typescriptBabelPreset],
86
86
  })) === null || _a === void 0 ? void 0 : _a.code;
87
+ const stateToScope = [];
87
88
  const output = babel.transform(jsxToUse, {
88
89
  configFile: false,
89
90
  babelrc: false,
@@ -145,7 +146,7 @@ function parseJsx(jsx, _options = {}) {
145
146
  };
146
147
  },
147
148
  });
148
- path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context)));
149
+ path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context, stateToScope)));
149
150
  }
150
151
  }
151
152
  },
@@ -189,7 +190,7 @@ function parseJsx(jsx, _options = {}) {
189
190
  .replace(/^\({/, '{')
190
191
  .replace(/}\);$/, '}'));
191
192
  const mitosisComponent = (0, json_1.tryParseJson)(stringifiedMitosisComponent);
192
- (0, state_1.mapStateIdentifiers)(mitosisComponent);
193
+ (0, state_1.mapStateIdentifiers)(mitosisComponent, stateToScope);
193
194
  (0, context_1.extractContextComponents)(mitosisComponent);
194
195
  mitosisComponent.subComponents = subComponentFunctions.map((item) => parseJsx(item, options));
195
196
  const signalTypeImportName = (0, signals_1.getSignalImportName)(jsxToUse);
@@ -5,6 +5,13 @@ import { ObjectExpression } from '@babel/types';
5
5
  * e.g.
6
6
  * text -> state.text
7
7
  * setText(...) -> state.text = ...
8
+ *
9
+ * This also applies to components that use both useState and useStore.
10
+ * e.g.
11
+ * const [foo, setFoo] = useState(1)
12
+ * const store = useStore({
13
+ * bar() { return foo } // becomes bar() { return state.foo }
14
+ * })`
8
15
  */
9
- export declare function mapStateIdentifiers(json: MitosisComponent): void;
16
+ export declare function mapStateIdentifiers(json: MitosisComponent, stateProperties: string[]): void;
10
17
  export declare const parseStateObjectToMitosisState: (object: ObjectExpression, isState?: boolean) => MitosisState;
@@ -97,9 +97,15 @@ const consolidateClassBindings = (item) => {
97
97
  * e.g.
98
98
  * text -> state.text
99
99
  * setText(...) -> state.text = ...
100
+ *
101
+ * This also applies to components that use both useState and useStore.
102
+ * e.g.
103
+ * const [foo, setFoo] = useState(1)
104
+ * const store = useStore({
105
+ * bar() { return foo } // becomes bar() { return state.foo }
106
+ * })`
100
107
  */
101
- function mapStateIdentifiers(json) {
102
- const stateProperties = Object.keys(json.state);
108
+ function mapStateIdentifiers(json, stateProperties) {
103
109
  const plugin = (0, process_code_1.createCodeProcessorPlugin)(() => (code) => mapStateIdentifiersInExpression(code, stateProperties));
104
110
  plugin(json);
105
111
  for (const key in json.targetBlocks) {
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.5.18",
25
+ "version": "0.5.19",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {