diff --git a/src/generate-output.ts b/src/generate-output.ts index f1682b5..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; @@ -231,6 +231,16 @@ 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) + )) { + const moduleNode = getClosestModuleLikeNode(node); + if (isAmbientModule(moduleNode)) { + return node; + } + } + return recreateEntityName(node, helpers); } } 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..d67c555 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,10 @@ import { TestCaseConfig } from '../test-case-config'; const config: TestCaseConfig = { + output: { + inlineDeclareExternals: true, + inlineDeclareGlobals: 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..04ff6f8 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,25 @@ 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; + } + + 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 c789c1b..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,3 +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 { @@ -8,5 +17,32 @@ export interface Int { localP: Promise$3; globalP: Promise; } +interface Interface1$1 extends Alias1 { +} +interface Interface2$1 extends Alias2 { +} +declare module "node:fs" { + interface BaseEncodingOptions { + newField: string; + } + 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 {}; 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"] + } +}