UNPKG

@areslabs/alita-core

Version:

alita-core

73 lines (58 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = classNameHandler; var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse")); var t = _interopRequireWildcard(require("@babel/types")); var _constants = require("../constants"); 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. * */ /** * 统一处理节点的类名添加 * @param ast * @param info * @returns {*} */ function classNameHandler(ast, info) { (0, _ErrorLogTraverse.default)(ast, { exit: path => { if (path.type === 'JSXOpeningElement' && path.node.name.name === 'view') { const attris = path.node.attributes; const origs = attris.filter(item => item.type === 'JSXAttribute' && item.name.name === _constants.originElementAttrName); if (origs.length === 0) { return; } const original = origs[0].value.value; if (original === _constants.viewOrigin || original === _constants.touchableOpacityOrigin || original === _constants.touchableHighlightOrigin) { attris.push(t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('view'))); } else if (original === _constants.outerTextOrigin) { attris.push(t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('text'))); } else if (original === _constants.innerTextOrigin) { attris.push(t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('textInner'))); } else if (original === _constants.touchableWithoutFeedbackOrigin) {// do nothing } else if (original === _constants.errorViewOrigin) {// do nothing } else { attris.push(t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('view'))); } } if (path.type === 'JSXOpeningElement' && path.node.name.name === 'image') { const attris = path.node.attributes; const origs = attris.filter(item => item.type === 'JSXAttribute' && item.name.name === _constants.originElementAttrName); if (origs.length === 0) { return; } attris.push(t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('image'))); } } }); return ast; }