Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reference marking of values merged with unresolvable type-only imports #54799

Merged
merged 2 commits into from Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 61 additions & 49 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/compiler/types.ts
Expand Up @@ -3814,8 +3814,8 @@ export type TypeOnlyImportDeclaration =

export type TypeOnlyExportDeclaration =
| ExportSpecifier & ({ readonly isTypeOnly: true } | { readonly parent: NamedExports & { readonly parent: ExportDeclaration & { readonly isTypeOnly: true } } })
| ExportDeclaration & { readonly isTypeOnly: true } // export * from "mod"
| NamespaceExport & { readonly parent: ExportDeclaration & { readonly isTypeOnly: true } } // export * as ns from "mod"
| ExportDeclaration & { readonly isTypeOnly: true, readonly moduleSpecifier: Expression } // export * from "mod"
| NamespaceExport & { readonly parent: ExportDeclaration & { readonly isTypeOnly: true, readonly moduleSpecifier: Expression } } // export * as ns from "mod"
;

export type TypeOnlyAliasDeclaration = TypeOnlyImportDeclaration | TypeOnlyExportDeclaration;
Expand Down Expand Up @@ -5878,7 +5878,7 @@ export interface SymbolLinks {
deferralParent?: Type; // Source union/intersection of a deferred type
cjsExportMerged?: Symbol; // Version of the symbol with all non export= exports merged with the export= target
typeOnlyDeclaration?: TypeOnlyAliasDeclaration | false; // First resolved alias declaration that makes the symbol only usable in type constructs
typeOnlyExportStarMap?: Map<__String, ExportDeclaration & { readonly isTypeOnly: true }>; // Set on a module symbol when some of its exports were resolved through a 'export type * from "mod"' declaration
typeOnlyExportStarMap?: Map<__String, ExportDeclaration & { readonly isTypeOnly: true, readonly moduleSpecifier: Expression }>; // Set on a module symbol when some of its exports were resolved through a 'export type * from "mod"' declaration
typeOnlyExportStarName?: __String; // Set to the name of the symbol re-exported by an 'export type *' declaration, when different from the symbol name
isConstructorDeclaredProperty?: boolean; // Property declared through 'this.x = ...' assignment in constructor
tupleLabelDeclaration?: NamedTupleMember | ParameterDeclaration; // Declaration associated with the tuple's label
Expand Down
12 changes: 12 additions & 0 deletions src/testRunner/unittests/services/transpile.ts
Expand Up @@ -620,4 +620,16 @@ export * as alias from './file';`, {
testVerbatimModuleSyntax: true
}
);

transpilesCorrectly("Preserves exported const merged with type-only import", `
import fooValue from "./values";
import type {Foo} from "./types";

const Foo: Foo = fooValue as any as Foo;

export {Foo};
`, {
options: { compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ESNext } },
testVerbatimModuleSyntax: true
});
});
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -5827,9 +5827,11 @@ declare namespace ts {
};
}) | ExportDeclaration & {
readonly isTypeOnly: true;
readonly moduleSpecifier: Expression;
} | NamespaceExport & {
readonly parent: ExportDeclaration & {
readonly isTypeOnly: true;
readonly moduleSpecifier: Expression;
};
};
type TypeOnlyAliasDeclaration = TypeOnlyImportDeclaration | TypeOnlyExportDeclaration;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -1774,9 +1774,11 @@ declare namespace ts {
};
}) | ExportDeclaration & {
readonly isTypeOnly: true;
readonly moduleSpecifier: Expression;
} | NamespaceExport & {
readonly parent: ExportDeclaration & {
readonly isTypeOnly: true;
readonly moduleSpecifier: Expression;
};
};
type TypeOnlyAliasDeclaration = TypeOnlyImportDeclaration | TypeOnlyExportDeclaration;
Expand Down
Expand Up @@ -5,6 +5,7 @@ import MyFunction from "./MyComponent";
>MyFunction : Symbol(MyFunction, Decl(main.ts, 0, 6))

MyFunction({msg: "Hello World"});
>MyFunction : Symbol(MyFunction, Decl(main.ts, 0, 6))
>msg : Symbol(msg, Decl(main.ts, 2, 12))


Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/importEquals3.js
Expand Up @@ -33,7 +33,9 @@ exports.A = A;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.x = void 0;
exports.x = exports.A = void 0;
var A = a.A; // Error
exports.A = A;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some illegal merges that were previously counted as unreferenced for purposes of emit are now counted as referenced. Stuff is going to crash either way, and I don’t really think one is particularly more correct than the other.

var x = 0;
exports.x = x;
//// [c.js]
Expand Down
43 changes: 43 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery2.js
@@ -0,0 +1,43 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery2.ts] ////

//// [z.ts]
interface A {}
export type { A };

//// [a.ts]
import { A } from './z';
const A = 0;
export { A };
export class B {};

//// [b.ts]
import * as types from './a';
let t: typeof types = {
A: undefined as any, // ok
B: undefined as any,
}


//// [z.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.B = exports.A = void 0;
var A = 0;
exports.A = A;
var B = /** @class */ (function () {
function B() {
}
return B;
}());
exports.B = B;
;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var t = {
A: undefined,
B: undefined,
};
39 changes: 39 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery2.symbols
@@ -0,0 +1,39 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery2.ts] ////

=== /z.ts ===
interface A {}
>A : Symbol(A, Decl(z.ts, 0, 0))

export type { A };
>A : Symbol(A, Decl(z.ts, 1, 13))

=== /a.ts ===
import { A } from './z';
>A : Symbol(A, Decl(a.ts, 0, 8), Decl(a.ts, 1, 5))

const A = 0;
>A : Symbol(A, Decl(a.ts, 0, 8), Decl(a.ts, 1, 5))

export { A };
>A : Symbol(A, Decl(a.ts, 2, 8))

export class B {};
>B : Symbol(B, Decl(a.ts, 2, 13))

=== /b.ts ===
import * as types from './a';
>types : Symbol(types, Decl(b.ts, 0, 6))

let t: typeof types = {
>t : Symbol(t, Decl(b.ts, 1, 3))
>types : Symbol(types, Decl(b.ts, 0, 6))

A: undefined as any, // ok
>A : Symbol(A, Decl(b.ts, 1, 23))
>undefined : Symbol(undefined)

B: undefined as any,
>B : Symbol(B, Decl(b.ts, 2, 22))
>undefined : Symbol(undefined)
}

41 changes: 41 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery2.types
@@ -0,0 +1,41 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery2.ts] ////

=== /z.ts ===
interface A {}
export type { A };
>A : A

=== /a.ts ===
import { A } from './z';
>A : 0

const A = 0;
>A : 0
>0 : 0

export { A };
>A : 0

export class B {};
>B : B

=== /b.ts ===
import * as types from './a';
>types : typeof types

let t: typeof types = {
>t : typeof types
>types : typeof types
>{ A: undefined as any, // ok B: undefined as any,} : { A: any; B: any; }

A: undefined as any, // ok
>A : any
>undefined as any : any
>undefined : undefined

B: undefined as any,
>B : any
>undefined as any : any
>undefined : undefined
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery3.errors.txt
@@ -0,0 +1,18 @@
/a.ts(1,24): error TS2307: Cannot find module './z' or its corresponding type declarations.


==== /a.ts (1 errors) ====
import type { A } from './z'; // unresolved
~~~~~
!!! error TS2307: Cannot find module './z' or its corresponding type declarations.
const A = 0;
export { A };
export class B {};

==== /b.ts (0 errors) ====
import * as types from './a';
let t: typeof types = {
A: undefined as any, // ok
B: undefined as any,
}

36 changes: 36 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery3.js
@@ -0,0 +1,36 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery3.ts] ////

//// [a.ts]
import type { A } from './z'; // unresolved
const A = 0;
export { A };
export class B {};

//// [b.ts]
import * as types from './a';
let t: typeof types = {
A: undefined as any, // ok
B: undefined as any,
}


//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.B = exports.A = void 0;
var A = 0;
exports.A = A;
var B = /** @class */ (function () {
function B() {
}
return B;
}());
exports.B = B;
;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var t = {
A: undefined,
B: undefined,
};
32 changes: 32 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery3.symbols
@@ -0,0 +1,32 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery3.ts] ////

=== /a.ts ===
import type { A } from './z'; // unresolved
>A : Symbol(A, Decl(a.ts, 0, 13), Decl(a.ts, 1, 5))

const A = 0;
>A : Symbol(A, Decl(a.ts, 0, 13), Decl(a.ts, 1, 5))

export { A };
>A : Symbol(A, Decl(a.ts, 2, 8))

export class B {};
>B : Symbol(B, Decl(a.ts, 2, 13))

=== /b.ts ===
import * as types from './a';
>types : Symbol(types, Decl(b.ts, 0, 6))

let t: typeof types = {
>t : Symbol(t, Decl(b.ts, 1, 3))
>types : Symbol(types, Decl(b.ts, 0, 6))

A: undefined as any, // ok
>A : Symbol(A, Decl(b.ts, 1, 23))
>undefined : Symbol(undefined)

B: undefined as any,
>B : Symbol(B, Decl(b.ts, 2, 22))
>undefined : Symbol(undefined)
}

36 changes: 36 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery3.types
@@ -0,0 +1,36 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery3.ts] ////

=== /a.ts ===
import type { A } from './z'; // unresolved
>A : any

const A = 0;
>A : 0
>0 : 0

export { A };
>A : 0

export class B {};
>B : B

=== /b.ts ===
import * as types from './a';
>types : typeof types

let t: typeof types = {
>t : typeof types
>types : typeof types
>{ A: undefined as any, // ok B: undefined as any,} : { A: any; B: any; }

A: undefined as any, // ok
>A : any
>undefined as any : any
>undefined : undefined

B: undefined as any,
>B : any
>undefined as any : any
>undefined : undefined
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery4.errors.txt
@@ -0,0 +1,23 @@
/a.ts(1,24): error TS2307: Cannot find module './z' or its corresponding type declarations.
/b.ts(3,3): error TS2322: Type '{ A: any; B: any; }' is not assignable to type 'typeof import("/a")'.
Object literal may only specify known properties, and 'A' does not exist in type 'typeof import("/a")'.


==== /a.ts (1 errors) ====
import type { A } from './z'; // unresolved
~~~~~
!!! error TS2307: Cannot find module './z' or its corresponding type declarations.
type A = 0;
export { A };
export class B {};

==== /b.ts (1 errors) ====
import * as types from './a';
let t: typeof types = {
A: undefined as any, // error
~
!!! error TS2322: Type '{ A: any; B: any; }' is not assignable to type 'typeof import("/a")'.
!!! error TS2322: Object literal may only specify known properties, and 'A' does not exist in type 'typeof import("/a")'.
B: undefined as any,
}

34 changes: 34 additions & 0 deletions tests/baselines/reference/namespaceImportTypeQuery4.js
@@ -0,0 +1,34 @@
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery4.ts] ////

//// [a.ts]
import type { A } from './z'; // unresolved
type A = 0;
export { A };
export class B {};

//// [b.ts]
import * as types from './a';
let t: typeof types = {
A: undefined as any, // error
B: undefined as any,
}


//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.B = void 0;
var B = /** @class */ (function () {
function B() {
}
return B;
}());
exports.B = B;
;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var t = {
A: undefined,
B: undefined,
};