From 1a903bef1fbd3fc457a7ceaf2caafe29e5c8b7ed Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Fri, 18 Oct 2024 18:54:04 -0600 Subject: [PATCH 1/5] Don't change name of interface/class/type --- src/generate-output.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/generate-output.ts b/src/generate-output.ts index f1682b5..14368ee 100644 --- a/src/generate-output.ts +++ b/src/generate-output.ts @@ -231,6 +231,14 @@ function getStatementText(statement: ts.Statement, includeSortingValue: boolean, return node; } + if (ts.isIdentifier(node) && (node.parent as ts.NamedDeclaration).name === node && ( + ts.isInterfaceDeclaration(node.parent) || + ts.isClassDeclaration(node.parent) || + ts.isTypeAliasDeclaration(node.parent) + )) { + return node; + } + return recreateEntityName(node, helpers); } } From 6991b361233a522fc6a1f6d335d927c2f36a1d0b Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sat, 19 Oct 2024 22:15:32 -0600 Subject: [PATCH 2/5] Exclude type alias as it seems to be handled by previous check --- src/generate-output.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/generate-output.ts b/src/generate-output.ts index 14368ee..14a28c8 100644 --- a/src/generate-output.ts +++ b/src/generate-output.ts @@ -233,8 +233,7 @@ function getStatementText(statement: ts.Statement, includeSortingValue: boolean, if (ts.isIdentifier(node) && (node.parent as ts.NamedDeclaration).name === node && ( ts.isInterfaceDeclaration(node.parent) || - ts.isClassDeclaration(node.parent) || - ts.isTypeAliasDeclaration(node.parent) + ts.isClassDeclaration(node.parent) )) { return node; } From 9f00415f48412b1b07906b773ae0c76ade81528c Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sat, 19 Oct 2024 22:15:54 -0600 Subject: [PATCH 3/5] Add test case --- .../names-collision-with-globals/config.ts | 3 +++ .../names-collision-with-globals/input.ts | 14 ++++++++++++++ .../names-collision-with-globals/output.d.ts | 14 ++++++++++++++ .../names-collision-with-globals/tsconfig.json | 6 ++++++ 4 files changed, 37 insertions(+) create mode 100644 tests/e2e/test-cases/names-collision-with-globals/tsconfig.json diff --git a/tests/e2e/test-cases/names-collision-with-globals/config.ts b/tests/e2e/test-cases/names-collision-with-globals/config.ts index 7f3c5de..5bdb2bc 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/config.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/config.ts @@ -1,6 +1,9 @@ import { TestCaseConfig } from '../test-case-config'; const config: TestCaseConfig = { + output: { + inlineDeclareExternals: true + } }; export = config; diff --git a/tests/e2e/test-cases/names-collision-with-globals/input.ts b/tests/e2e/test-cases/names-collision-with-globals/input.ts index 7fd5f8e..4597a42 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/input.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/input.ts @@ -1,4 +1,5 @@ import { Date as LocalDate, Promise as LocalPromise } from './local-types'; +import { BaseEncodingOptions as Alias1, BigIntOptions as Alias2 } from 'node:fs'; export interface Int { localD: LocalDate; @@ -7,3 +8,16 @@ export interface Int { localP: LocalPromise; globalP: Promise; } + +export interface Interface1 extends Alias1 {} +export interface Interface2 extends Alias2 {} + +declare module 'node:fs' { + interface BaseEncodingOptions { + newField: string; + } + + class BigIntOptions { + newField: string; + } +} diff --git a/tests/e2e/test-cases/names-collision-with-globals/output.d.ts b/tests/e2e/test-cases/names-collision-with-globals/output.d.ts index c789c1b..315dcec 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/output.d.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/output.d.ts @@ -1,3 +1,5 @@ +import { BaseEncodingOptions as Alias1, BigIntOptions as Alias2 } from 'node:fs'; + interface Promise$3 { } interface Date$2 { @@ -8,5 +10,17 @@ export interface Int { localP: Promise$3; globalP: Promise; } +export interface Interface1 extends Alias1 { +} +export interface Interface2 extends Alias2 { +} +declare module "node:fs" { + interface BaseEncodingOptions { + newField: string; + } + class BigIntOptions { + newField: string; + } +} export {}; diff --git a/tests/e2e/test-cases/names-collision-with-globals/tsconfig.json b/tests/e2e/test-cases/names-collision-with-globals/tsconfig.json new file mode 100644 index 0000000..83e602c --- /dev/null +++ b/tests/e2e/test-cases/names-collision-with-globals/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": ["node"] + } +} From cefb3614d2cc6e4dc2d85f1f3d4fbcd2d56326d5 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Wed, 23 Oct 2024 20:21:56 -0600 Subject: [PATCH 4/5] Check if class/interface are part of ambient module --- src/generate-output.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/generate-output.ts b/src/generate-output.ts index 14a28c8..2dc930c 100644 --- a/src/generate-output.ts +++ b/src/generate-output.ts @@ -1,7 +1,7 @@ import * as ts from 'typescript'; import { packageVersion } from './helpers/package-version'; -import { getModifiers, getNodeName, modifiersToMap, recreateRootLevelNodeWithModifiers } from './helpers/typescript'; +import { getClosestModuleLikeNode, getModifiers, getNodeName, isAmbientModule, modifiersToMap, recreateRootLevelNodeWithModifiers } from './helpers/typescript'; export interface ModuleImportsSet { defaultImports: Set; @@ -235,7 +235,10 @@ function getStatementText(statement: ts.Statement, includeSortingValue: boolean, ts.isInterfaceDeclaration(node.parent) || ts.isClassDeclaration(node.parent) )) { - return node; + const moduleNode = getClosestModuleLikeNode(node); + if (isAmbientModule(moduleNode)) { + return node; + } } return recreateEntityName(node, helpers); From 541db79fa873f3919648afeb65533589ab0f3efa Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Wed, 23 Oct 2024 20:31:03 -0600 Subject: [PATCH 5/5] Add more testcases --- .../names-collision-with-globals/config.ts | 3 ++- .../names-collision-with-globals/input.ts | 9 +++++++ .../local-types.ts | 2 ++ .../names-collision-with-globals/output.d.ts | 26 +++++++++++++++++-- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/tests/e2e/test-cases/names-collision-with-globals/config.ts b/tests/e2e/test-cases/names-collision-with-globals/config.ts index 5bdb2bc..d67c555 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/config.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/config.ts @@ -2,7 +2,8 @@ import { TestCaseConfig } from '../test-case-config'; const config: TestCaseConfig = { output: { - inlineDeclareExternals: true + inlineDeclareExternals: true, + inlineDeclareGlobals: true } }; diff --git a/tests/e2e/test-cases/names-collision-with-globals/input.ts b/tests/e2e/test-cases/names-collision-with-globals/input.ts index 4597a42..04ff6f8 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/input.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/input.ts @@ -20,4 +20,13 @@ declare module 'node:fs' { class BigIntOptions { newField: string; } + + interface Interface1 {} + + interface Interface3 extends Interface2 {} +} + +declare global { + interface Interface2 {} + interface Interface3 extends Interface1 {} } diff --git a/tests/e2e/test-cases/names-collision-with-globals/local-types.ts b/tests/e2e/test-cases/names-collision-with-globals/local-types.ts index c363421..e88fffc 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/local-types.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/local-types.ts @@ -4,6 +4,8 @@ declare global { type Promise$2 = string; type Date$1 = string; + + interface Interface1 {} } export interface Promise {} diff --git a/tests/e2e/test-cases/names-collision-with-globals/output.d.ts b/tests/e2e/test-cases/names-collision-with-globals/output.d.ts index 315dcec..1a4b148 100644 --- a/tests/e2e/test-cases/names-collision-with-globals/output.d.ts +++ b/tests/e2e/test-cases/names-collision-with-globals/output.d.ts @@ -1,5 +1,12 @@ import { BaseEncodingOptions as Alias1, BigIntOptions as Alias2 } from 'node:fs'; +declare global { + type Promise$1 = string; + type Promise$2 = string; + type Date$1 = string; + interface Interface1 { + } +} interface Promise$3 { } interface Date$2 { @@ -10,9 +17,9 @@ export interface Int { localP: Promise$3; globalP: Promise; } -export interface Interface1 extends Alias1 { +interface Interface1$1 extends Alias1 { } -export interface Interface2 extends Alias2 { +interface Interface2$1 extends Alias2 { } declare module "node:fs" { interface BaseEncodingOptions { @@ -21,6 +28,21 @@ declare module "node:fs" { class BigIntOptions { newField: string; } + interface Interface1 { + } + interface Interface3 extends Interface2$1 { + } } +declare global { + interface Interface2 { + } + interface Interface3 extends Interface1$1 { + } +} + +export { + Interface1$1 as Interface1, + Interface2$1 as Interface2, +}; export {};