Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/generate-output.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/test-cases/names-collision-with-globals/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {
output: {
inlineDeclareExternals: true,
inlineDeclareGlobals: true
}
};

export = config;
23 changes: 23 additions & 0 deletions tests/e2e/test-cases/names-collision-with-globals/input.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -7,3 +8,25 @@ export interface Int {
localP: LocalPromise;
globalP: Promise<number>;
}

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 {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare global {
type Promise$2 = string;

type Date$1 = string;

interface Interface1 {}
}

export interface Promise {}
Expand Down
36 changes: 36 additions & 0 deletions tests/e2e/test-cases/names-collision-with-globals/output.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -8,5 +17,32 @@ export interface Int {
localP: Promise$3;
globalP: Promise<number>;
}
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 {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node"]
}
}
Loading