Skip to content

Commit

Permalink
🤖 Pick PR #47007 (Use features for selected module re...) into releas…
Browse files Browse the repository at this point in the history
…e-4.5 (#47016)

* Cherry-pick PR #47007 into release-4.5

Component commits:
48b9027 Use features for selected module resolution rather than all features for type reference directives, since they can add restrictions to resolutions

* Update baselines for error in stable release versions

Co-authored-by: Wesley Wigham <[email protected]>
Co-authored-by: Andrew Branch <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2021
1 parent c1758b5 commit d710e39
Show file tree
Hide file tree
Showing 13 changed files with 194 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,19 @@
error TS4124: Compiler option 'module' of value 'node12' is unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
tests/cases/compiler/usage.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.


!!! error TS4124: Compiler option 'module' of value 'node12' is unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
==== 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,19 @@
error TS4124: Compiler option 'module' of value 'nodenext' is unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
tests/cases/compiler/usage.ts(1,23): error TS2688: Cannot find type definition file for 'pkg'.


!!! error TS4124: Compiler option 'module' of value 'nodenext' is unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
==== 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 d710e39

Please sign in to comment.