npm install ast.jsxWrite AST by JSX.
``jsonc
// tsconfig.json
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "ast.jsx",
}
}
`
`tsx
// demo.tsx
import g from '@babel/generator';
import * as t from '@babel/types';
export const variableDeclaration = (
attributes={{ kind: 'const' }}
>
attributes={{
id: (
variableDeclaration
),
}}
/>
);
console.log(g(variableDeclaration).code);
// const variableDeclaration = (
//
//
//
// );
function PlainJSXElement(props: {
tag: string;
attributes?: Record
children?: string | t.JSXElement;
}) {
const attributes = Object.entries(props.attributes || {}).map(
([key, value]) => {
return (
{typeof value === 'string' ? (
) : value === true ? null : (
)}
);
}
);
if (typeof props.children === 'string') {
return (
}
closeingElement={
}
>
);
} else if (!props.children) {
return (
}
>
);
} else {
return (
}
closeingElement={
}
>
{props.children}
);
}
}
``