UNPKG

@areslabs/alita-core

Version:

alita-core

177 lines (139 loc) 6.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = funcCompToClassComp; var t = _interopRequireWildcard(require("@babel/types")); var _uast = require("../util/uast"); var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * Copyright (c) Areslabs. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ //import traverse from "@babel/traverse"; /** * 把函数声明的组件, 转化为FuncComponent 组件 * * 1. export default () => {} * * 2. export default function B () { * return <A/> * } * * 3. export default B = () => { * return <A/> * } * * 4. export default B = function (){ * return <A/> * } * * * * @param ast * @returns {*} */ function funcCompToClassComp(ast, info) { let hasJSXElement = false; let iden = null; let comps = new Set([]); (0, _ErrorLogTraverse.default)(ast, { enter: path => { if (path.type === 'FunctionDeclaration' || path.type === 'ArrowFunctionExpression' || path.type === 'FunctionExpression') { hasJSXElement = false; } if (path.type === 'JSXOpeningElement') { hasJSXElement = true; } }, exit: path => { // 收集所有组件(class & func) if (path.type === 'ClassDeclaration' || path.type === 'ClassExpression') { // @ts-ignore if ((0, _uast.isReactComponent)(path.node.superClass)) { if (path.node.id) { // @ts-ignore comps.add(path.node.id.name); } } return; } // function x {} if (path.type === 'FunctionDeclaration' && path.parentPath.type === 'Program' && hasJSXElement) { // @ts-ignore iden = path.node.id.name; comps.add(iden); path.replaceWith(geneClassDec(path)); } // export function x {} if (path.type === 'FunctionDeclaration' && path.parentPath.type === 'ExportNamedDeclaration' && hasJSXElement) { // @ts-ignore iden = path.node.id.name; comps.add(iden); path.replaceWith(geneClassDec(path)); } // const x = () => {} if (path.type === 'ArrowFunctionExpression' && path.parentPath.type === 'VariableDeclarator' && path.parentPath.parentPath.parentPath.type === 'Program' && hasJSXElement) { // @ts-ignore iden = path.parentPath.node.id.name; comps.add(iden); path.replaceWith(geneClassDec(path)); } // export const x = () => {} if (path.type === 'ArrowFunctionExpression' && path.parentPath.type === 'VariableDeclarator' && path.parentPath.parentPath.parentPath.type === 'ExportNamedDeclaration' && hasJSXElement) { // @ts-ignore iden = path.parentPath.node.id.name; comps.add(iden); path.replaceWith(geneClassDec(path)); } // const b = function (){} if (path.type === 'FunctionExpression' && path.parentPath.type === 'VariableDeclarator' && path.parentPath.parentPath.parentPath.type === 'Program' && hasJSXElement) { // @ts-ignore iden = path.parentPath.node.id.name; comps.add(iden); path.replaceWith(geneClassDec(path)); } // export const b = function (){} if (path.type === 'FunctionExpression' && path.parentPath.type === 'VariableDeclarator' && path.parentPath.parentPath.parentPath.type === 'ExportNamedDeclaration' && hasJSXElement) { // @ts-ignore iden = path.parentPath.node.id.name; comps.add(iden); path.replaceWith(geneClassDec(path)); } // export default () => {} if (path.type === 'ArrowFunctionExpression' && path.parentPath.type === 'ExportDefaultDeclaration' && hasJSXElement) { path.replaceWith(geneClassDec(path)); } // export default function () {} if (path.type === 'FunctionDeclaration' && path.parentPath.type === 'ExportDefaultDeclaration' && hasJSXElement) { path.replaceWith(geneClassDec(path)); } // export default (function () {}) if (path.type === 'FunctionExpression' && path.parentPath.type === 'ExportDefaultDeclaration' && hasJSXElement) { path.replaceWith(geneClassDec(path)); } } }); info.outComp.push(...Array.from(comps)); return ast; } function geneClassDec(path) { const funcPathNode = path.node; if (funcPathNode.body.type !== 'BlockStatement') { funcPathNode.body = t.blockStatement([t.returnStatement(funcPathNode.body)]); } const propsVar = funcPathNode.params[0]; if (propsVar) { const propDec = t.variableDeclaration('const', [t.variableDeclarator(propsVar, t.memberExpression(t.thisExpression(), t.identifier('props')))]); funcPathNode.body.body.unshift(propDec); } const contextVar = funcPathNode.params[1]; if (contextVar) { const contextDec = t.variableDeclaration('const', [t.variableDeclarator(contextVar, t.memberExpression(t.thisExpression(), t.identifier('context')))]); funcPathNode.body.body.unshift(contextDec); } const classDec = t.classExpression( // @ts-ignore path.node.id, t.memberExpression(t.identifier('React'), t.identifier('FuncComponent')), t.classBody([t.classMethod('method', t.identifier('render'), [], funcPathNode.body)])); if (funcPathNode.type === 'FunctionDeclaration') { // @ts-ignore classDec.type = 'ClassDeclaration'; } return classDec; }