@areslabs/alita-core
Version:
alita-core
59 lines (47 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var t = _interopRequireWildcard(require("@babel/types"));
var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse"));
var _uast = require("../util/uast");
var _constants = require("../constants");
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.
*
*/
/**
* 处理 <> xxx </> 和 <React.Fragment> xxx </React.Fragment>
*
* 上述写法转化为: <block> xxx </block>
*
* @param ast
* @param info
* @returns {any}
*/
function _default(ast, info) {
(0, _ErrorLogTraverse.default)(ast, {
exit: path => {
if (path.type === 'JSXFragment') {
replaceElement(path);
}
if (path.type === 'JSXElement') {
if ((0, _uast.isReactFragment)(path.node.openingElement) && (0, _uast.isReactFragment)(path.node.closingElement)) {
replaceElement(path);
}
}
}
});
return ast;
}
function replaceElement(path) {
let newEle = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier('block'), [t.jsxAttribute(t.jsxIdentifier(_constants.reactFragmentFlag), t.jsxExpressionContainer(t.booleanLiteral(true)))]), t.jsxClosingElement(t.jsxIdentifier('block')), path.node.children, true);
path.replaceWith(newEle);
}