From d7ca03031c9fb913d42c4ccf7ad2bc7ebf3d6203 Mon Sep 17 00:00:00 2001 From: wangzhiyusss Date: Mon, 18 Aug 2025 19:32:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81&&=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=BD=AC=E6=8D=A2=E7=A9=BA=E6=A0=BC=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangzhiyusss --- build-tools/handleApiFiles.js | 141 +++++++++++++++++++++++++--------- build-tools/intToNumber.js | 9 +-- 2 files changed, 107 insertions(+), 43 deletions(-) diff --git a/build-tools/handleApiFiles.js b/build-tools/handleApiFiles.js index 2a5c3e5fbf..db43a1168e 100755 --- a/build-tools/handleApiFiles.js +++ b/build-tools/handleApiFiles.js @@ -181,54 +181,48 @@ function handleApiFileByType(apiRelativePath, rootPath, type, output, isPublic) writeFile(outputPath, fileContent); return; } - const handleFullpath = getHandleFullPath(fullPath, apiRelativePath, type); - if (type === 'ets2' && handleFullpath !== undefined) { - handleFileInSecondType(apiRelativePath, handleFullpath, type, output); - } else if (type === 'ets' && handleFullpath !== undefined) { - handleFileInFirstType(apiRelativePath, handleFullpath, type, output); + const isCorrectHandleFullpath = isHandleFullPath(fullPath, apiRelativePath, type); + if (type === 'ets2' && isCorrectHandleFullpath) { + handleFileInSecondType(apiRelativePath, fullPath, type, output); + } else if (type === 'ets' && isCorrectHandleFullpath) { + handleFileInFirstType(apiRelativePath, fullPath, type, output); } } -function getHandleFullPath(fullPath, apiRelativePath, type) { - const isEndWithEts = isEtsFile(apiRelativePath); - const isEndWithTs = isTsFile(apiRelativePath); - const isEndWithStatic = isStaticFile(apiRelativePath); - // 处理.ts文件逻辑 - if (isEndWithTs) { +/** + * 判断当前的文件路径是否符合当前打包场景,过滤同名文件 + * @param {*} fullPath + * @param {*} apiRelativePath + * @param {*} type + * @returns + */ +function isHandleFullPath(fullPath, apiRelativePath, type) { + // 当前文件为.ts结尾文件 + if (isTsFile(apiRelativePath)) { + if (type === 'ets') { + return true; + } if (!(hasEtsFile(fullPath)) && !(hasStaticFile(fullPath))) { - return fullPath; - } else { - if (type === 'ets') { - return fullPath; - } + return true; } } - // 处理.ets文件逻辑 - if (isEndWithEts) { + // 当前文件为.ets结尾文件 + if (isEtsFile(apiRelativePath)) { if (!(hasTsFile(fullPath)) && !(hasStaticFile(fullPath))) { - return fullPath; + return true; } - if (hasTsFile(fullPath) && !(hasStaticFile(fullPath))) { - if (type === 'ets2') { - return fullPath; - } - } - if (hasStaticFile(fullPath) && !(hasTsFile(fullPath))) { - if (type === 'ets') { - return fullPath; - } + if (hasTsFile(fullPath) && !(hasStaticFile(fullPath)) && type === 'ets2') { + return true; } - if (hasStaticFile(fullPath) && hasTsFile(fullPath)) { - return undefined; + if (hasStaticFile(fullPath) && !(hasTsFile(fullPath)) && type === 'ets') { + return true; } } - // 处理.static文件逻辑 - if (isEndWithStatic) { - if (type === 'ets2') { - return fullPath; - } + // 当前文件为.static结尾文件 + if (isStaticFile(apiRelativePath) && type === 'ets2') { + return true; } - return undefined; + return false; } /** @@ -433,6 +427,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 +453,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 +499,7 @@ function handlehasTagFile(sourceFile, outputPath) { } // 保留最后一段注释 newContent = saveLatestJsDoc(newContent); + newContent = getFileContent(newContent, outputPath); newContent = deleteUnsportedTag(newContent); writeFile(outputPath, deleteArktsTag(newContent)); } @@ -533,11 +530,81 @@ function handleNoTagFileInSecondType(sourceFile, outputPath, fullPath) { } // 保留最后一段注释 newContent = saveLatestJsDoc(newContent); + newContent = getFileContent(newContent, outputPath); newContent = deleteArktsTag(newContent); newContent = deleteUnsportedTag(newContent); writeFile(outputPath, newContent); } +/** + * 获取删除overload节点后的文件内容 + * @param {*} newContent 文件内容 + * @param {*} filePath 文件路径 + * @returns + */ +function getFileContent(newContent, filePath) { + const regex = /^overload\s+.+$/; + if (isArkTsSpecialSyntax(newContent) || !regex.test(newContent)) { + return newContent; + } + const 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]); + return output; +} + +/** + * 判断文件中是否存在@memo节点和@interface节点 + * @param {*} content 文件内容 + * @returns + */ +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子节点,防止tsc自动增加constructor方法 + */ +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 @@ -546,7 +613,7 @@ function handleNoTagFileInSecondType(sourceFile, outputPath, fullPath) { */ function saveApiByArktsDefinition(sourceFile, fileContent, outputPath) { const regx = /\/\*\*\* if arkts (1.1&)?1.2 \*\/\s*([\s\S]*?)\s*\/\*\*\* endif \*\//g; - const regex = /\/\*\r?\n\s*\*\s*Copyright[\s\S]*?limitations under the License\.\r?\n\s*\*\//g; + const regex = /\/\*\r?\n\s*\*\s*Copyright[\s\S]*?\*\//g; const copyrightMessage = fileContent.match(regex)[0]; const firstNode = sourceFile.statements.find(statement => { return !ts.isExpressionStatement(statement); diff --git a/build-tools/intToNumber.js b/build-tools/intToNumber.js index 92a43c7f00..00efb38d76 100644 --- a/build-tools/intToNumber.js +++ b/build-tools/intToNumber.js @@ -307,7 +307,6 @@ function applJSDocTransformations(typeExpr, newTypeExpr, tagDataList, isChange) if (finalContent.includes('number') && typeExpr.kind === ts.SyntaxKind.JSDocNullableType && !finalContent.includes('?number') && isChange) { if (typeExpr.type.type && typeExpr.type.type.kind === ts.SyntaxKind.UnionType) { const data = { - isDelete: false, pos: typeExpr.pos, end: typeExpr.end, convertedText: '?' + `(${finalContent})` @@ -315,7 +314,6 @@ function applJSDocTransformations(typeExpr, newTypeExpr, tagDataList, isChange) tagDataList.push(data); } else { const data = { - isDelete: false, pos: typeExpr.pos, end: typeExpr.end, convertedText: '?' + finalContent @@ -324,7 +322,6 @@ function applJSDocTransformations(typeExpr, newTypeExpr, tagDataList, isChange) } } else if (finalContent.includes('number')) { const data = { - isDelete: true, pos: typeExpr.pos, end: typeExpr.end, convertedText: finalContent @@ -344,10 +341,10 @@ function changeContent(tagDataList) { for (const data of tagDataList) { const before = jsDocContent.substring(0, data.pos); const after = jsDocContent.substring(data.end); - if (data.isDelete) { - jsDocContent = before + `${data.convertedText}` + after; - } else { + if (jsDocContent.substring(data.pos, data.pos + 1) === ' ') { jsDocContent = before + ` ${data.convertedText}` + after; + } else { + jsDocContent = before + `${data.convertedText}` + after; } } } -- Gitee