UNPKG

@areslabs/alita-core

Version:

alita-core

129 lines (106 loc) 4.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = checkJSX; var _traverse = _interopRequireDefault(require("@babel/traverse")); var _constants = require("../constants"); var _util = require("./util"); var _uast = require("../util/uast"); var _getAndStorecompInfos = require("../util/getAndStorecompInfos"); 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. * */ const supportRNAPI = new Set(['StyleSheet', 'Platform', 'Dimensions', 'Alert', 'PixelRatio', 'AsyncStorage', 'unstable_batchedUpdates']); const notSupportCommonAttris = new Set(['onStartShouldSetResponder', 'onMoveShouldSetResponder', 'onResponderGrant', 'onResponderReject', 'onResponderMove', 'onResponderRelease', 'onResponderTerminationRequest', 'onResponderTerminate']); const notSupportJSXElementAttris = { FlatList: new Set(['ItemSeparatorComponent', 'columnWrapperStyle', 'extraData', 'inverted', 'onViewableItemsChanged', 'progressViewOffset', 'legacyImplementation']), ScrollView: new Set(['onMomentumScrollBegin', 'onMomentumScrollEnd', 'pagingEnabled', 'scrollEnabled']) //TODO 补充。。。 }; /** * 在转化之前,提前check一次代码,并对错误给出友好提示。 * * @param ast * @param filepath * @param rawCode */ function checkJSX(ast, filepath, rawCode) { (0, _traverse.default)(ast, { exit: path => { if (path.type === 'ImportDeclaration' && path.node.source.value === 'react-native') { ; path.node.specifiers.forEach(item => { item = item; const importedName = item.imported.name; const localName = item.local.name; if ((_constants.RNCOMPSET.has(importedName) || _constants.backToViewNode.has(importedName)) && importedName !== localName) { (0, _util.printError)(filepath, path, rawCode, `导入RN组件的时候,不能使用import {xx as yy} 写法`); } }); } // @ts-ignore if (path.type === 'ClassProperty' && path.node.key.name === 'wxNavigationOptions' && path.node.static === true) { // @ts-ignore const v = path.node.value; v.properties.forEach(op => { if (!op.value.type.endsWith('Literal')) { (0, _util.printError)(filepath, path, rawCode, `wxNavigationOptions 属性值,值需要是字面量`); } }); } }, Identifier(path) { if (path.node.name === "h") { (0, _util.printError)(filepath, path, rawCode, `不允许声明/导入 名字为h的变量`); } if (path.node.name === 'HocComponent') { (0, _util.printError)(filepath, path, rawCode, `HOC组件,需要使用React.createElement创建元素`); } }, JSXSpreadAttribute: path => { // @ts-ignore const elementName = path.parentPath.node.name.name; if (_getAndStorecompInfos.allBaseComp.has(elementName) || _constants.backToViewNode.has(elementName)) { (0, _util.printError)(filepath, path, rawCode, `基本组件不支持属性展开`); } }, JSXMemberExpression: path => { if ((0, _uast.isReactFragmentExpression)(path.node)) return; (0, _util.printWarn)(filepath, path, rawCode, `小程序不允许存在<A.B/> 形式`); }, ImportDeclaration: path => { const node = path.node; if (node.source.value !== 'react-native') return; node.specifiers.forEach(item => { if (_constants.backToViewNode.has(item.local.name) || _constants.RNCOMPSET.has(item.local.name) || supportRNAPI.has(item.local.name) || item.local.name === 'RefreshControl' || item.local.name === 'StatusBar') { return; } if (item.local.name === 'Animated') { (0, _util.printWarn)(filepath, path, rawCode, `不支持Animated组件, 需要使用@areslabs/wx-animated库替换`); } if (item.local.name === 'WebView') { (0, _util.printWarn)(filepath, path, rawCode, `小程序webview占满全屏,和RN不同, 避免使用`); } (0, _util.printWarn)(filepath, path, rawCode, `React Native ${item.local.name}尚未支持`); }); }, JSXAttribute: path => { const node = path.node; const name = node.name.name; if (notSupportCommonAttris.has(name)) { (0, _util.printWarn)(filepath, path, rawCode, `不支持${name}属性`); } else { const jop = path.parentPath; // @ts-ignore const elementName = jop.node.name.name; const jsxEleAttr = notSupportJSXElementAttris[elementName]; if (jsxEleAttr && jsxEleAttr.has(name)) { (0, _util.printWarn)(filepath, path, rawCode, `组件${elementName}不支持${name}属性`); } } } }); }