Fix handling of empty 'types', 'typings', etc. fields in package.json (#31539)
This commit is contained in:
parent
bb4080c175
commit
dfd28d2751
|
@ -3927,6 +3927,10 @@
|
|||
"category": "Message",
|
||||
"code": 6219
|
||||
},
|
||||
"'package.json' had a falsy '{0}' field.": {
|
||||
"category": "Message",
|
||||
"code": 6220
|
||||
},
|
||||
|
||||
"Projects to reference": {
|
||||
"category": "Message",
|
||||
|
|
|
@ -141,7 +141,15 @@ namespace ts {
|
|||
|
||||
function readPackageJsonPathField<K extends "typings" | "types" | "main" | "tsconfig">(jsonContent: PackageJson, fieldName: K, baseDirectory: string, state: ModuleResolutionState): PackageJson[K] | undefined {
|
||||
const fileName = readPackageJsonField(jsonContent, fieldName, "string", state);
|
||||
if (fileName === undefined) return;
|
||||
if (fileName === undefined) {
|
||||
return;
|
||||
}
|
||||
if (!fileName) {
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.package_json_had_a_falsy_0_field, fieldName);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const path = normalizePath(combinePaths(baseDirectory, fileName));
|
||||
if (state.traceEnabled) {
|
||||
trace(state.host, Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, fileName, path);
|
||||
|
|
20
tests/baselines/reference/typesVersions.emptyTypes.js
Normal file
20
tests/baselines/reference/typesVersions.emptyTypes.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
//// [tests/cases/conformance/moduleResolution/typesVersions.emptyTypes.ts] ////
|
||||
|
||||
//// [package.json]
|
||||
{
|
||||
"types": "",
|
||||
"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 });
|
|
@ -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))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
[
|
||||
"======== 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.",
|
||||
"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' had a falsy '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'. ========"
|
||||
]
|
9
tests/baselines/reference/typesVersions.emptyTypes.types
Normal file
9
tests/baselines/reference/typesVersions.emptyTypes.types
Normal 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
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
// @baseUrl: /
|
||||
// @traceResolution: true
|
||||
// @target: esnext
|
||||
// @module: commonjs
|
||||
|
||||
// @filename: /a/package.json
|
||||
{
|
||||
"types": "",
|
||||
"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";
|
Loading…
Reference in a new issue