Skip to content

Commit 0c3019e

Browse files
authored
Handle invalid package.json typings fields when generating specifiers (#36137)
Fixes #35437
1 parent 00b21ef commit 0c3019e

5 files changed

+139
-1
lines changed

src/compiler/moduleSpecifiers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ namespace ts.moduleSpecifiers {
342342
// If the file is the main module, it can be imported by the package name
343343
if (packageJsonContent) {
344344
const mainFileRelative = packageJsonContent.typings || packageJsonContent.types || packageJsonContent.main;
345-
if (mainFileRelative) {
345+
if (isString(mainFileRelative)) {
346346
const mainExportFile = toPath(mainFileRelative, packageRootPath, getCanonicalFileName);
347347
if (removeFileExtension(mainExportFile) === removeFileExtension(getCanonicalFileName(path))) {
348348
return packageRootPath;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [tests/cases/compiler/declarationEmitWithInvalidPackageJsonTypings.ts] ////
2+
3+
//// [index.d.ts]
4+
export function bar(): number;
5+
//// [package.json]
6+
{
7+
"main": "./lib",
8+
"name": "csv-parse",
9+
"types": [
10+
"./lib/index.d.ts",
11+
"./lib/sync.d.ts"
12+
],
13+
"version": "4.8.2"
14+
}
15+
//// [index.ts]
16+
export interface MutableRefObject<T> {
17+
current: T;
18+
}
19+
export function useRef<T>(current: T): MutableRefObject<T> {
20+
return { current };
21+
}
22+
export const useCsvParser = () => {
23+
const parserRef = useRef<typeof import("csv-parse")>(null);
24+
return parserRef;
25+
};
26+
27+
28+
//// [index.js]
29+
"use strict";
30+
exports.__esModule = true;
31+
function useRef(current) {
32+
return { current: current };
33+
}
34+
exports.useRef = useRef;
35+
exports.useCsvParser = function () {
36+
var parserRef = useRef(null);
37+
return parserRef;
38+
};
39+
40+
41+
//// [index.d.ts]
42+
export interface MutableRefObject<T> {
43+
current: T;
44+
}
45+
export declare function useRef<T>(current: T): MutableRefObject<T>;
46+
export declare const useCsvParser: () => MutableRefObject<typeof import("csv-parse/lib")>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== /p1/node_modules/csv-parse/lib/index.d.ts ===
2+
export function bar(): number;
3+
>bar : Symbol(bar, Decl(index.d.ts, 0, 0))
4+
5+
=== /p1/index.ts ===
6+
export interface MutableRefObject<T> {
7+
>MutableRefObject : Symbol(MutableRefObject, Decl(index.ts, 0, 0))
8+
>T : Symbol(T, Decl(index.ts, 0, 34))
9+
10+
current: T;
11+
>current : Symbol(MutableRefObject.current, Decl(index.ts, 0, 38))
12+
>T : Symbol(T, Decl(index.ts, 0, 34))
13+
}
14+
export function useRef<T>(current: T): MutableRefObject<T> {
15+
>useRef : Symbol(useRef, Decl(index.ts, 2, 1))
16+
>T : Symbol(T, Decl(index.ts, 3, 23))
17+
>current : Symbol(current, Decl(index.ts, 3, 26))
18+
>T : Symbol(T, Decl(index.ts, 3, 23))
19+
>MutableRefObject : Symbol(MutableRefObject, Decl(index.ts, 0, 0))
20+
>T : Symbol(T, Decl(index.ts, 3, 23))
21+
22+
return { current };
23+
>current : Symbol(current, Decl(index.ts, 4, 12))
24+
}
25+
export const useCsvParser = () => {
26+
>useCsvParser : Symbol(useCsvParser, Decl(index.ts, 6, 12))
27+
28+
const parserRef = useRef<typeof import("csv-parse")>(null);
29+
>parserRef : Symbol(parserRef, Decl(index.ts, 7, 9))
30+
>useRef : Symbol(useRef, Decl(index.ts, 2, 1))
31+
32+
return parserRef;
33+
>parserRef : Symbol(parserRef, Decl(index.ts, 7, 9))
34+
35+
};
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== /p1/node_modules/csv-parse/lib/index.d.ts ===
2+
export function bar(): number;
3+
>bar : () => number
4+
5+
=== /p1/index.ts ===
6+
export interface MutableRefObject<T> {
7+
current: T;
8+
>current : T
9+
}
10+
export function useRef<T>(current: T): MutableRefObject<T> {
11+
>useRef : <T>(current: T) => MutableRefObject<T>
12+
>current : T
13+
14+
return { current };
15+
>{ current } : { current: T; }
16+
>current : T
17+
}
18+
export const useCsvParser = () => {
19+
>useCsvParser : () => MutableRefObject<typeof import("/p1/node_modules/csv-parse/lib/index")>
20+
>() => { const parserRef = useRef<typeof import("csv-parse")>(null); return parserRef;} : () => MutableRefObject<typeof import("/p1/node_modules/csv-parse/lib/index")>
21+
22+
const parserRef = useRef<typeof import("csv-parse")>(null);
23+
>parserRef : MutableRefObject<typeof import("/p1/node_modules/csv-parse/lib/index")>
24+
>useRef<typeof import("csv-parse")>(null) : MutableRefObject<typeof import("/p1/node_modules/csv-parse/lib/index")>
25+
>useRef : <T>(current: T) => MutableRefObject<T>
26+
>null : null
27+
28+
return parserRef;
29+
>parserRef : MutableRefObject<typeof import("/p1/node_modules/csv-parse/lib/index")>
30+
31+
};
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @declaration: true
2+
// @filename: /p1/node_modules/csv-parse/lib/index.d.ts
3+
export function bar(): number;
4+
// @filename: /p1/node_modules/csv-parse/package.json
5+
{
6+
"main": "./lib",
7+
"name": "csv-parse",
8+
"types": [
9+
"./lib/index.d.ts",
10+
"./lib/sync.d.ts"
11+
],
12+
"version": "4.8.2"
13+
}
14+
// @filename: /p1/index.ts
15+
export interface MutableRefObject<T> {
16+
current: T;
17+
}
18+
export function useRef<T>(current: T): MutableRefObject<T> {
19+
return { current };
20+
}
21+
export const useCsvParser = () => {
22+
const parserRef = useRef<typeof import("csv-parse")>(null);
23+
return parserRef;
24+
};

0 commit comments

Comments
 (0)