UNPKG

@areslabs/alita-core

Version:

alita-core

140 lines (111 loc) 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = checkEntry; var _traverse = _interopRequireDefault(require("@babel/traverse")); var _util = require("./util"); 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 checkEntry(ast, filepath, rawCode) { // 收集所有 import/require 组件 const allModuleVarSet = new Set([]); (0, _traverse.default)(ast, { enter: path => { if (path.type === 'ImportDeclaration') { ; path.node.specifiers.forEach(item => { allModuleVarSet.add(item.local.name); }); } if (path.type === 'CallExpression' // @ts-ignore && path.node.callee.name === 'require' // @ts-ignore && path.node.arguments.length === 1) { const pp = path.parentPath; // @ts-ignore const id = pp.node.id; if (id && id.type === 'Identifier') { allModuleVarSet.add(id.name); } if (id && id.type === 'ObjectPattern') { id.properties.forEach(pro => { if (pro.type === 'Property') { allModuleVarSet.add(pro.value.name); } }); } } }, exit: path => { // @ts-ignore if (path.type === 'JSXOpeningElement' && path.node.name.name === 'Route') { const parentJSX = path.parentPath.parentPath; if (parentJSX.type === 'JSXElement' // @ts-ignore && (parentJSX.node.openingElement.name.name === 'Router' || parentJSX.node.openingElement.name.name === 'TabRouter')) {} else { (0, _util.printError)(filepath, path, rawCode, `入口文件限制:Route标签需要直接放置在 Router/TabRouter 下`); } const pnode = path.node; // @ts-ignore const hasKey = pnode.attributes.some(attr => attr.name.name === 'key'); if (!hasKey) { (0, _util.printError)(filepath, path, rawCode, `Route标签 缺少key属性`); } // @ts-ignore const hasComponent = pnode.attributes.some(attr => attr.name.name === 'component'); if (!hasComponent) { (0, _util.printError)(filepath, path, rawCode, `Route标签 缺少component属性`); } } // @ts-ignore if (path.type === 'JSXOpeningElement' && path.node.name.name === 'TabRouter') { const parentJSX = path.parentPath.parentPath; if (parentJSX.type === 'JSXElement' // @ts-ignore && parentJSX.node.openingElement.name.name === 'Router') {} else { (0, _util.printError)(filepath, path, rawCode, `入口文件限制:TabRouter标签需要直接放置在 TabRouter 下`); } } // @ts-ignore if (path.type === 'JSXAttribute' && path.node.name.name === 'key') { // @ts-ignore const v = path.node.value; if (v.type === 'StringLiteral') return; if (v.type === 'JSXExpressionContainer' && v.expression && v.expression.type === 'StringLiteral') return; (0, _util.printError)(filepath, path, rawCode, `Route标签 key属性需要字符串常量`); } // @ts-ignore if (path.type === 'JSXAttribute' && path.node.name.name === 'component') { // @ts-ignore const v = path.node.value; if (v.type !== 'JSXExpressionContainer') { (0, _util.printError)(filepath, path, rawCode, `Route标签 component属性值需要为组件`); } else { const expre = v.expression; if (!allModuleVarSet.has(expre.name)) { (0, _util.printError)(filepath, path, rawCode, `Route标签 component属性值需要为直接导入的组件`); } } } // @ts-ignore if (path.type === 'JSXAttribute' && path.node.name.name === 'wxNavigationOptions') { // @ts-ignore const v = path.node.value; if (v.type !== 'JSXExpressionContainer') { (0, _util.printError)(filepath, path, rawCode, `Router标签 wxNavigationOptions属性值需要为对象`); } else { const expre = v.expression; expre.properties.forEach(op => { if (!op.value.type.endsWith('Literal')) { (0, _util.printError)(filepath, path, rawCode, `wxNavigationOptions 属性值,值需要是字面量`); } }); } } }, JSXSpreadAttribute: path => { const pp = path.parentPath.node; const name = pp.name.name; if (name === 'Router' || name === 'TabRouter' || name === 'Route') { (0, _util.printError)(filepath, path, rawCode, `Router/TabRouter/Route 不允许使用属性展开操作!`); } } }); }