Skip to content

Commit

Permalink
Use features for selected module resolution rather than all features …
Browse files Browse the repository at this point in the history
…for type reference directives, since they can add restrictions to resolutions
  • Loading branch information
weswigham committed Dec 3, 2021
1 parent 4b794fe commit 48b9027
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,11 @@ namespace ts {
}

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

Node12Default = Imports | SelfName | Exports,

NodeNextDefault = AllFeatures,

EsmMode = 1 << 5,
}

function node12ModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
return nodeNextModuleNameResolverWorker(
NodeResolutionFeatures.Imports | NodeResolutionFeatures.SelfName | NodeResolutionFeatures.Exports,
NodeResolutionFeatures.Node12Default,
moduleName,
containingFile,
compilerOptions,
Expand All @@ -1208,7 +1216,7 @@ namespace ts {
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
return nodeNextModuleNameResolverWorker(
NodeResolutionFeatures.AllFeatures,
NodeResolutionFeatures.NodeNextDefault,
moduleName,
containingFile,
compilerOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/tripleSlashTypesReferenceWithMissingExports.ts] ////

//// [index.d.ts]
interface GlobalThing { a: number }
//// [package.json]
{
"name": "pkg",
"types": "index.d.ts",
"exports": "some-other-thing.js"
}
//// [usage.ts]
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };

//// [usage.js]
/// <reference types="pkg" />
var a = { a: 0 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
interface GlobalThing { a: number }
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
>a : Symbol(GlobalThing.a, Decl(index.d.ts, 0, 23))

=== tests/cases/compiler/usage.ts ===
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };
>a : Symbol(a, Decl(usage.ts, 2, 5))
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
>a : Symbol(a, Decl(usage.ts, 2, 24))

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
interface GlobalThing { a: number }
>a : number

=== tests/cases/compiler/usage.ts ===
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };
>a : GlobalThing
>{ a: 0 } : { a: number; }
>a : number
>0 : 0

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/usage.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.


==== tests/cases/compiler/node_modules/pkg/index.d.ts (0 errors) ====
interface GlobalThing { a: number }
==== tests/cases/compiler/node_modules/pkg/package.json (0 errors) ====
{
"name": "pkg",
"types": "index.d.ts",
"exports": "some-other-thing.js"
}
==== tests/cases/compiler/usage.ts (1 errors) ====
/// <reference types="pkg" />
~~~
!!! error TS2688: Cannot find type definition file for 'pkg'.

const a: GlobalThing = { a: 0 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/tripleSlashTypesReferenceWithMissingExports.ts] ////

//// [index.d.ts]
interface GlobalThing { a: number }
//// [package.json]
{
"name": "pkg",
"types": "index.d.ts",
"exports": "some-other-thing.js"
}
//// [usage.ts]
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };

//// [usage.js]
/// <reference types="pkg" />
const a = { a: 0 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
interface GlobalThing { a: number }
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
>a : Symbol(GlobalThing.a, Decl(index.d.ts, 0, 23))

=== tests/cases/compiler/usage.ts ===
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };
>a : Symbol(a, Decl(usage.ts, 2, 5))
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
>a : Symbol(a, Decl(usage.ts, 2, 24))

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
interface GlobalThing { a: number }
>a : number

=== tests/cases/compiler/usage.ts ===
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };
>a : GlobalThing
>{ a: 0 } : { a: number; }
>a : number
>0 : 0

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/usage.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.


==== tests/cases/compiler/node_modules/pkg/index.d.ts (0 errors) ====
interface GlobalThing { a: number }
==== tests/cases/compiler/node_modules/pkg/package.json (0 errors) ====
{
"name": "pkg",
"types": "index.d.ts",
"exports": "some-other-thing.js"
}
==== tests/cases/compiler/usage.ts (1 errors) ====
/// <reference types="pkg" />
~~~
!!! error TS2688: Cannot find type definition file for 'pkg'.

const a: GlobalThing = { a: 0 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/tripleSlashTypesReferenceWithMissingExports.ts] ////

//// [index.d.ts]
interface GlobalThing { a: number }
//// [package.json]
{
"name": "pkg",
"types": "index.d.ts",
"exports": "some-other-thing.js"
}
//// [usage.ts]
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };

//// [usage.js]
/// <reference types="pkg" />
const a = { a: 0 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
interface GlobalThing { a: number }
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
>a : Symbol(GlobalThing.a, Decl(index.d.ts, 0, 23))

=== tests/cases/compiler/usage.ts ===
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };
>a : Symbol(a, Decl(usage.ts, 2, 5))
>GlobalThing : Symbol(GlobalThing, Decl(index.d.ts, 0, 0))
>a : Symbol(a, Decl(usage.ts, 2, 24))

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
interface GlobalThing { a: number }
>a : number

=== tests/cases/compiler/usage.ts ===
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };
>a : GlobalThing
>{ a: 0 } : { a: number; }
>a : number
>0 : 0

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @module: commonjs,node12,nodenext
// @filename: node_modules/pkg/index.d.ts
interface GlobalThing { a: number }
// @filename: node_modules/pkg/package.json
{
"name": "pkg",
"types": "index.d.ts",
"exports": "some-other-thing.js"
}
// @filename: usage.ts
/// <reference types="pkg" />

const a: GlobalThing = { a: 0 };

0 comments on commit 48b9027

Please sign in to comment.