Support loading "index.d.ts" using "typesVersions" without "types", "typings", or "main" (#27514)

* Support loading "index.d.ts" using "typesVersions" without "types", "typings", or "main"

* Update baseline
This commit is contained in:
Andy
2018-10-03 11:44:16 -07:00
committed by GitHub
parent 81f8b47e5f
commit 9bdd6a3b55
8 changed files with 112 additions and 29 deletions

View File

@@ -1043,15 +1043,6 @@ namespace ts {
return withPackageId(packageId, loadNodeModuleFromDirectoryWorker(extensions, candidate, onlyRecordFailures, state, packageJsonContent, versionPaths));
}
function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, packageJsonContent: PackageJsonPathFields | undefined, versionPaths: VersionPaths | undefined): PathAndExtension | undefined {
const fromPackageJson = packageJsonContent && loadModuleFromPackageJson(packageJsonContent, versionPaths, extensions, candidate, state);
if (fromPackageJson) {
return fromPackageJson;
}
const directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host);
return loadModuleFromFile(extensions, combinePaths(candidate, "index"), !directoryExists, state);
}
interface PackageJsonInfo {
packageJsonContent: PackageJsonPathFields | undefined;
packageId: PackageId | undefined;
@@ -1111,22 +1102,12 @@ namespace ts {
}
}
function loadModuleFromPackageJson(jsonContent: PackageJsonPathFields, versionPaths: VersionPaths | undefined, extensions: Extensions, candidate: string, state: ModuleResolutionState): PathAndExtension | undefined {
let file = extensions !== Extensions.JavaScript && extensions !== Extensions.Json
? readPackageJsonTypesFields(jsonContent, candidate, state)
: readPackageJsonMainField(jsonContent, candidate, state);
if (!file) {
if (extensions === Extensions.TypeScript) {
function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, jsonContent: PackageJsonPathFields | undefined, versionPaths: VersionPaths | undefined): PathAndExtension | undefined {
const packageFile = jsonContent && (extensions !== Extensions.JavaScript && extensions !== Extensions.Json
? readPackageJsonTypesFields(jsonContent, candidate, state) ||
// When resolving typescript modules, try resolving using main field as well
file = readPackageJsonMainField(jsonContent, candidate, state);
if (!file) {
return undefined;
}
}
else {
return undefined;
}
}
(extensions === Extensions.TypeScript ? readPackageJsonMainField(jsonContent, candidate, state) : undefined)
: readPackageJsonMainField(jsonContent, candidate, state));
const loader: ResolutionKindSpecificLoader = (extensions, candidate, onlyRecordFailures, state) => {
const fromFile = tryFile(candidate, onlyRecordFailures, state);
@@ -1146,21 +1127,26 @@ namespace ts {
return nodeLoadModuleByRelativeName(nextExtensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ false);
};
const onlyRecordFailures = !directoryProbablyExists(getDirectoryPath(file), state.host);
const onlyRecordFailuresForPackageFile = packageFile ? !directoryProbablyExists(getDirectoryPath(packageFile), state.host) : undefined;
const onlyRecordFailuresForIndex = onlyRecordFailures || !directoryProbablyExists(candidate, state.host);
const indexPath = combinePaths(candidate, "index");
if (versionPaths && containsPath(candidate, file)) {
const moduleName = getRelativePathFromDirectory(candidate, file, /*ignoreCase*/ false);
if (versionPaths && (!packageFile || containsPath(candidate, packageFile))) {
const moduleName = getRelativePathFromDirectory(candidate, packageFile || indexPath, /*ignoreCase*/ false);
if (state.traceEnabled) {
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, moduleName);
}
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, loader, onlyRecordFailures, state);
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state);
if (result) {
return removeIgnoredPackageId(result.value);
}
}
// It won't have a `packageId` set, because we disabled `considerPackageJson`.
return removeIgnoredPackageId(loader(extensions, file, onlyRecordFailures, state));
const packageFileResult = packageFile && removeIgnoredPackageId(loader(extensions, packageFile, onlyRecordFailuresForPackageFile!, state));
if (packageFileResult) return packageFileResult;
return loadModuleFromFile(extensions, indexPath, onlyRecordFailuresForIndex, state);
}
/** Resolve from an arbitrarily specified file. Return `undefined` if it has an unsupported extension. */

View File

@@ -32,6 +32,10 @@
"File 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other.ts' does not exist.",
"File 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other.tsx' does not exist.",
"File 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other.d.ts' does not exist.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
"Loading module as file / folder, candidate module location 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other/ts3.1/index', target file type 'TypeScript'.",
"Directory 'tests/cases/conformance/moduleResolution/node_modules/@types' does not exist, skipping all lookups in it.",
"Directory 'tests/cases/conformance/node_modules' does not exist, skipping all lookups in it.",
"Directory 'tests/cases/node_modules' does not exist, skipping all lookups in it.",
@@ -46,6 +50,10 @@
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/other'.",
"File 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other.js' does not exist.",
"File 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other.jsx' does not exist.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
"Loading module as file / folder, candidate module location 'tests/cases/conformance/moduleResolution/node_modules/ext/ts3.1/other/ts3.1/index', target file type 'JavaScript'.",
"Directory 'tests/cases/conformance/node_modules' does not exist, skipping all lookups in it.",
"Directory 'tests/cases/node_modules' does not exist, skipping all lookups in it.",
"Directory 'tests/node_modules' does not exist, skipping all lookups in it.",

View File

@@ -0,0 +1,19 @@
//// [tests/cases/conformance/moduleResolution/typesVersions.justIndex.ts] ////
//// [package.json]
{
"typesVersions": {
">=3.1.0-0": { "*" : ["ts3.1/*"] }
}
}
//// [index.d.ts]
export const a = 0;
//// [user.ts]
import { a } from "a";
//// [user.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -0,0 +1,8 @@
=== /a/ts3.1/index.d.ts ===
export const a = 0;
>a : Symbol(a, Decl(index.d.ts, 0, 12))
=== /b/user.ts ===
import { a } from "a";
>a : Symbol(a, Decl(user.ts, 0, 8))

View File

@@ -0,0 +1,28 @@
[
"======== Resolving module 'a' from '/b/user.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'a'.",
"Resolving module name 'a' relative to base url '/' - '/a'.",
"Loading module as file / folder, candidate module location '/a', target file type 'TypeScript'.",
"File '/a.ts' does not exist.",
"File '/a.tsx' does not exist.",
"File '/a.d.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' does not have a 'main' field.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"Found 'package.json' at '/a/package.json'.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' does not have a 'main' field.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
"File '/a/ts3.1/index' does not exist.",
"Loading module as file / folder, candidate module location '/a/ts3.1/index', target file type 'TypeScript'.",
"File '/a/ts3.1/index.ts' does not exist.",
"File '/a/ts3.1/index.tsx' does not exist.",
"File '/a/ts3.1/index.d.ts' exist - use it as a name resolution result.",
"======== Module name 'a' was successfully resolved to '/a/ts3.1/index.d.ts'. ========"
]

View File

@@ -0,0 +1,9 @@
=== /a/ts3.1/index.d.ts ===
export const a = 0;
>a : 0
>0 : 0
=== /b/user.ts ===
import { a } from "a";
>a : 0

View File

@@ -32,6 +32,10 @@
"File 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other.ts' does not exist.",
"File 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other.tsx' does not exist.",
"File 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other.d.ts' does not exist.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
"Loading module as file / folder, candidate module location 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other/ts3.1/index', target file type 'TypeScript'.",
"Directory 'tests/cases/conformance/declarationEmit/node_modules/@types' does not exist, skipping all lookups in it.",
"Directory 'tests/cases/conformance/node_modules' does not exist, skipping all lookups in it.",
"Directory 'tests/cases/node_modules' does not exist, skipping all lookups in it.",
@@ -46,6 +50,10 @@
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/other'.",
"File 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other.js' does not exist.",
"File 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other.jsx' does not exist.",
"'package.json' has a 'typesVersions' entry '>=3.1.0-0' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'index'.",
"Module name 'index', matched pattern '*'.",
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
"Loading module as file / folder, candidate module location 'tests/cases/conformance/declarationEmit/node_modules/ext/ts3.1/other/ts3.1/index', target file type 'JavaScript'.",
"Directory 'tests/cases/conformance/node_modules' does not exist, skipping all lookups in it.",
"Directory 'tests/cases/node_modules' does not exist, skipping all lookups in it.",
"Directory 'tests/node_modules' does not exist, skipping all lookups in it.",

View File

@@ -0,0 +1,17 @@
// @baseUrl: /
// @traceResolution: true
// @target: esnext
// @module: commonjs
// @filename: /a/package.json
{
"typesVersions": {
">=3.1.0-0": { "*" : ["ts3.1/*"] }
}
}
// @filename: /a/ts3.1/index.d.ts
export const a = 0;
// @filename: /b/user.ts
import { a } from "a";