diff --git a/build-tools/delete_systemapi_plugin.js b/build-tools/delete_systemapi_plugin.js index 163c6b9dad99c83b75c7f08b3917059c5285554a..83b8200ae0333af20d72f3c34f04f7be74f5ec76 100644 --- a/build-tools/delete_systemapi_plugin.js +++ b/build-tools/delete_systemapi_plugin.js @@ -41,6 +41,12 @@ const PATT = { REFERENCEURL_RIGHTSDK: /(..\/)(\S*)build-tools\/ets-loader\/declarations\/(\S*)/g, REFERENCEURL_SDK: /(..\/)(\S*)component\/(\S*)/g, }; +const METHOD_KIND = [ + ts.SyntaxKind.MethodDeclaration, + ts.SyntaxKind.FunctionDeclaration, + ts.SyntaxKind.MethodSignature, + ts.SyntaxKind.Constructor +]; function start() { const program = new commander.Command(); @@ -493,6 +499,59 @@ function writeFile(url, data, option) { const globalModules = new Map(); +/** + * 遍历处理overload节点 + * @param context 解析过后的内容 + * @param node 解析过后的节点 + * @returns ts.node + */ +function visitEachChild(context, node) { + return ts.visitEachChild(node, processAllNodes, context); // 遍历所有子节点 + function processAllNodes(node) { + if (ts.isOverloadDeclaration(node)) { + node = processInterfaceDeclaration(node); + } + return ts.visitEachChild(node, processAllNodes, context); + } + function processInterfaceDeclaration(overloadNode) { + // 获取方法类型兄弟节点列表 + const parentNode = overloadNode.parent; + const brotherNodes = []; + const brotherFuntionNames = new Set([]); + if (ts.isSourceFile(parentNode) || ts.isModuleBlock(parentNode)) { + brotherNodes.push(...parentNode.statements); + } else if (ts.isInterfaceDeclaration(parentNode) || ts.isClassDeclaration(parentNode)) { + brotherNodes.push(...parentNode.members); + } + if (brotherNodes.length === 0) { + return undefined; + } + brotherNodes.forEach(brotherNode => { + if (METHOD_KIND.includes(brotherNode.kind) && brotherNode.name && ts.isIdentifier(brotherNode.name) && + !brotherFuntionNames.has(brotherNode.name.escapedText.toString())) { + brotherFuntionNames.add(brotherNode.name.escapedText.toString()); + } + }); + + // 更新overload节点 + const overloadChildren = overloadNode.members; + if (overloadChildren.length === 0) { + return undefined; + } + const newChildren = []; + overloadChildren.forEach(overloadChild => { + if (overloadChild.name && ts.isIdentifier(overloadChild.name) && + brotherFuntionNames.has(overloadChild.name.escapedText.toString())) { + newChildren.push(overloadChild); + } + }); + if (newChildren.length === 0) { + return undefined; + } + return ts.factory.updateOverloadDeclaration(overloadNode, overloadNode.modifier, overloadNode.name, newChildren); + } +} + /** * 每个文件处理前回调函数第二个 * @param {string} url 文件路径 @@ -505,7 +564,7 @@ function formatImportDeclaration(url, copyrightMessage = '', isCopyrightDeleted sourceFile = node; collectAllIdentifier(node); // 获取所有标识符 formatValue = formatAllNodes(url, node, allIdentifierSet); // 获取所有节点 - node = formatValue.node; + node = visitEachChild(context, formatValue.node); const referencesMessage = formatValue.referencesMessage; if (formatValue.isCopyrightDeleted) { copyrightMessage = formatValue.copyrightMessage; diff --git a/build-tools/package-lock.json b/build-tools/package-lock.json index c92425b8bf584b64c3ba780f2e1d8c914f7fbe5c..aada9081620d6e0c46c3fb59db093182509d9c07 100644 --- a/build-tools/package-lock.json +++ b/build-tools/package-lock.json @@ -53,9 +53,9 @@ }, "node_modules/typescript": { "name": "ohos-typescript", - "version": "4.9.5-r8", - "resolved": "https://repo.huaweicloud.com/repository/npm/ohos-typescript/-/ohos-typescript-4.9.5-r8.tgz", - "integrity": "sha512-Wyj9xCBUOSMf7J1NsA82H75bpmA6Hm/U5V6yF62uz51XSq9spik57osyn/smAvPZqzrIhF/gRtPDYe0SWfqrAw==", + "version": "4.9.5-r9", + "resolved": "https://repo.huaweicloud.com/repository/npm/ohos-typescript/-/ohos-typescript-4.9.5-r9.tgz", + "integrity": "sha512-Hs7i6MKxHi15TjKK8qGiA75ejf0ggfF0L4F2GJ9XKLARu1hM5QQA395X+RwFYCjW76dqvf8s/CbhtiHNtackHg==", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/build-tools/package.json b/build-tools/package.json index ef7d9a08bf741212f872f847e5d52e681b47bd54..a28fb3e7d735dff61dfd5a4966ea258d25dd2627 100644 --- a/build-tools/package.json +++ b/build-tools/package.json @@ -13,6 +13,6 @@ "commander": "^13.1.0", "fs": "^0.0.1-security", "path": "^0.12.7", - "typescript": "npm:ohos-typescript@4.9.5-r8" + "typescript": "npm:ohos-typescript@4.9.5-r9" } }