diff --git a/build-tools/handleApiFiles.js b/build-tools/handleApiFiles.js index 2a5c3e5fbf1fdeea349dc0e84e318dee9f571577..341c3eb7b7723c43c8efa2d060e97c92941ba247 100755 --- a/build-tools/handleApiFiles.js +++ b/build-tools/handleApiFiles.js @@ -433,6 +433,7 @@ function handleFileInSecondType(apiRelativePath, fullPath, type, output) { // 有1.2标签的文件,删除标记 if (secondRegx.test(sourceFile.getFullText())) { let newFileContent = deleteUnsportedTag(fileContent); + newFileContent = getFileContent(newFileContent, fullPath); writeFile(outputPath, deleteArktsTag(newFileContent)); return; } @@ -458,6 +459,7 @@ function handleFileInSecondType(apiRelativePath, fullPath, type, output) { // 有1.2标签的文件,删除标记 if (secondRegx.test(firstJsdocText)) { let newFileContent = deleteUnsportedTag(fileContent); + newFileContent = getFileContent(newFileContent, fullPath); writeFile(outputPath, deleteArktsTag(newFileContent)); return; } @@ -503,6 +505,7 @@ function handlehasTagFile(sourceFile, outputPath) { } // 保留最后一段注释 newContent = saveLatestJsDoc(newContent); + newContent = getFileContent(newContent, outputPath); newContent = deleteUnsportedTag(newContent); writeFile(outputPath, deleteArktsTag(newContent)); } @@ -533,11 +536,75 @@ function handleNoTagFileInSecondType(sourceFile, outputPath, fullPath) { } // 保留最后一段注释 newContent = saveLatestJsDoc(newContent); + newContent = getFileContent(newContent, outputPath); newContent = deleteArktsTag(newContent); newContent = deleteUnsportedTag(newContent); writeFile(outputPath, newContent); } +/** + * 获取删除overload节点后的文件内容 + * @param {*} sourceFile + * @returns + */ +function getFileContent(newContent, filePath) { + if (isArkTsSpecialSyntax(newContent)) { + return newContent; + } + let sourceFile = ts.createSourceFile(path.basename(filePath), newContent, ts.ScriptTarget.ES2017, true); + const printer = ts.createPrinter(); + const result = ts.transform(sourceFile, [deleteOverLoadJsDoc]); + const output = printer.printFile(result.transformed[0]); + sourceFile = ''; + return output; +} + +function isArkTsSpecialSyntax(content) { + return /\@memo|(? { + const visitNode = (node) => { + if (ts.isStructDeclaration(node)) { + return processStructDeclaration(node); + } + if (ts.isOverloadDeclaration(node)) { + return ts.factory.createOverloadDeclaration(node.modifiers, node.name, node.members); + } + return ts.visitEachChild(node, visitNode, context); + }; + const newSourceFile = ts.visitNode(sourceFile, visitNode); + return newSourceFile; + }; +} + +/** + * 处理struct子节点 + */ +function processStructDeclaration(node) { + const newMembers = []; + node.members.forEach((member, index) => { + if (index >= 1) { + newMembers.push(member); + } + }); + node = ts.factory.updateStructDeclaration( + node, + node.modifiers, + node.name, + node.typeParameters, + node.heritageClauses, + newMembers + ); + return node; +} + /** * 没有arkts标签,但有if arkts 1.2和1.1&1.2的情况 * @param {*} sourceFile