Skip to content

Commit

Permalink
Fix handling of empty 'types', 'typings', etc. fields in package.json (
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed May 24, 2019
1 parent bb4080c commit dfd28d2
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -3927,6 +3927,10 @@
"category": "Message",
"code": 6219
},
"'package.json' had a falsy '{0}' field.": {
"category": "Message",
"code": 6220
},

"Projects to reference": {
"category": "Message",
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/moduleNameResolver.ts
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/typesVersions.emptyTypes.js
@@ -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 });
8 changes: 8 additions & 0 deletions tests/baselines/reference/typesVersions.emptyTypes.symbols
@@ -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))

24 changes: 24 additions & 0 deletions tests/baselines/reference/typesVersions.emptyTypes.trace.json
@@ -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 changes: 9 additions & 0 deletions tests/baselines/reference/typesVersions.emptyTypes.types
@@ -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";

0 comments on commit dfd28d2

Please sign in to comment.