|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- // src/core/index.ts
- import {
- DEFINE_RENDER,
- MagicString,
- babelParse,
- getLang,
- getTransformResult,
- isCallOf,
- isFunctionType,
- walkAST
- } from "@vue-macros/common";
- function transformDefineRender(code, id) {
- if (!code.includes(DEFINE_RENDER))
- return;
- const lang = getLang(id);
- const program = babelParse(code, lang === "vue" ? "js" : lang);
- const nodes = [];
- walkAST(program, {
- enter(node, parent) {
- if (node.type !== "ExpressionStatement" || !isCallOf(node.expression, DEFINE_RENDER) || parent.type !== "BlockStatement")
- return;
- nodes.push({
- parent,
- node,
- arg: node.expression.arguments[0]
- });
- }
- });
- if (nodes.length === 0)
- return;
- const s = new MagicString(code);
- for (const { parent, node, arg } of nodes) {
- const returnStmt = parent.body.find(
- (node2) => node2.type === "ReturnStatement"
- );
- if (returnStmt)
- s.removeNode(returnStmt);
- const index = returnStmt ? returnStmt.start : parent.end - 1;
- const shouldAddFn = !isFunctionType(arg) && arg.type !== "Identifier";
- s.appendLeft(index, `return ${shouldAddFn ? "() => (" : ""}`);
- s.moveNode(arg, index);
- if (shouldAddFn)
- s.appendRight(index, `)`);
- s.remove(node.start, arg.start);
- s.remove(arg.end, node.end);
- }
- return getTransformResult(s, id);
- }
-
- export {
- transformDefineRender
- };
|