UNPKG

@areslabs/alita-core

Version:

alita-core

58 lines (48 loc) 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = compOutElementToBlock; var _uast = require("../util/uast"); var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse")); var _constants = require("../constants"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * 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. * */ /** * 微信小程序自定义节点会退化为 view, 故把render 直接下的view 替换为block,减少组件层级 * @param ast * @param info * @returns {*} */ function compOutElementToBlock(ast, info) { if (info.isPageComp) return ast; (0, _ErrorLogTraverse.default)(ast, { exit: path => { if (path.type === 'JSXOpeningElement' && path.node.name.name === 'view' && (0, _uast.isRenderReturn)(path)) { const original = (0, _uast.getOriginal)(path); /* 没有origial属性的view,可能是直接使用的小程序内置组件,这个时候view可能有其他属性,故无法转化为block 有origial情况下,只有当origial为 innerTextOrigin / outerTextOrigin / viewOrigin 这3种,且无 onXXX 属性,才可以转换为block */ if (original && (original === _constants.viewOrigin || original === _constants.innerTextOrigin || original === _constants.outerTextOrigin) && !hasEventAttri(path)) { path.node.name.name = 'block'; if (path.parentPath.node.closingElement) { path.parentPath.node.closingElement.name.name = 'block'; } } } } }); return ast; } function hasEventAttri(path) { const attris = path.node.attributes; return attris.find(attri => attri.type === 'JSXAttribute' && /^on/.test(attri.name.name)); }