Skip to content

Commit bca8d38

Browse files
weswighammprobst
authored andcommitted
Use features for selected module resolution rather than all features for type reference directives, since they can add restrictions to resolutions (microsoft#47007)
1 parent c265fa2 commit bca8d38

13 files changed

+190
-3
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,11 @@ namespace ts {
340340
}
341341

342342
const failedLookupLocations: string[] = [];
343-
const moduleResolutionState: ModuleResolutionState = { compilerOptions: options, host, traceEnabled, failedLookupLocations, packageJsonInfoCache: cache, features: NodeResolutionFeatures.AllFeatures, conditions: ["node", "require", "types"] };
343+
const features =
344+
getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node12 ? NodeResolutionFeatures.Node12Default :
345+
getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext ? NodeResolutionFeatures.NodeNextDefault :
346+
NodeResolutionFeatures.None;
347+
const moduleResolutionState: ModuleResolutionState = { compilerOptions: options, host, traceEnabled, failedLookupLocations, packageJsonInfoCache: cache, features, conditions: ["node", "require", "types"] };
344348
let resolved = primaryLookup();
345349
let primary = true;
346350
if (!resolved) {
@@ -1186,14 +1190,18 @@ namespace ts {
11861190
ExportsPatternTrailers = 1 << 4,
11871191
AllFeatures = Imports | SelfName | Exports | ExportsPatternTrailers,
11881192

1193+
Node12Default = Imports | SelfName | Exports,
1194+
1195+
NodeNextDefault = AllFeatures,
1196+
11891197
EsmMode = 1 << 5,
11901198
}
11911199

11921200
function node12ModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
11931201
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
11941202
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
11951203
return nodeNextModuleNameResolverWorker(
1196-
NodeResolutionFeatures.Imports | NodeResolutionFeatures.SelfName | NodeResolutionFeatures.Exports,
1204+
NodeResolutionFeatures.Node12Default,
11971205
moduleName,
11981206
containingFile,
11991207
compilerOptions,
@@ -1208,7 +1216,7 @@ namespace ts {
12081216
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
12091217
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
12101218
return nodeNextModuleNameResolverWorker(
1211-
NodeResolutionFeatures.AllFeatures,
1219+
NodeResolutionFeatures.NodeNextDefault,
12121220
moduleName,
12131221
containingFile,
12141222
compilerOptions,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/tripleSlashTypesReferenceWithMissingExports.ts] ////
2+
3+
//// [index.d.ts]
4+
interface GlobalThing { a: number }
5+
//// [package.json]
6+
{
7+
"name": "pkg",
8+
"types": "index.d.ts",
9+
"exports": "some-other-thing.js"
10+
}
11+
//// [usage.ts]
12+
/// <reference types="pkg" />
13+
14+
const a: GlobalThing = { a: 0 };
15+
16+
//// [usage.js]
17+
/// <reference types="pkg" />
18+
var a = { a: 0 };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
2+
interface GlobalThing { a: number }
3+
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
4+
>a : Symbol(GlobalThing.a, Decl(index.d.ts, 0, 23))
5+
6+
=== tests/cases/compiler/usage.ts ===
7+
/// <reference types="pkg" />
8+
9+
const a: GlobalThing = { a: 0 };
10+
>a : Symbol(a, Decl(usage.ts, 2, 5))
11+
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
12+
>a : Symbol(a, Decl(usage.ts, 2, 24))
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
2+
interface GlobalThing { a: number }
3+
>a : number
4+
5+
=== tests/cases/compiler/usage.ts ===
6+
/// <reference types="pkg" />
7+
8+
const a: GlobalThing = { a: 0 };
9+
>a : GlobalThing
10+
>{ a: 0 } : { a: number; }
11+
>a : number
12+
>0 : 0
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/usage.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.
2+
3+
4+
==== tests/cases/compiler/node_modules/pkg/index.d.ts (0 errors) ====
5+
interface GlobalThing { a: number }
6+
==== tests/cases/compiler/node_modules/pkg/package.json (0 errors) ====
7+
{
8+
"name": "pkg",
9+
"types": "index.d.ts",
10+
"exports": "some-other-thing.js"
11+
}
12+
==== tests/cases/compiler/usage.ts (1 errors) ====
13+
/// <reference types="pkg" />
14+
~~~
15+
!!! error TS2688: Cannot find type definition file for 'pkg'.
16+
17+
const a: GlobalThing = { a: 0 };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/tripleSlashTypesReferenceWithMissingExports.ts] ////
2+
3+
//// [index.d.ts]
4+
interface GlobalThing { a: number }
5+
//// [package.json]
6+
{
7+
"name": "pkg",
8+
"types": "index.d.ts",
9+
"exports": "some-other-thing.js"
10+
}
11+
//// [usage.ts]
12+
/// <reference types="pkg" />
13+
14+
const a: GlobalThing = { a: 0 };
15+
16+
//// [usage.js]
17+
/// <reference types="pkg" />
18+
const a = { a: 0 };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
2+
interface GlobalThing { a: number }
3+
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
4+
>a : Symbol(GlobalThing.a, Decl(index.d.ts, 0, 23))
5+
6+
=== tests/cases/compiler/usage.ts ===
7+
/// <reference types="pkg" />
8+
9+
const a: GlobalThing = { a: 0 };
10+
>a : Symbol(a, Decl(usage.ts, 2, 5))
11+
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
12+
>a : Symbol(a, Decl(usage.ts, 2, 24))
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
2+
interface GlobalThing { a: number }
3+
>a : number
4+
5+
=== tests/cases/compiler/usage.ts ===
6+
/// <reference types="pkg" />
7+
8+
const a: GlobalThing = { a: 0 };
9+
>a : GlobalThing
10+
>{ a: 0 } : { a: number; }
11+
>a : number
12+
>0 : 0
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/usage.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.
2+
3+
4+
==== tests/cases/compiler/node_modules/pkg/index.d.ts (0 errors) ====
5+
interface GlobalThing { a: number }
6+
==== tests/cases/compiler/node_modules/pkg/package.json (0 errors) ====
7+
{
8+
"name": "pkg",
9+
"types": "index.d.ts",
10+
"exports": "some-other-thing.js"
11+
}
12+
==== tests/cases/compiler/usage.ts (1 errors) ====
13+
/// <reference types="pkg" />
14+
~~~
15+
!!! error TS2688: Cannot find type definition file for 'pkg'.
16+
17+
const a: GlobalThing = { a: 0 };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/tripleSlashTypesReferenceWithMissingExports.ts] ////
2+
3+
//// [index.d.ts]
4+
interface GlobalThing { a: number }
5+
//// [package.json]
6+
{
7+
"name": "pkg",
8+
"types": "index.d.ts",
9+
"exports": "some-other-thing.js"
10+
}
11+
//// [usage.ts]
12+
/// <reference types="pkg" />
13+
14+
const a: GlobalThing = { a: 0 };
15+
16+
//// [usage.js]
17+
/// <reference types="pkg" />
18+
const a = { a: 0 };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
2+
interface GlobalThing { a: number }
3+
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
4+
>a : Symbol(GlobalThing.a, Decl(index.d.ts, 0, 23))
5+
6+
=== tests/cases/compiler/usage.ts ===
7+
/// <reference types="pkg" />
8+
9+
const a: GlobalThing = { a: 0 };
10+
>a : Symbol(a, Decl(usage.ts, 2, 5))
11+
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
12+
>a : Symbol(a, Decl(usage.ts, 2, 24))
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
2+
interface GlobalThing { a: number }
3+
>a : number
4+
5+
=== tests/cases/compiler/usage.ts ===
6+
/// <reference types="pkg" />
7+
8+
const a: GlobalThing = { a: 0 };
9+
>a : GlobalThing
10+
>{ a: 0 } : { a: number; }
11+
>a : number
12+
>0 : 0
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @module: commonjs,node12,nodenext
2+
// @filename: node_modules/pkg/index.d.ts
3+
interface GlobalThing { a: number }
4+
// @filename: node_modules/pkg/package.json
5+
{
6+
"name": "pkg",
7+
"types": "index.d.ts",
8+
"exports": "some-other-thing.js"
9+
}
10+
// @filename: usage.ts
11+
/// <reference types="pkg" />
12+
13+
const a: GlobalThing = { a: 0 };

0 commit comments

Comments
 (0)