UNPKG

rapplus

Version:
152 lines (151 loc) 6.94 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; exports.__esModule = true; exports.requestFileParse = void 0; // const path = require('path'); var path = require("path"); var ts = require("typescript"); var utils_1 = require("./../../utils"); function isNodeExported(node) { return ((ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0 || (!!node.parent && node.parent.kind === ts.SyntaxKind.SourceFile)); } function getRealPath(importPath, alias) { for (var item in alias) { var aliasReg = new RegExp("^" + item); if (aliasReg.test(importPath)) { return importPath.replace(aliasReg, alias[item]); } } return null; } function getAllLeadingComments(node, sourceFile) { var allRanges = []; var nodeText = node.getFullText(sourceFile); var cr = ts.getLeadingCommentRanges(nodeText, 0); if (cr) allRanges.push.apply(allRanges, cr.map(function (c) { return (__assign(__assign({}, c), { text: nodeText.substring(c.pos, c.end) })); })); var synthetic = ts.getSyntheticLeadingComments(node); if (synthetic) allRanges.push.apply(allRanges, synthetic); return allRanges; } function requestFileParse(file, formatFunc, config) { var filePath = file.path, content = file.content; var sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest); // const sourceFile = program.getSourceFile(filePath); // console.log(sourceFile.getText(sourceFile), 'sourceFile') // 引入的 TypeOnly 文件 // 文件名称 文件路径 var importTypes = []; // 导出的 接口函数 var exportInterfaceFunc = []; var importType = { importNames: [], importPath: '' }; // 当前文件不能放到 importType 中, 一个文件可能会有多个 文件导入的类型 var fileInfo = { filePath: filePath, fileName: path.basename(filePath).replace(/\.[a-z]+$/, ''), content: content, moduleId: utils_1.getRapModuleId(content) }; ts.forEachChild(sourceFile, function (node) { var _a, _b; if (ts.isImportDeclaration(node) && node.importClause.isTypeOnly) { // 只是查了 isNamedImports 还有一种 nameSpaceImport if (ts.isNamedImports(node.importClause.namedBindings)) { node.importClause.namedBindings.forEachChild(function (node) { // console.log(node.getText(sourceFile)); importType.importNames.push(node.getText(sourceFile)); }); var importPath = node.moduleSpecifier.getText(sourceFile).replace(/["']/g, ''); var absolutePath = path.resolve(__dirname, filePath, '..', importPath); if (config.upload.alias) { var realPath = getRealPath(importPath, config.upload.alias); if (realPath) { absolutePath = realPath; } } node.moduleSpecifier.getText(sourceFile); importType.importPath = /ts$/.test(absolutePath) ? absolutePath : absolutePath + '.ts'; node.moduleSpecifier.getText(sourceFile); } importTypes.push(importType); } // 导出的 函数定义 if (ts.isFunctionDeclaration(node) && isNodeExported(node)) { var comment_1 = ''; node.getChildren(sourceFile).forEach(function (el) { if (ts.isJSDoc(el)) { // ts.JSDoc.comment?: string | ts.NodeArray<ts.JSDocText | ts.JSDocLink> if (typeof el.comment == 'string') { comment_1 = el.comment; } } }); exportInterfaceFunc.push({ funcName: node.name.getText(sourceFile), comment: comment_1, body: node.getText(sourceFile), funcType: 'FunctionDeclaration' }); return; } // 导出的 变量列表 if (ts.isVariableStatement(node) && isNodeExported(node)) { var comments = getAllLeadingComments(node, sourceFile) || []; var comment_2 = (_b = (_a = comments[comments.length - 1]) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : ''; node.getChildren(sourceFile).forEach(function (el) { if (ts.isVariableDeclarationList(el)) { // console.log(el, '=============='); el.forEachChild(function (declarationNode) { // 不知道为啥没有推断出出来是 declarationNode 还要在收窄一次类型 if (!ts.isVariableDeclaration(declarationNode)) return; var funcName = declarationNode.name.getText(sourceFile); declarationNode.forEachChild(function (functionNode) { // 函数调用 var isCallExpression = ts.isCallExpression(functionNode); // isCallExpression 判断不出来 - 导出的是不是一个函数 // 要去找到这个函数定义的地方 可是函数可以定义在任何地方 if (isCallExpression || ts.isArrowFunction(functionNode)) { exportInterfaceFunc.push({ funcName: funcName, comment: comment_2, body: node.getText(sourceFile), funcType: isCallExpression ? 'CallExpression' : 'ArrowFunction' }); } }); }); } }); } }); var funcTypes = exportInterfaceFunc.map(formatFunc).map(function (data, index) { // 把 body 重新赋值上去,放在内部 避免 config 设置无用字段 if (data) { var _a = exportInterfaceFunc[index], body = _a.body, funcName = _a.funcName, comment = _a.comment; return __assign(__assign({}, data), { body: body, funcName: funcName, comment: comment }); } return null; }).filter(function (name) { return name; }); return { importType: importType, funcTypes: funcTypes, fileInfo: fileInfo }; } exports.requestFileParse = requestFileParse;