Skip to content

Commit

Permalink
Merge pull request #121 from bloomberg/isolated-declarations-unused-i…
Browse files Browse the repository at this point in the history
…mports

Revert change in checker that prevented TS from removing  some imports
  • Loading branch information
dragomirtitian authored Nov 29, 2023
2 parents 4e01096 + cf1ca98 commit c0831c2
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 7 deletions.
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2764,10 +2764,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return symbol;
}
if (symbol.flags & SymbolFlags.Alias) {
// Do not take target symbol meaning into account in isolated declaration mode since we don't have access to info from other files.
if (compilerOptions.isolatedDeclarations) {
return symbol;
}
const targetFlags = getSymbolFlags(symbol);
// `targetFlags` will be `SymbolFlags.All` if an error occurred in alias resolution; this avoids cascading errors
if (targetFlags & meaning) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// [[Reason: TSC removes import that is not used in a semantically valid way. DTE can't know about this.]] ////

//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision7.ts] ////

===================================================================
--- TSC declarations
+++ DTE declarations
@@ -5,8 +5,9 @@
doSomething(): invalid;
}

//// [service.d.ts]
+import db from './db';
declare class MyClass {
db: db.db;
constructor(db: db.db);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// [[Reason: TSC removes type only import. DTE can't know import is type only.]] ////

//// [tests/cases/compiler/typeReferenceDirectives13.ts] ////

===================================================================
--- TSC declarations
+++ DTE declarations
@@ -1,7 +1,8 @@


//// [/app.d.ts]
+import { $ } from "./ref";
export interface A {
x: () => typeof $;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// [[Reason: TSC removes type only import. DTE can't know import is type only.]] ////

//// [tests/cases/compiler/typeReferenceDirectives5.ts] ////

===================================================================
--- TSC declarations
+++ DTE declarations
@@ -1,7 +1,8 @@


//// [/app.d.ts]
+import { $ } from "./ref";
export interface A {
x: typeof $;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision7.ts] ////

//// [db.ts]
export default class db {
public doSomething() {
}
}

//// [service.ts]
import db from './db';
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: db.db; //error

constructor(db: db.db) { // error
this.db = db;
this.db.doSomething();
}
}
export {MyClass};


/// [Declarations] ////



//// [db.d.ts]
export default class db {
doSomething(): invalid;
}

//// [service.d.ts]
import db from './db';
declare class MyClass {
db: db.db;
constructor(db: db.db);
}
export { MyClass };

/// [Errors] ////

db.ts(2,12): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
service.ts(7,9): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
service.ts(7,9): error TS4031: Public property 'db' of exported class has or is using private name 'db'.
service.ts(9,21): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
service.ts(9,21): error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.


==== db.ts (1 errors) ====
export default class db {
public doSomething() {
~~~~~~~~~~~
!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
}
}

==== service.ts (4 errors) ====
import db from './db';
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: db.db; //error
~~
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
~~
!!! error TS4031: Public property 'db' of exported class has or is using private name 'db'.

constructor(db: db.db) { // error
~~
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
~~
!!! error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.
this.db = db;
this.db.doSomething();
}
}
export {MyClass};

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/typeReferenceDirectives13.ts] ////

//// [/app.ts]
/// <reference types="lib"/>
import {$} from "./ref";
export interface A {
x: () => typeof $
}

//// [/ref.d.ts]
export interface $ { x }

//// [/types/lib/index.d.ts]
declare let $: { x: number }


/// [Declarations] ////



//// [/app.d.ts]
import { $ } from "./ref";
export interface A {
x: () => typeof $;
}

/// [Errors] ////

/app.ts(1,23): error TS9010: Reference directives are not supported in isolated declaration mode.


==== /app.ts (1 errors) ====
/// <reference types="lib"/>
~~~
!!! error TS9010: Reference directives are not supported in isolated declaration mode.
import {$} from "./ref";
export interface A {
x: () => typeof $
}

==== /ref.d.ts (0 errors) ====
export interface $ { x }

==== /types/lib/index.d.ts (0 errors) ====
declare let $: { x: number }

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/typeReferenceDirectives5.ts] ////

//// [/app.ts]
/// <reference types="lib"/>
import {$} from "./ref";
export interface A {
x: typeof $;
}
//// [/ref.d.ts]
export interface $ { x }

//// [/types/lib/index.d.ts]
declare let $: { x: number }


/// [Declarations] ////



//// [/app.d.ts]
import { $ } from "./ref";
export interface A {
x: typeof $;
}

/// [Errors] ////

/app.ts(1,23): error TS9010: Reference directives are not supported in isolated declaration mode.


==== /app.ts (1 errors) ====
/// <reference types="lib"/>
~~~
!!! error TS9010: Reference directives are not supported in isolated declaration mode.
import {$} from "./ref";
export interface A {
x: typeof $;
}
==== /ref.d.ts (0 errors) ====
export interface $ { x }

==== /types/lib/index.d.ts (0 errors) ====
declare let $: { x: number }

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//// [tests/cases/compiler/decoratorMetadataWithImportDeclarationNameCollision7.ts] ////

//// [db.ts]
export default class db {
public doSomething() {
}
}

//// [service.ts]
import db from './db';
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: db.db; //error

constructor(db: db.db) { // error
this.db = db;
this.db.doSomething();
}
}
export {MyClass};


/// [Declarations] ////



//// [db.d.ts]
export default class db {
doSomething(): invalid;
}

//// [service.d.ts]
declare class MyClass {
db: db.db;
constructor(db: db.db);
}
export { MyClass };

/// [Errors] ////

db.ts(2,12): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
service.ts(7,9): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
service.ts(7,9): error TS4031: Public property 'db' of exported class has or is using private name 'db'.
service.ts(9,21): error TS2702: 'db' only refers to a type, but is being used as a namespace here.
service.ts(9,21): error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.


==== db.ts (1 errors) ====
export default class db {
public doSomething() {
~~~~~~~~~~~
!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
}
}

==== service.ts (4 errors) ====
import db from './db';
function someDecorator(target) {
return target;
}
@someDecorator
class MyClass {
db: db.db; //error
~~
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
~~
!!! error TS4031: Public property 'db' of exported class has or is using private name 'db'.

constructor(db: db.db) { // error
~~
!!! error TS2702: 'db' only refers to a type, but is being used as a namespace here.
~~
!!! error TS4063: Parameter 'db' of constructor from exported class has or is using private name 'db'.
this.db = db;
this.db.doSomething();
}
}
export {MyClass};

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/typeReferenceDirectives13.ts] ////

//// [/app.ts]
/// <reference types="lib"/>
import {$} from "./ref";
export interface A {
x: () => typeof $
}

//// [/ref.d.ts]
export interface $ { x }

//// [/types/lib/index.d.ts]
declare let $: { x: number }


/// [Declarations] ////



//// [/app.d.ts]
export interface A {
x: () => typeof $;
}

/// [Errors] ////

/app.ts(1,23): error TS9010: Reference directives are not supported in isolated declaration mode.


==== /app.ts (1 errors) ====
/// <reference types="lib"/>
~~~
!!! error TS9010: Reference directives are not supported in isolated declaration mode.
import {$} from "./ref";
export interface A {
x: () => typeof $
}

==== /ref.d.ts (0 errors) ====
export interface $ { x }

==== /types/lib/index.d.ts (0 errors) ====
declare let $: { x: number }

Loading

0 comments on commit c0831c2

Please sign in to comment.