@builder.io/mitosis 0.0.50-8 → 0.0.54
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/dist/src/__tests__/angular.test.d.ts +1 -0
- package/dist/src/__tests__/angular.test.js +18 -0
- package/dist/src/__tests__/builder.test.d.ts +1 -0
- package/dist/src/__tests__/builder.test.js +235 -0
- package/dist/src/__tests__/context.test.d.ts +1 -0
- package/dist/src/__tests__/context.test.js +35 -0
- package/dist/src/__tests__/data/blocks/onUpdate.raw copy.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/onUpdate.raw copy.jsx +10 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component-with-children.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component-with-children.raw.jsx +15 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component.raw.jsx +12 -0
- package/dist/src/__tests__/data/blocks/slot.raw.d.ts +5 -0
- package/dist/src/__tests__/data/blocks/slot.raw.jsx +6 -0
- package/dist/src/__tests__/data/blocks/styles.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/styles.raw.jsx +10 -0
- package/dist/src/__tests__/html.test.d.ts +1 -0
- package/dist/src/__tests__/html.test.js +18 -0
- package/dist/src/__tests__/liquid.test.d.ts +1 -0
- package/dist/src/__tests__/liquid.test.js +108 -0
- package/dist/src/__tests__/parse-jsx.test.d.ts +1 -0
- package/dist/src/__tests__/parse-jsx.test.js +15 -0
- package/dist/src/__tests__/qwik.directive.test.d.ts +1 -0
- package/dist/src/__tests__/qwik.directive.test.js +107 -0
- package/dist/src/__tests__/qwik.test.d.ts +1 -0
- package/dist/src/__tests__/qwik.test.js +434 -0
- package/dist/src/__tests__/react-native.test.d.ts +1 -0
- package/dist/src/__tests__/react-native.test.js +140 -0
- package/dist/src/__tests__/react.test.d.ts +1 -0
- package/dist/src/__tests__/react.test.js +141 -0
- package/dist/src/__tests__/solid.test.d.ts +1 -0
- package/dist/src/__tests__/solid.test.js +102 -0
- package/dist/src/__tests__/stencil.test.d.ts +1 -0
- package/dist/src/__tests__/stencil.test.js +102 -0
- package/dist/src/__tests__/svelte.test.d.ts +1 -0
- package/dist/src/__tests__/svelte.test.js +36 -0
- package/dist/src/__tests__/vue.test.d.ts +1 -0
- package/dist/src/__tests__/vue.test.js +139 -0
- package/dist/src/__tests__/webcomponent.test.d.ts +1 -0
- package/dist/src/__tests__/webcomponent.test.js +12 -0
- package/dist/src/constants/html_tags.d.ts +1 -0
- package/dist/src/constants/html_tags.js +119 -0
- package/dist/src/generators/angular.js +1 -1
- package/dist/src/generators/context/react copy.d.ts +8 -0
- package/dist/src/generators/context/react copy.js +28 -0
- package/dist/src/generators/context/solid.d.ts +8 -0
- package/dist/src/generators/context/solid.js +28 -0
- package/dist/src/generators/context/svelte.d.ts +8 -0
- package/dist/src/generators/context/svelte.js +28 -0
- package/dist/src/generators/helpers/context.d.ts +2 -0
- package/dist/src/generators/helpers/context.js +8 -0
- package/dist/src/generators/mitosis.js +4 -3
- package/dist/src/generators/qwik/component.js +3 -1
- package/dist/src/generators/qwik/directives.d.ts +14 -5
- package/dist/src/generators/qwik/directives.js +22 -14
- package/dist/src/generators/qwik/jsx.js +14 -2
- package/dist/src/generators/qwik/src-generator.js +47 -24
- package/dist/src/generators/react.js +2 -5
- package/dist/src/generators/solid.js +82 -8
- package/dist/src/generators/stencil/generate.js +1 -1
- package/dist/src/generators/svelte.d.ts +9 -1
- package/dist/src/generators/svelte.js +170 -77
- package/dist/src/generators/vue.js +4 -1
- package/dist/src/helpers/babel-transform copy.d.ts +8 -0
- package/dist/src/helpers/babel-transform copy.js +138 -0
- package/dist/src/helpers/babel-transform.d.ts +4 -7
- package/dist/src/helpers/babel-transform.js +13 -1
- package/dist/src/helpers/babel-transform.test.d.ts +1 -0
- package/dist/src/helpers/babel-transform.test.js +7 -0
- package/dist/src/helpers/generic-format.test.d.ts +1 -0
- package/dist/src/helpers/generic-format.test.js +8 -0
- package/dist/src/helpers/get-state-object-string.js +2 -1
- package/dist/src/helpers/parsers.d.ts +8 -0
- package/dist/src/helpers/parsers.js +66 -0
- package/dist/src/helpers/remove-surrounding-block.test.d.ts +1 -0
- package/dist/src/helpers/remove-surrounding-block.test.js +9 -0
- package/dist/src/helpers/render-imports.d.ts +2 -4
- package/dist/src/helpers/render-imports.js +32 -14
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/parsers/builder.d.ts +0 -2
- package/dist/src/parsers/builder.js +5 -33
- package/dist/src/parsers/jsx.d.ts +7 -0
- package/dist/src/parsers/jsx.js +7 -0
- package/dist/src/symbols/symbol-processor.js +3 -3
- package/dist/src/types/mitosis-component.d.ts +0 -1
- package/dist/test/qwik/Accordion/high.jsx +0 -0
- package/dist/test/qwik/Accordion/low.jsx +207 -0
- package/dist/test/qwik/Accordion/med.jsx +10 -0
- package/dist/test/qwik/For/high.jsx +0 -0
- package/dist/test/qwik/For/low.jsx +33 -0
- package/dist/test/qwik/For/med.jsx +10 -0
- package/dist/test/qwik/Image/high.js +9 -0
- package/dist/test/qwik/Image/low.js +0 -0
- package/dist/test/qwik/Image/med.js +149 -0
- package/dist/test/qwik/Image.slow/high.js +9 -0
- package/dist/test/qwik/Image.slow/low.js +0 -0
- package/dist/test/qwik/Image.slow/med.js +149 -0
- package/dist/test/qwik/bindings/high.cjs +0 -0
- package/dist/test/qwik/bindings/low.cjs +33 -0
- package/dist/test/qwik/bindings/med.cjs +12 -0
- package/dist/test/qwik/button/high.js +3 -0
- package/dist/test/qwik/button/low.js +11 -0
- package/dist/test/qwik/button/med.js +37 -0
- package/dist/test/qwik/component/bindings/high.jsx +0 -0
- package/dist/test/qwik/component/bindings/low.jsx +43 -0
- package/dist/test/qwik/component/bindings/med.jsx +53 -0
- package/dist/test/qwik/component/component/inputs/high.cjsx +9 -0
- package/dist/test/qwik/component/component/inputs/low.cjsx +0 -0
- package/dist/test/qwik/component/component/inputs/med.cjsx +65 -0
- package/dist/test/qwik/hello_world/stylesheet/high.jsx +0 -0
- package/dist/test/qwik/hello_world/stylesheet/low.jsx +12 -0
- package/dist/test/qwik/hello_world/stylesheet/med.jsx +10 -0
- package/dist/test/qwik/page-with-symbol/high.js +0 -0
- package/dist/test/qwik/page-with-symbol/low.js +19 -0
- package/dist/test/qwik/page-with-symbol/med.js +10 -0
- package/dist/test/qwik/show-hide/high.jsx +9 -0
- package/dist/test/qwik/show-hide/low.jsx +0 -0
- package/dist/test/qwik/show-hide/med.jsx +174 -0
- package/dist/test/qwik/svg/high.js +0 -0
- package/dist/test/qwik/svg/low.js +13 -0
- package/dist/test/qwik/svg/med.js +10 -0
- package/dist/test/qwik/todo/Todo.cjs/high.cjs +23 -0
- package/dist/test/qwik/todo/Todo.cjs/low.cjs +0 -0
- package/dist/test/qwik/todo/Todo.cjs/med.cjs +48 -0
- package/dist/test/qwik/todo/Todo.js/high.js +22 -0
- package/dist/test/qwik/todo/Todo.js/low.js +0 -0
- package/dist/test/qwik/todo/Todo.js/med.js +45 -0
- package/dist/test/qwik/todo/Todo.tsx/high.tsx +22 -0
- package/dist/test/qwik/todo/Todo.tsx/low.tsx +0 -0
- package/dist/test/qwik/todo/Todo.tsx/med.tsx +41 -0
- package/dist/test/qwik/todos/Todo.tsx/high.tsx +7 -0
- package/dist/test/qwik/todos/Todo.tsx/low.tsx +27 -0
- package/dist/test/qwik/todos/Todo.tsx/med.tsx +9 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +6 -5
|
@@ -35,17 +35,94 @@ var plugins_1 = require("../modules/plugins");
|
|
|
35
35
|
var is_children_1 = __importDefault(require("../helpers/is-children"));
|
|
36
36
|
var strip_meta_properties_1 = require("../helpers/strip-meta-properties");
|
|
37
37
|
var remove_surrounding_block_1 = require("../helpers/remove-surrounding-block");
|
|
38
|
+
var getters_to_functions_1 = require("../helpers/getters-to-functions");
|
|
39
|
+
var babel_transform_1 = require("../helpers/babel-transform");
|
|
40
|
+
var function_1 = require("fp-ts/lib/function");
|
|
41
|
+
var context_1 = require("./helpers/context");
|
|
42
|
+
var html_tags_1 = require("../constants/html_tags");
|
|
38
43
|
var mappers = {
|
|
39
|
-
Fragment: function (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.
|
|
44
|
+
Fragment: function (_a) {
|
|
45
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
46
|
+
if (json.bindings.innerHTML) {
|
|
47
|
+
return BINDINGS_MAPPER.innerHTML(json, options);
|
|
48
|
+
}
|
|
49
|
+
else if (json.children.length > 0) {
|
|
50
|
+
return "".concat(json.children
|
|
51
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
52
|
+
.join('\n'));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return '';
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
For: function (_a) {
|
|
59
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
60
|
+
return "\n{#each ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.each, {
|
|
61
|
+
includeState: options.stateType === 'variables',
|
|
62
|
+
}), " as ").concat(json.properties._forName, ", index }\n").concat(json.children
|
|
63
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
64
|
+
.join('\n'), "\n{/each}\n");
|
|
65
|
+
},
|
|
66
|
+
Show: function (_a) {
|
|
67
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
68
|
+
return "\n{#if ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.when, {
|
|
69
|
+
includeState: options.stateType === 'variables',
|
|
70
|
+
}), " }\n").concat(json.children
|
|
71
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
72
|
+
.join('\n'), "\n{/if}");
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
var getContextCode = function (json) {
|
|
76
|
+
var contextGetters = json.context.get;
|
|
77
|
+
return Object.keys(contextGetters)
|
|
78
|
+
.map(function (key) { return "let ".concat(key, " = getContext(").concat(contextGetters[key].name, ".key);"); })
|
|
79
|
+
.join('\n');
|
|
80
|
+
};
|
|
81
|
+
var setContextCode = function (json) {
|
|
82
|
+
var contextSetters = json.context.set;
|
|
83
|
+
return Object.keys(contextSetters)
|
|
84
|
+
.map(function (key) {
|
|
85
|
+
var _a = contextSetters[key], value = _a.value, name = _a.name;
|
|
86
|
+
return "setContext(".concat(name, ".key, ").concat(value ? (0, get_state_object_string_1.getMemberObjectString)(value) : 'undefined', ");");
|
|
87
|
+
})
|
|
88
|
+
.join('\n');
|
|
89
|
+
};
|
|
90
|
+
var BINDINGS_MAPPER = {
|
|
91
|
+
innerHTML: function (json, options) {
|
|
92
|
+
return "{@html ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.innerHTML), "}");
|
|
43
93
|
},
|
|
44
94
|
};
|
|
45
|
-
var
|
|
95
|
+
var SVELTE_SPECIAL_TAGS = {
|
|
96
|
+
COMPONENT: 'svelte:component',
|
|
97
|
+
ELEMENT: 'svelte:element',
|
|
98
|
+
SELF: 'svelte:self',
|
|
99
|
+
};
|
|
100
|
+
var getTagName = function (_a) {
|
|
101
|
+
var json = _a.json, parentComponent = _a.parentComponent;
|
|
102
|
+
if (parentComponent && json.name === parentComponent.name) {
|
|
103
|
+
return SVELTE_SPECIAL_TAGS.SELF;
|
|
104
|
+
}
|
|
105
|
+
var isValidHtmlTag = html_tags_1.VALID_HTML_TAGS.includes(json.name);
|
|
106
|
+
var isSpecialSvelteTag = json.name.startsWith('svelte:');
|
|
107
|
+
// Check if any import matches `json.name`
|
|
108
|
+
var hasMatchingImport = parentComponent.imports.some(function (_a) {
|
|
109
|
+
var imports = _a.imports;
|
|
110
|
+
return Object.keys(imports).some(function (name) { return name === json.name; });
|
|
111
|
+
});
|
|
112
|
+
// TO-DO: no way to decide between <svelte:component> and <svelte:element>...need to do that through metadata
|
|
113
|
+
// overrides for now
|
|
114
|
+
if (!isValidHtmlTag && !isSpecialSvelteTag && !hasMatchingImport) {
|
|
115
|
+
json.bindings.this = json.name;
|
|
116
|
+
return SVELTE_SPECIAL_TAGS.COMPONENT;
|
|
117
|
+
}
|
|
118
|
+
return json.name;
|
|
119
|
+
};
|
|
120
|
+
var blockToSvelte = function (_a) {
|
|
121
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
46
122
|
if (mappers[json.name]) {
|
|
47
|
-
return mappers[json.name](json, options);
|
|
123
|
+
return mappers[json.name]({ json: json, options: options, parentComponent: parentComponent });
|
|
48
124
|
}
|
|
125
|
+
var tagName = getTagName({ json: json, parentComponent: parentComponent });
|
|
49
126
|
if ((0, is_children_1.default)(json)) {
|
|
50
127
|
return "<slot></slot>";
|
|
51
128
|
}
|
|
@@ -58,63 +135,58 @@ var blockToSvelte = function (json, options) {
|
|
|
58
135
|
}), "}");
|
|
59
136
|
}
|
|
60
137
|
var str = '';
|
|
61
|
-
|
|
62
|
-
|
|
138
|
+
str += "<".concat(tagName, " ");
|
|
139
|
+
if (json.bindings._spread) {
|
|
140
|
+
str += "{...".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings._spread, {
|
|
63
141
|
includeState: options.stateType === 'variables',
|
|
64
|
-
}), "
|
|
65
|
-
str += json.children.map(function (item) { return (0, exports.blockToSvelte)(item, options); }).join('\n');
|
|
66
|
-
str += "{/each}";
|
|
142
|
+
}), "}");
|
|
67
143
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}), " }");
|
|
72
|
-
str += json.children.map(function (item) { return (0, exports.blockToSvelte)(item, options); }).join('\n');
|
|
73
|
-
str += "{/if}";
|
|
144
|
+
for (var key in json.properties) {
|
|
145
|
+
var value = json.properties[key];
|
|
146
|
+
str += " ".concat(key, "=\"").concat(value, "\" ");
|
|
74
147
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
str += "{...".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings._spread, {
|
|
79
|
-
includeState: options.stateType === 'variables',
|
|
80
|
-
}), "}");
|
|
148
|
+
for (var key in json.bindings) {
|
|
149
|
+
if (key === 'innerHTML') {
|
|
150
|
+
continue;
|
|
81
151
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
str += " ".concat(key, "=\"").concat(value, "\" ");
|
|
152
|
+
if (key === '_spread') {
|
|
153
|
+
continue;
|
|
85
154
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
var
|
|
93
|
-
|
|
94
|
-
});
|
|
95
|
-
if (key.startsWith('on')) {
|
|
96
|
-
var event_1 = key.replace('on', '').toLowerCase();
|
|
97
|
-
// TODO: handle quotes in event handler values
|
|
98
|
-
str += " on:".concat(event_1, "=\"{event => ").concat((0, remove_surrounding_block_1.removeSurroundingBlock)(useValue), "}\" ");
|
|
99
|
-
}
|
|
100
|
-
else if (key === 'ref') {
|
|
101
|
-
str += " bind:this={".concat(useValue, "} ");
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
str += " ".concat(key, "={").concat(useValue, "} ");
|
|
105
|
-
}
|
|
155
|
+
var value = json.bindings[key];
|
|
156
|
+
// TODO: proper babel transform to replace. Util for this
|
|
157
|
+
var useValue = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value, {
|
|
158
|
+
includeState: options.stateType === 'variables',
|
|
159
|
+
});
|
|
160
|
+
if (key.startsWith('on')) {
|
|
161
|
+
var event_1 = key.replace('on', '').toLowerCase();
|
|
162
|
+
// TODO: handle quotes in event handler values
|
|
163
|
+
str += " on:".concat(event_1, "=\"{event => ").concat((0, remove_surrounding_block_1.removeSurroundingBlock)(useValue), "}\" ");
|
|
106
164
|
}
|
|
107
|
-
if (
|
|
108
|
-
|
|
165
|
+
else if (key === 'ref') {
|
|
166
|
+
str += " bind:this={".concat(useValue, "} ");
|
|
109
167
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
str += json.children
|
|
113
|
-
.map(function (item) { return (0, exports.blockToSvelte)(item, options); })
|
|
114
|
-
.join('\n');
|
|
168
|
+
else {
|
|
169
|
+
str += " ".concat(key, "={").concat(useValue, "} ");
|
|
115
170
|
}
|
|
116
|
-
str += "</".concat(json.name, ">");
|
|
117
171
|
}
|
|
172
|
+
// if we have innerHTML, it doesn't matter whether we have closing tags or not, or children or not.
|
|
173
|
+
// we use the innerHTML content as children and don't render the self-closing tag.
|
|
174
|
+
if (json.bindings.innerHTML) {
|
|
175
|
+
str += '>';
|
|
176
|
+
str += BINDINGS_MAPPER.innerHTML(json, options);
|
|
177
|
+
str += "</".concat(tagName, ">");
|
|
178
|
+
return str;
|
|
179
|
+
}
|
|
180
|
+
if (jsx_1.selfClosingTags.has(tagName)) {
|
|
181
|
+
return str + ' />';
|
|
182
|
+
}
|
|
183
|
+
str += '>';
|
|
184
|
+
if (json.children) {
|
|
185
|
+
str += json.children
|
|
186
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
187
|
+
.join('\n');
|
|
188
|
+
}
|
|
189
|
+
str += "</".concat(tagName, ">");
|
|
118
190
|
return str;
|
|
119
191
|
};
|
|
120
192
|
exports.blockToSvelte = blockToSvelte;
|
|
@@ -140,6 +212,12 @@ var useBindValue = function (json, options) {
|
|
|
140
212
|
}
|
|
141
213
|
});
|
|
142
214
|
};
|
|
215
|
+
/**
|
|
216
|
+
* Removes all `this.` references.
|
|
217
|
+
*/
|
|
218
|
+
var stripThisRefs = function (str) {
|
|
219
|
+
return str.replace(/this\.([a-zA-Z_\$0-9]+)/g, '$1');
|
|
220
|
+
};
|
|
143
221
|
var componentToSvelte = function (options) {
|
|
144
222
|
if (options === void 0) { options = {}; }
|
|
145
223
|
return function (_a) {
|
|
@@ -153,12 +231,13 @@ var componentToSvelte = function (options) {
|
|
|
153
231
|
}
|
|
154
232
|
var refs = Array.from((0, get_refs_1.getRefs)(json));
|
|
155
233
|
useBindValue(json, useOptions);
|
|
234
|
+
(0, getters_to_functions_1.gettersToFunctions)(json);
|
|
156
235
|
if (useOptions.plugins) {
|
|
157
236
|
json = (0, plugins_1.runPostJsonPlugins)(json, useOptions.plugins);
|
|
158
237
|
}
|
|
159
238
|
var css = (0, collect_styles_1.collectCss)(json);
|
|
160
239
|
(0, strip_meta_properties_1.stripMetaProperties)(json);
|
|
161
|
-
var dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
240
|
+
var dataString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
162
241
|
data: true,
|
|
163
242
|
functions: false,
|
|
164
243
|
getters: false,
|
|
@@ -169,63 +248,77 @@ var componentToSvelte = function (options) {
|
|
|
169
248
|
includeState: useOptions.stateType === 'variables',
|
|
170
249
|
});
|
|
171
250
|
},
|
|
172
|
-
});
|
|
173
|
-
var getterString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
251
|
+
}), babel_transform_1.babelTransformCode);
|
|
252
|
+
var getterString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
174
253
|
data: false,
|
|
175
254
|
getters: true,
|
|
176
255
|
functions: false,
|
|
177
256
|
format: 'variables',
|
|
178
257
|
keyPrefix: '$: ',
|
|
179
258
|
valueMapper: function (code) {
|
|
180
|
-
return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code
|
|
259
|
+
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code
|
|
181
260
|
.replace(/^get ([a-zA-Z_\$0-9]+)/, '$1 = ')
|
|
182
261
|
.replace(/\)/, ') => '), {
|
|
183
262
|
includeState: useOptions.stateType === 'variables',
|
|
184
|
-
});
|
|
263
|
+
}), stripThisRefs);
|
|
185
264
|
},
|
|
186
|
-
});
|
|
187
|
-
var functionsString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
265
|
+
}), babel_transform_1.babelTransformCode);
|
|
266
|
+
var functionsString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
188
267
|
data: false,
|
|
189
268
|
getters: false,
|
|
190
269
|
functions: true,
|
|
191
270
|
format: 'variables',
|
|
192
271
|
keyPrefix: 'function ',
|
|
193
272
|
valueMapper: function (code) {
|
|
194
|
-
return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
273
|
+
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
195
274
|
includeState: useOptions.stateType === 'variables',
|
|
196
|
-
});
|
|
275
|
+
}), stripThisRefs);
|
|
197
276
|
},
|
|
198
|
-
});
|
|
277
|
+
}), babel_transform_1.babelTransformCode);
|
|
199
278
|
var hasData = dataString.length > 4;
|
|
200
279
|
var props = Array.from((0, get_props_1.getProps)(json));
|
|
201
|
-
var
|
|
280
|
+
var transformHookCode = function (hookCode) {
|
|
281
|
+
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hookCode, {
|
|
282
|
+
includeState: useOptions.stateType === 'variables',
|
|
283
|
+
}), babel_transform_1.babelTransformCode);
|
|
284
|
+
};
|
|
285
|
+
var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n <script>\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n\n ", "\n "], ["\n <script>\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n\n ", "\n "])), !((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? '' : "import { onMount } from 'svelte'", !((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length)
|
|
202
286
|
? ''
|
|
203
|
-
: "import { afterUpdate } from 'svelte'", !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "import { onDestroy } from 'svelte'", (0, render_imports_1.renderPreComponent)(json),
|
|
287
|
+
: "import { afterUpdate } from 'svelte'", !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "import { onDestroy } from 'svelte'", (0, render_imports_1.renderPreComponent)(json, 'svelte'), (0, context_1.hasContext)(component)
|
|
288
|
+
? 'import { getContext, setContext } from "svelte";'
|
|
289
|
+
: '', !hasData || useOptions.stateType === 'variables'
|
|
204
290
|
? ''
|
|
205
291
|
: "import onChange from 'on-change'", refs
|
|
206
292
|
.concat(props)
|
|
207
|
-
.map(function (name) {
|
|
208
|
-
|
|
293
|
+
.map(function (name) {
|
|
294
|
+
if (name === 'children') {
|
|
295
|
+
return '';
|
|
296
|
+
}
|
|
297
|
+
return "export let ".concat(name, ";");
|
|
298
|
+
})
|
|
299
|
+
.join('\n'), functionsString.length < 4 ? '' : functionsString, getterString.length < 4 ? '' : getterString, getContextCode(json), setContextCode(json), useOptions.stateType === 'proxies'
|
|
209
300
|
? dataString.length < 4
|
|
210
301
|
? ''
|
|
211
302
|
: "let state = onChange(".concat(dataString, ", () => state = state)")
|
|
212
303
|
: dataString, !((_e = json.hooks.onMount) === null || _e === void 0 ? void 0 : _e.code)
|
|
213
304
|
? ''
|
|
214
|
-
: "onMount(() => { ".concat((
|
|
215
|
-
includeState: useOptions.stateType === 'variables',
|
|
216
|
-
}), " });"), !((_f = json.hooks.onUpdate) === null || _f === void 0 ? void 0 : _f.length)
|
|
305
|
+
: "onMount(() => { ".concat(transformHookCode(json.hooks.onMount.code), " });"), !((_f = json.hooks.onUpdate) === null || _f === void 0 ? void 0 : _f.length)
|
|
217
306
|
? ''
|
|
218
307
|
: json.hooks.onUpdate
|
|
219
308
|
.map(function (hook) {
|
|
220
|
-
return "afterUpdate(() => { ".concat((
|
|
221
|
-
includeState: useOptions.stateType === 'variables',
|
|
222
|
-
}), " })");
|
|
309
|
+
return "afterUpdate(() => { ".concat(transformHookCode(hook.code), " })");
|
|
223
310
|
})
|
|
224
311
|
.join(';'), !((_g = json.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code)
|
|
225
312
|
? ''
|
|
226
|
-
: "onDestroy(() => { ".concat((
|
|
227
|
-
|
|
228
|
-
|
|
313
|
+
: "onDestroy(() => { ".concat(transformHookCode(json.hooks.onUnMount.code), " });"), json.children
|
|
314
|
+
.map(function (item) {
|
|
315
|
+
return (0, exports.blockToSvelte)({
|
|
316
|
+
json: item,
|
|
317
|
+
options: useOptions,
|
|
318
|
+
parentComponent: json,
|
|
319
|
+
});
|
|
320
|
+
})
|
|
321
|
+
.join('\n'), !css.trim().length
|
|
229
322
|
? ''
|
|
230
323
|
: "<style>\n ".concat(css, "\n </style>"));
|
|
231
324
|
if (useOptions.plugins) {
|
|
@@ -266,7 +266,10 @@ var onUpdatePlugin = function (options) { return ({
|
|
|
266
266
|
.filter(function (hook) { var _a; return (_a = hook.deps) === null || _a === void 0 ? void 0 : _a.length; })
|
|
267
267
|
.forEach(function (hook, index) {
|
|
268
268
|
var _a;
|
|
269
|
-
component.state[getOnUpdateHookName(index)] = "".concat(method_literal_prefix_1.methodLiteralPrefix, "get ").concat(getOnUpdateHookName(index), " () {\n return
|
|
269
|
+
component.state[getOnUpdateHookName(index)] = "".concat(method_literal_prefix_1.methodLiteralPrefix, "get ").concat(getOnUpdateHookName(index), " () {\n return {\n ").concat((_a = hook.deps) === null || _a === void 0 ? void 0 : _a.slice(1, -1).split(',').map(function (dep, k) {
|
|
270
|
+
var val = dep.trim();
|
|
271
|
+
return "".concat(k, ": ").concat(val);
|
|
272
|
+
}).join(','), "\n }\n }");
|
|
270
273
|
});
|
|
271
274
|
}
|
|
272
275
|
},
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as babel from '@babel/core';
|
|
2
|
+
declare type Visitor<ContextType = any> = {
|
|
3
|
+
[key: string]: (path: any, context: ContextType) => void;
|
|
4
|
+
};
|
|
5
|
+
export declare const babelTransform: <VisitorContextType = any>(code: string, visitor?: Visitor<VisitorContextType> | undefined) => babel.BabelFileResult | null;
|
|
6
|
+
export declare const babelTransformCode: <VisitorContextType = any>(code: string, visitor?: Visitor<VisitorContextType> | undefined) => string;
|
|
7
|
+
export declare const babelTransformExpression: <VisitorContextType = any>(code: string, visitor: Visitor<VisitorContextType>, type?: 'expression' | 'unknown' | 'block' | 'functionBody') => string;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
};
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.babelTransformExpression = exports.babelTransformCode = exports.babelTransform = void 0;
|
|
36
|
+
var babel = __importStar(require("@babel/core"));
|
|
37
|
+
var lodash_1 = require("lodash");
|
|
38
|
+
var jsxPlugin = require('@babel/plugin-syntax-jsx');
|
|
39
|
+
var tsPreset = require('@babel/preset-typescript');
|
|
40
|
+
var decorators = require('@babel/plugin-syntax-decorators');
|
|
41
|
+
var babelTransform = function (code, visitor) {
|
|
42
|
+
return babel.transform(code, {
|
|
43
|
+
sourceFileName: 'file.tsx',
|
|
44
|
+
configFile: false,
|
|
45
|
+
babelrc: false,
|
|
46
|
+
presets: [[tsPreset, { isTSX: true, allExtensions: true }]],
|
|
47
|
+
plugins: __spreadArray([
|
|
48
|
+
[decorators, { legacy: true }],
|
|
49
|
+
jsxPlugin
|
|
50
|
+
], (visitor ? [function () { return ({ visitor: visitor }); }] : []), true),
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
exports.babelTransform = babelTransform;
|
|
54
|
+
var babelTransformCode = function (code, visitor) {
|
|
55
|
+
var _a;
|
|
56
|
+
return ((_a = (0, exports.babelTransform)(code, visitor)) === null || _a === void 0 ? void 0 : _a.code) || '';
|
|
57
|
+
};
|
|
58
|
+
exports.babelTransformCode = babelTransformCode;
|
|
59
|
+
var babelTransformExpression = function (code, visitor, type) {
|
|
60
|
+
var _a;
|
|
61
|
+
if (type === void 0) { type = 'unknown'; }
|
|
62
|
+
if (!code) {
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
// match for object literal like { foo: ... }
|
|
66
|
+
if (type === 'unknown' && code.trim().match(/^\s*{\s*[a-z0-9]+:/i)) {
|
|
67
|
+
type = 'expression';
|
|
68
|
+
}
|
|
69
|
+
// For Builder content
|
|
70
|
+
if (type === 'unknown' &&
|
|
71
|
+
(code.includes('return _virtual_index') ||
|
|
72
|
+
code.trim().startsWith('return ')) &&
|
|
73
|
+
!code.trim().startsWith('function')) {
|
|
74
|
+
type = 'functionBody';
|
|
75
|
+
}
|
|
76
|
+
var useCode = code;
|
|
77
|
+
if (type === 'functionBody') {
|
|
78
|
+
useCode = "function(){".concat(useCode, "}");
|
|
79
|
+
}
|
|
80
|
+
var result = type === 'expression'
|
|
81
|
+
? null
|
|
82
|
+
: (0, lodash_1.attempt)(function () {
|
|
83
|
+
var _a;
|
|
84
|
+
var result = ((_a = (0, exports.babelTransform)(useCode, visitor)) === null || _a === void 0 ? void 0 : _a.code) || '';
|
|
85
|
+
if (type === 'functionBody') {
|
|
86
|
+
return result.replace(/^function\(\)\{/, '').replace(/\};$/, '');
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// Babel addes trailing semicolons, but for expressions we need those gone
|
|
90
|
+
// TODO: maybe detect if the original code ended with one, and keep it if so, for the case
|
|
91
|
+
// of appending several fragements
|
|
92
|
+
return result.replace(/;$/, '');
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
if ((0, lodash_1.isError)(result) || type === 'expression') {
|
|
96
|
+
try {
|
|
97
|
+
// If it can't, e.g. this is an expression or code fragment, modify the code below and try again
|
|
98
|
+
// Detect method fragments. These get passed sometimes and otherwise
|
|
99
|
+
// generate compile errors. They are of the form `foo() { ... }`
|
|
100
|
+
var isMethod = Boolean(!code.startsWith('function') &&
|
|
101
|
+
code.match(/^[a-z0-9]+\s*\([^\)]*\)\s*[\{:]/i));
|
|
102
|
+
if (isMethod) {
|
|
103
|
+
useCode = "function ".concat(useCode);
|
|
104
|
+
}
|
|
105
|
+
// Parse the code as an expression (instead of the default, a block) by giving it a fake variable assignment
|
|
106
|
+
// e.g. if the code parsed is { ... } babel will treat that as a block by deafult, unless processed as an expression
|
|
107
|
+
// that is an object
|
|
108
|
+
useCode = "let _ = ".concat(useCode);
|
|
109
|
+
result = (((_a = (0, exports.babelTransform)(useCode, visitor)) === null || _a === void 0 ? void 0 : _a.code) || '')
|
|
110
|
+
// Babel addes trailing semicolons, but for expressions we need those gone
|
|
111
|
+
.replace(/;$/, '')
|
|
112
|
+
// Remove our fake variable assignment
|
|
113
|
+
.replace(/let _ =\s/, '');
|
|
114
|
+
if (isMethod) {
|
|
115
|
+
result = result.replace('function', '');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
console.error('Error parsing code:\n', code, '\n', result);
|
|
120
|
+
try {
|
|
121
|
+
return (0, exports.babelTransformExpression)(code, visitor, 'functionBody');
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
throw err;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (type === 'functionBody') {
|
|
129
|
+
return result.replace(/^function\s*\(\)\s*\{/, '').replace(/\};?$/, '');
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
// Babel addes trailing semicolons, but for expressions we need those gone
|
|
133
|
+
// TODO: maybe detect if the original code ended with one, and keep it if so, for the case
|
|
134
|
+
// of appending several fragements
|
|
135
|
+
return result.replace(/;$/, '');
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
exports.babelTransformExpression = babelTransformExpression;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import * as babel from '@babel/core';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const babelTransformCode: <VisitorContextType = any>(code: string, visitor: Visitor<VisitorContextType>) => string;
|
|
7
|
-
export declare const babelTransformExpression: <VisitorContextType = any>(code: string, visitor: Visitor<VisitorContextType>, type?: 'expression' | 'unknown' | 'block' | 'functionBody') => string;
|
|
8
|
-
export {};
|
|
2
|
+
import type { Visitor } from '@babel/traverse';
|
|
3
|
+
export declare const babelTransform: <VisitorContextType = any>(code: string, visitor?: babel.Visitor<VisitorContextType> | undefined) => babel.BabelFileResult | null;
|
|
4
|
+
export declare const babelTransformCode: <VisitorContextType = any>(code: string, visitor?: babel.Visitor<VisitorContextType> | undefined) => string;
|
|
5
|
+
export declare const babelTransformExpression: <VisitorContextType = any>(code: string, visitor: babel.Visitor<VisitorContextType>, type?: 'expression' | 'unknown' | 'block' | 'functionBody') => string;
|
|
@@ -22,6 +22,15 @@ 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
|
+
};
|
|
25
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
35
|
exports.babelTransformExpression = exports.babelTransformCode = exports.babelTransform = void 0;
|
|
27
36
|
var babel = __importStar(require("@babel/core"));
|
|
@@ -35,7 +44,10 @@ var babelTransform = function (code, visitor) {
|
|
|
35
44
|
configFile: false,
|
|
36
45
|
babelrc: false,
|
|
37
46
|
presets: [[tsPreset, { isTSX: true, allExtensions: true }]],
|
|
38
|
-
plugins: [
|
|
47
|
+
plugins: __spreadArray([
|
|
48
|
+
[decorators, { legacy: true }],
|
|
49
|
+
jsxPlugin
|
|
50
|
+
], (visitor ? [function () { return ({ visitor: visitor }); }] : []), true),
|
|
39
51
|
});
|
|
40
52
|
};
|
|
41
53
|
exports.babelTransform = babelTransform;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var babel_transform_1 = require("./babel-transform");
|
|
4
|
+
test('babelTransform', function () {
|
|
5
|
+
var code = "\nconst symbol = symbol;\n\nif (symbol) {\n getContent({\n apiKey: builderContext.apiKey!,\n }).then(response => {\n content = response;\n });\n}\n";
|
|
6
|
+
expect((0, babel_transform_1.babelTransformCode)(code)).toMatchSnapshot();
|
|
7
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var globals_1 = require("@jest/globals");
|
|
4
|
+
var generic_format_1 = require("./generic-format");
|
|
5
|
+
(0, globals_1.test)('Can generic format sloppy Swift code', function () {
|
|
6
|
+
var code = "\n import SwiftUI\n import JavaScriptCore\n \n struct MyComponent: View {\n \n @State private var updateIndex = 0\n private var context = JSContext()\n \n func eval(expression: String) -> JSValue! {\n return context?.evaluateScript(expression)\n }\n \n init() {\n let jsSource = \"\"\"\n const state = { name: \"Steve\" };\n \n \"\"\"\n context?.evaluateScript(jsSource)\n }\n \n \n var body: some View {\n VStack {\n Text(String(updateIndex)).hidden()\n VStack(){\n Foo(\n bar: baz\n )\n TextField(){}\n Text(\"Hello\")\n Text(eval(expression: \"\"\"state.name\"\"\"))\n Text(\"! I can run in React, Vue, Solid, or Liquid!\")}\n }\n }\n }\n ";
|
|
7
|
+
(0, globals_1.expect)((0, generic_format_1.format)(code)).toMatchSnapshot();
|
|
8
|
+
});
|
|
@@ -71,6 +71,7 @@ var getMemberObjectString = function (object, options) {
|
|
|
71
71
|
exports.getMemberObjectString = getMemberObjectString;
|
|
72
72
|
var getStateObjectStringFromComponent = function (component, options) {
|
|
73
73
|
if (options === void 0) { options = {}; }
|
|
74
|
-
|
|
74
|
+
var stateObjectStr = (0, exports.getMemberObjectString)(component.state, options);
|
|
75
|
+
return stateObjectStr;
|
|
75
76
|
};
|
|
76
77
|
exports.getStateObjectStringFromComponent = getStateObjectStringFromComponent;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as babel from '@babel/core';
|
|
2
|
+
export declare function parseCode(code: string): babel.types.Statement[];
|
|
3
|
+
export declare const isCodeBodyExpression: (body: babel.types.Statement[]) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Returns `true` if the `code` is a valid expression. (vs a statement)
|
|
6
|
+
*/
|
|
7
|
+
export declare function isExpression(code: string): boolean;
|
|
8
|
+
export declare const isCodeBodyIdentifier: (body: babel.types.Statement[]) => boolean;
|