Skip to content

Commit

Permalink
Fixed incorrect handling of declare module statements with non-rela…
Browse files Browse the repository at this point in the history
…tive module name

Fixes #186
  • Loading branch information
timocov committed Jan 31, 2022
1 parent 01638d6 commit 56b22bc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
15 changes: 0 additions & 15 deletions src/helpers/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as ts from 'typescript';

import { fixPath } from './fix-path';
import { getLibraryName } from './node-modules';

const namedDeclarationKinds = [
Expand Down Expand Up @@ -110,20 +109,6 @@ export function isDeclareModule(node: ts.Node): node is ts.ModuleDeclaration {
return ts.isModuleDeclaration(node) && !(node.flags & ts.NodeFlags.Namespace) && !isGlobalScopeAugmentation(node);
}

/**
* Returns whether a node is `declare module` ModuleDeclaration with a relative path
*/
export function isRelativeDeclareModule(node: ts.Node): node is ts.ModuleDeclaration {
// `declare module ""`, `declare global` and `namespace {}` are ModuleDeclaration
// but here we need to check only `declare module` statements
if (!isDeclareModule(node) || !ts.isStringLiteral(node.name)) {
return false;
}

const moduleName = fixPath(node.name.text);
return moduleName.startsWith('./') || moduleName.startsWith('../');
}

/**
* Returns whether statement is `declare global` ModuleDeclaration
*/
Expand Down
4 changes: 2 additions & 2 deletions src/types-usage-evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as ts from 'typescript';
import {
getActualSymbol,
isDeclareModule,
isNamedTupleMember,
isNodeNamedDeclaration,
isRelativeDeclareModule,
splitTransientSymbol,
} from './helpers/typescript';

Expand Down Expand Up @@ -59,7 +59,7 @@ export class TypesUsageEvaluator {
}

private computeUsageForNode(node: ts.Node): void {
if (isRelativeDeclareModule(node) && node.body !== undefined && ts.isModuleBlock(node.body)) {
if (isDeclareModule(node) && node.body !== undefined && ts.isModuleBlock(node.body)) {
for (const statement of node.body.statements) {
this.computeUsageForNode(statement);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test-cases/inline-from-deps/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {
libraries: {
inlinedLibraries: ['fake-package'],
inlinedLibraries: ['fake-package', 'fake-types-lib-2'],
},
};

Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/test-cases/inline-from-deps/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Interface, Type } from 'fake-package';
import { Derived } from 'fake-types-lib-2';
import { SomeClass } from 'fake-package/some-class';

export type TestType = Interface | Type;
export class MyClass extends SomeClass {}
export type ReExportedTypes = Derived;
7 changes: 7 additions & 0 deletions tests/e2e/test-cases/inline-from-deps/output.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
export interface Interface {
}
export type Type = number | string;
export interface Base {
id: string;
}
export interface Derived extends Base {
name: string;
}
declare class SomeClass {
private x;
public constructor();
}
export declare type TestType = Interface | Type;
export declare class MyClass extends SomeClass {
}
export declare type ReExportedTypes = Derived;

export {};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 56b22bc

Please sign in to comment.