Skip to content

Commit dfd28d2

Browse files
authored
Fix handling of empty 'types', 'typings', etc. fields in package.json (#31539)
1 parent bb4080c commit dfd28d2

7 files changed

+92
-1
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3927,6 +3927,10 @@
39273927
"category": "Message",
39283928
"code": 6219
39293929
},
3930+
"'package.json' had a falsy '{0}' field.": {
3931+
"category": "Message",
3932+
"code": 6220
3933+
},
39303934

39313935
"Projects to reference": {
39323936
"category": "Message",

src/compiler/moduleNameResolver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,15 @@ namespace ts {
141141

142142
function readPackageJsonPathField<K extends "typings" | "types" | "main" | "tsconfig">(jsonContent: PackageJson, fieldName: K, baseDirectory: string, state: ModuleResolutionState): PackageJson[K] | undefined {
143143
const fileName = readPackageJsonField(jsonContent, fieldName, "string", state);
144-
if (fileName === undefined) return;
144+
if (fileName === undefined) {
145+
return;
146+
}
147+
if (!fileName) {
148+
if (state.traceEnabled) {
149+
trace(state.host, Diagnostics.package_json_had_a_falsy_0_field, fieldName);
150+
}
151+
return;
152+
}
145153
const path = normalizePath(combinePaths(baseDirectory, fileName));
146154
if (state.traceEnabled) {
147155
trace(state.host, Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, fileName, path);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/conformance/moduleResolution/typesVersions.emptyTypes.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"types": "",
6+
"typesVersions": {
7+
">=3.1.0-0": { "*" : ["ts3.1/*"] }
8+
}
9+
}
10+
11+
//// [index.d.ts]
12+
export const a = 0;
13+
14+
//// [user.ts]
15+
import { a } from "a";
16+
17+
18+
//// [user.js]
19+
"use strict";
20+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== /a/ts3.1/index.d.ts ===
2+
export const a = 0;
3+
>a : Symbol(a, Decl(index.d.ts, 0, 12))
4+
5+
=== /b/user.ts ===
6+
import { a } from "a";
7+
>a : Symbol(a, Decl(user.ts, 0, 8))
8+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
"======== Resolving module 'a' from '/b/user.ts'. ========",
3+
"Module resolution kind is not specified, using 'NodeJs'.",
4+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'a'.",
5+
"Resolving module name 'a' relative to base url '/' - '/a'.",
6+
"Loading module as file / folder, candidate module location '/a', target file type 'TypeScript'.",
7+
"File '/a.ts' does not exist.",
8+
"File '/a.tsx' does not exist.",
9+
"File '/a.d.ts' does not exist.",
10+
"Found 'package.json' at '/a/package.json'.",
11+
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
12+
"'package.json' does not have a 'typings' field.",
13+
"'package.json' had a falsy 'types' field.",
14+
"'package.json' does not have a 'main' field.",
15+
"'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'.",
16+
"Module name 'index', matched pattern '*'.",
17+
"Trying substitution 'ts3.1/*', candidate module location: 'ts3.1/index'.",
18+
"File '/a/ts3.1/index' does not exist.",
19+
"Loading module as file / folder, candidate module location '/a/ts3.1/index', target file type 'TypeScript'.",
20+
"File '/a/ts3.1/index.ts' does not exist.",
21+
"File '/a/ts3.1/index.tsx' does not exist.",
22+
"File '/a/ts3.1/index.d.ts' exist - use it as a name resolution result.",
23+
"======== Module name 'a' was successfully resolved to '/a/ts3.1/index.d.ts'. ========"
24+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== /a/ts3.1/index.d.ts ===
2+
export const a = 0;
3+
>a : 0
4+
>0 : 0
5+
6+
=== /b/user.ts ===
7+
import { a } from "a";
8+
>a : 0
9+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @baseUrl: /
2+
// @traceResolution: true
3+
// @target: esnext
4+
// @module: commonjs
5+
6+
// @filename: /a/package.json
7+
{
8+
"types": "",
9+
"typesVersions": {
10+
">=3.1.0-0": { "*" : ["ts3.1/*"] }
11+
}
12+
}
13+
14+
// @filename: /a/ts3.1/index.d.ts
15+
export const a = 0;
16+
17+
// @filename: /b/user.ts
18+
import { a } from "a";

0 commit comments

Comments
 (0)