@areslabs/alita-core
Version:
alita-core
77 lines (62 loc) • 2.89 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");
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.
*
*/
/**
* 处理onLayout
*
* <View onLayout={() => {}}/> 转化为: <View class="m-lt" data-parent="{{parentDiuu}}"/>
*
* @param ast
* @param info
* @returns {any}
*/
function _default(ast, info) {
(0, _ErrorLogTraverse.default)(ast, {
exit: path => {
if (path.type === 'JSXOpeningElement') {
const attris = path.node.attributes || [];
let hasLayout = false;
let hasDataDiuu = false;
let diuuValue = "";
path.node.attributes = attris.filter(attr => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'onLayout') {
hasLayout = true;
return false;
}
if (attr.type === 'JSXAttribute' && attr.name.name === 'data-diuu') {
hasDataDiuu = true;
}
if (attr.type === 'JSXAttribute' && attr.name.name === 'diuu') {
diuuValue = attr.value.value;
}
return true;
});
if (!hasLayout) {
// 没有onLayout属性
return;
}
(0, _uast.elementAddClass)(path.node, 'm-lt');
path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('data-parent'), t.stringLiteral(`{{parentDiuu}}`)));
if (!hasDataDiuu) {
path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('data-diuu'), t.stringLiteral(`{{${diuuValue}}}`)));
}
}
}
});
return ast;
}