@areslabs/alita-core
Version:
alita-core
79 lines (61 loc) • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = addTempName;
var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse"));
var t = _interopRequireWildcard(require("@babel/types"));
var _uast = require("../util/uast");
var _util = require("../util/util");
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; }
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.
*
*/
function addTempName(ast, info) {
let tempNameCount = (0, _util.geneOrder)();
let diuuCount = (0, _util.geneOrder)('_'); // _的传递,防止是 "diuu + 属性" 和 "diuu自增" 重复
let datakeyCount = (0, _util.geneOrder)();
(0, _ErrorLogTraverse.default)(ast, {
exit: path => {
if (path.type === 'JSXElement' && (!(0, _uast.isJSXChild)(path) || (0, _uast.isChildCompChild)(path, info.filepath))) {
const jsxOp = path.node.openingElement;
const tempName = tempNameCount.next;
jsxOp.attributes.push(t.jsxAttribute(t.jsxIdentifier('tempName'), t.stringLiteral(tempName)));
} // 如果只使用一个child 小程序会报递归, 然后就不渲染了
if (path.type === 'JSXElement' && !(0, _uast.isChildComp)(path.node.openingElement.name.name, info.filepath)) {
const children = path.node.children;
let tempName = null;
const isTextEle = (0, _uast.isTextElement)(path.node.openingElement);
path.node.children = children.map(ele => {
if (ele.type === 'JSXExpressionContainer') {
if (!tempName) {
tempName = tempNameCount.next;
}
const datakey = datakeyCount.next;
const templateAttris = [t.jsxAttribute(t.jsxIdentifier('datakey'), t.stringLiteral(datakey)), t.jsxAttribute(t.jsxIdentifier('tempVnode'), ele), t.jsxAttribute(t.jsxIdentifier('wx:if'), t.stringLiteral(`{{${datakey}}} !== undefined`)), t.jsxAttribute(t.jsxIdentifier('is'), t.stringLiteral(tempName)), t.jsxAttribute(t.jsxIdentifier('data'), t.stringLiteral(`{{d: ${datakey}}}`))];
if (isTextEle) {
templateAttris.push(t.jsxAttribute(t.jsxIdentifier('isTextElement')));
}
return t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier('template'), templateAttris), t.jsxClosingElement(t.jsxIdentifier('template')), [], true);
}
return ele;
});
if (tempName && !isTextEle) {
info.childTemplates.push(tempName);
}
}
if (path.type === 'JSXOpeningElement') {
const jsxOp = path.node;
const key = diuuCount.next;
jsxOp.attributes.push(t.jsxAttribute(t.jsxIdentifier('diuu'), t.stringLiteral(key)));
}
}
});
return ast;
}