MCPcopy
hub / github.com/BuilderIO/mitosis / parseJsx

Function parseJsx

packages/core/src/parsers/jsx/jsx.ts:48–246  ·  view source on GitHub ↗
(
  jsx: string,
  _options: Partial<ParseMitosisOptions> = {},
)

Source from the content-addressed store, hash-verified

46 * @returns A JSON representation of the Mitosis component
47 */
48export function parseJsx(
49 jsx: string,
50 _options: Partial<ParseMitosisOptions> = {},
51): MitosisComponent {
52 let subComponentFunctions: string[] = [];
53
54 const options: ParseMitosisOptions = {
55 typescript: false,
56 ..._options,
57 };
58
59 const stateToScope: string[] = [];
60
61 const jsxToUse = babelStripTypes(jsx, !options.typescript);
62
63 const output = babelDefaultTransform(jsxToUse, {
64 JSXExpressionContainer(path, context) {
65 if (types.isJSXEmptyExpression(path.node.expression)) {
66 path.remove();
67 }
68 },
69 Program(path, context) {
70 if (context.builder) {
71 return;
72 }
73
74 beforeParse(path);
75
76 context.builder = {
77 component: createMitosisComponent(),
78 };
79
80 const keepStatements = path.node.body.filter(
81 (statement) => isImportOrDefaultExport(statement) || isTypeOrInterface(statement),
82 );
83
84 context.builder.component.exports = generateExports(path);
85
86 subComponentFunctions = path.node.body
87 .filter(
88 (node) => !types.isExportDefaultDeclaration(node) && types.isFunctionDeclaration(node),
89 )
90 .map((node) => `export default ${generate(node).code!}`);
91
92 const preComponentCode = pipe(
93 path,
94 collectModuleScopeHooks(context, options),
95 types.program,
96 generate,
97 (generatorResult) => generatorResult.code,
98 );
99
100 // TODO: support multiple? e.g. for others to add imports?
101 context.builder.component.hooks.preComponent = { code: preComponentCode };
102
103 path.replaceWith(types.program(keepStatements));
104 },
105 FunctionDeclaration(path, context) {

Callers 15

getHooksFunction · 0.90
builder.test.tsFile · 0.90
parseNodeFunction · 0.90
parseNodesFunction · 0.90
qwik.test.tsFile · 0.90
styles.test.tsFile · 0.90
parse-jsx.test.tsFile · 0.90
context.test.tsFile · 0.90
runTestsForJsxFunction · 0.90
runTestsForTargetFunction · 0.90
symbols.test.tsFile · 0.90

Calls 10

babelStripTypesFunction · 0.90
babelDefaultTransformFunction · 0.90
stripNewlinesInStringsFunction · 0.90
tryParseJsonFunction · 0.90
mapStateIdentifiersFunction · 0.90
extractContextComponentsFunction · 0.90
getSignalImportNameFunction · 0.90
findOptionalPropsFunction · 0.90
findSignalsFunction · 0.90
traverseNodesFunction · 0.90

Tested by 2

runTestsForJsxFunction · 0.72
runTestsForTargetFunction · 0.72