diff --git a/static_core/plugins/ets/tools/declgen_ts2sts/src/ASTAutofixer.ts b/static_core/plugins/ets/tools/declgen_ts2sts/src/ASTAutofixer.ts index e75471ecdec34be6029982205c55b865ab4afa8a..823c1cffbe0babc92afed8ae10bde28b54e97b24 100644 --- a/static_core/plugins/ets/tools/declgen_ts2sts/src/ASTAutofixer.ts +++ b/static_core/plugins/ets/tools/declgen_ts2sts/src/ASTAutofixer.ts @@ -2842,12 +2842,26 @@ function mergeReturnTypes( : returnTypes[0]; } +function getTypeNameKey(type: ts.TypeNode): string { + if (ts.isTypeReferenceNode(type)) { + const getName = (name: ts.EntityName): string => + ts.isIdentifier(name) + ? name.escapedText.toString() + : `${getName(name.left)}.${name.right.escapedText.toString()}`; + return getName(type.typeName); + } + if (ts.isArrayTypeNode(type)) { + return getTypeNameKey(type.elementType) + "[]"; + } + return ts.SyntaxKind[type.kind]; +} + function removeDuplicateTypes(types: ts.TypeNode[]): ts.TypeNode[] { const seenTypes = new Set(); const uniqueTypes: ts.TypeNode[] = []; for (const type of types) { - const typeText = type.getText(); + const typeText = getTypeNameKey(type); if (!seenTypes.has(typeText)) { seenTypes.add(typeText); uniqueTypes.push(type); @@ -2855,4 +2869,4 @@ function removeDuplicateTypes(types: ts.TypeNode[]): ts.TypeNode[] { } return uniqueTypes; -} \ No newline at end of file +} diff --git a/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dets_output/arkts-function-overload.d.ets b/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dets_output/arkts-function-overload.d.ets index b2aaab30d9fff174368e682516b70698447b3791..61fec30bc38ce79ce92cb57dd106a6a7be11ea93 100644 --- a/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dets_output/arkts-function-overload.d.ets +++ b/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dets_output/arkts-function-overload.d.ets @@ -45,7 +45,5 @@ export declare interface Transformer1 { transform1(param0: T[] | T): T[] | T; } export declare interface Formatter3 { - format1(value: string): string; - format2(value: number, precision: number): string; - format3(value: Date, formatString?: string): string; + format1(param0: string | number | Date, param1?: number | string): Any | string; } diff --git a/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dts_declarations/arkts-function-overload.d.ts b/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dts_declarations/arkts-function-overload.d.ts index ced317481113ca90fa34fd21af992ab521a5e3b7..8c5886815d9908919d67dd3beb25157998a59c69 100644 --- a/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dts_declarations/arkts-function-overload.d.ts +++ b/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/dts_declarations/arkts-function-overload.d.ts @@ -65,9 +65,9 @@ export declare interface Transformer1 { } export declare interface Formatter3 { - format1(value: string): string; - format2(value: number, precision: number): string; - format3(value: Date, formatString?: string): string; + format1(value: string): any; + format1(value: number, precision: number): string; + format1(value: Date, formatString?: string): string; } export declare class Calculator3 { diff --git a/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/ts_source/arkts-function-overload.ts b/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/ts_source/arkts-function-overload.ts new file mode 100644 index 0000000000000000000000000000000000000000..be127bd4bf38608a15d5cf8d230b7f97ebd251a9 --- /dev/null +++ b/static_core/plugins/ets/tools/declgen_ts2sts/test/cookbook_tests/ts_source/arkts-function-overload.ts @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export class Calculator1 { + add1(a: number, b: number): number; + add1(a: number, b: number, c: number): number; + add1(numbers: number[]): string; + add1(a: number | number[], b?: number, c?: number): number | string { return 0; } +} + +export class Calculator2 { + add2(a: number, b: number): number; + add2(a: number, b: number, c: number): number; + add2(numbers: number[]): number; + add2(...args: any[]): any; + add2(a: number | number[], b?: number): number { return 0; } +} + +export interface Formatter1 { + format1(value: number): any; + format1(value: Date): number; + format1(value: string, prefix: string): string; +} + +export interface Formatter2 { + format2(value: string): string; + format2(value: number, precision?: number): string; + format2(value: Date, formatString?: string): string; +} + +export interface Processor1 { + process1(input: string): string; + process1(input: number): number; + process1(input: boolean): boolean; +} + +export interface DataProcessor1 { + processData1(data: string[]): void; + processData1(data: { key: string; value: number }[]): void; + processData1(data: number[]): void; +} + +export interface Logger1 { + log1(message: string): void; +} + +export interface Validator1 { + validate1(input: string): boolean; + validate1(input: number): boolean; +} + +export interface Transformer1 { + transform1(input: T[]): T[]; + transform1(input: T): T; +} + +export interface Formatter3 { + format1(value: string): any; + format1(value: number, precision: number): string; + format1(value: Date, formatString?: string): string; +} + +export class Calculator3 { + add3(a: number, b: number): number { return 0; } + subtract3(a: number, b: number): number { return 0; } +}