Skip to content

Commit 738bd60

Browse files
Cherry-pick #58966 to release-5.5 (#59002)
Co-authored-by: Oleksandr T <[email protected]>
1 parent ce2e60e commit 738bd60

File tree

7 files changed

+159
-3
lines changed

7 files changed

+159
-3
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4590,7 +4590,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
45904590
}
45914591
if (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext) {
45924592
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
4593-
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined;
4593+
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | JSDocImportTag | undefined;
45944594
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
45954595
// normal mode restrictions
45964596
if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext && !hasResolutionModeOverride(overrideHost)) {

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ export function getModeForUsageLocation(file: { impliedNodeFormat?: ResolutionMo
902902
}
903903

904904
function getModeForUsageLocationWorker(file: { impliedNodeFormat?: ResolutionMode; }, usage: StringLiteralLike, compilerOptions?: CompilerOptions) {
905-
if ((isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent))) {
905+
if (isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent) || isJSDocImportTag(usage.parent)) {
906906
const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent);
907907
if (isTypeOnly) {
908908
const override = getResolutionModeOverride(usage.parent.attributes);

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10850,7 +10850,7 @@ export function isExpandoPropertyDeclaration(declaration: Declaration | undefine
1085010850
}
1085110851

1085210852
/** @internal */
10853-
export function hasResolutionModeOverride(node: ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined) {
10853+
export function hasResolutionModeOverride(node: ImportTypeNode | ImportDeclaration | ExportDeclaration | JSDocImportTag | undefined) {
1085410854
if (node === undefined) {
1085510855
return false;
1085610856
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/a.js(8,5): error TS2322: Type '1' is not assignable to type '"module"'.
2+
/a.js(15,5): error TS2322: Type '1' is not assignable to type '"script"'.
3+
4+
5+
==== /node_modules/@types/foo/package.json (0 errors) ====
6+
{
7+
"name": "@types/foo",
8+
"version": "1.0.0",
9+
"exports": {
10+
".": {
11+
"import": "./index.d.mts",
12+
"require": "./index.d.cts"
13+
}
14+
}
15+
}
16+
17+
==== /node_modules/@types/foo/index.d.mts (0 errors) ====
18+
export declare const Import: "module";
19+
20+
==== /node_modules/@types/foo/index.d.cts (0 errors) ====
21+
export declare const Require: "script";
22+
23+
==== /a.js (2 errors) ====
24+
/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */
25+
/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */
26+
27+
/**
28+
* @returns { Import }
29+
*/
30+
export function f1() {
31+
return 1;
32+
~~~~~~
33+
!!! error TS2322: Type '1' is not assignable to type '"module"'.
34+
}
35+
36+
/**
37+
* @returns { Require }
38+
*/
39+
export function f2() {
40+
return 1;
41+
~~~~~~
42+
!!! error TS2322: Type '1' is not assignable to type '"script"'.
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [tests/cases/conformance/jsdoc/importTag17.ts] ////
2+
3+
=== /node_modules/@types/foo/index.d.mts ===
4+
export declare const Import: "module";
5+
>Import : Symbol(Import, Decl(index.d.mts, 0, 20))
6+
7+
=== /node_modules/@types/foo/index.d.cts ===
8+
export declare const Require: "script";
9+
>Require : Symbol(Require, Decl(index.d.cts, 0, 20))
10+
11+
=== /a.js ===
12+
/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */
13+
/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */
14+
15+
/**
16+
* @returns { Import }
17+
*/
18+
export function f1() {
19+
>f1 : Symbol(f1, Decl(a.js, 0, 0))
20+
21+
return 1;
22+
}
23+
24+
/**
25+
* @returns { Require }
26+
*/
27+
export function f2() {
28+
>f2 : Symbol(f2, Decl(a.js, 8, 1))
29+
30+
return 1;
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/conformance/jsdoc/importTag17.ts] ////
2+
3+
=== /node_modules/@types/foo/index.d.mts ===
4+
export declare const Import: "module";
5+
>Import : "module"
6+
> : ^^^^^^^^
7+
8+
=== /node_modules/@types/foo/index.d.cts ===
9+
export declare const Require: "script";
10+
>Require : "script"
11+
> : ^^^^^^^^
12+
13+
=== /a.js ===
14+
/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */
15+
/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */
16+
17+
/**
18+
* @returns { Import }
19+
*/
20+
export function f1() {
21+
>f1 : () => "module"
22+
> : ^^^^^^^^^^^^^^
23+
24+
return 1;
25+
>1 : 1
26+
> : ^
27+
}
28+
29+
/**
30+
* @returns { Require }
31+
*/
32+
export function f2() {
33+
>f2 : () => "script"
34+
> : ^^^^^^^^^^^^^^
35+
36+
return 1;
37+
>1 : 1
38+
> : ^
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @module: node16
2+
// @checkJs: true
3+
// @allowJs: true
4+
// @noEmit: true
5+
6+
// @Filename: /node_modules/@types/foo/package.json
7+
{
8+
"name": "@types/foo",
9+
"version": "1.0.0",
10+
"exports": {
11+
".": {
12+
"import": "./index.d.mts",
13+
"require": "./index.d.cts"
14+
}
15+
}
16+
}
17+
18+
// @Filename: /node_modules/@types/foo/index.d.mts
19+
export declare const Import: "module";
20+
21+
// @Filename: /node_modules/@types/foo/index.d.cts
22+
export declare const Require: "script";
23+
24+
// @Filename: /a.js
25+
/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */
26+
/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */
27+
28+
/**
29+
* @returns { Import }
30+
*/
31+
export function f1() {
32+
return 1;
33+
}
34+
35+
/**
36+
* @returns { Require }
37+
*/
38+
export function f2() {
39+
return 1;
40+
}

0 commit comments

Comments
 (0)