Skip to content

Commit 2f1a3ab

Browse files
committed
If any of the file path option is from node_modules folder, consider only paths in node_modules folder
1 parent c795052 commit 2f1a3ab

6 files changed

+89
-19
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ namespace ts.moduleSpecifiers {
177177
): T | undefined {
178178
const getCanonicalFileName = hostGetCanonicalFileName(host);
179179
const cwd = host.getCurrentDirectory();
180-
const redirects = host.redirectTargetsMap.get(toPath(importedFileName, cwd, getCanonicalFileName));
181-
const importedFileNames = redirects ? [...redirects, importedFileName] : [importedFileName];
180+
const redirects = host.redirectTargetsMap.get(toPath(importedFileName, cwd, getCanonicalFileName)) || emptyArray;
181+
const importedFileNames = [importedFileName, ...redirects];
182182
const targets = importedFileNames.map(f => getNormalizedAbsolutePath(f, cwd));
183183
if (!preferSymlinks) {
184184
const result = forEach(targets, cb);
@@ -216,6 +216,7 @@ namespace ts.moduleSpecifiers {
216216
const cwd = host.getCurrentDirectory();
217217
const getCanonicalFileName = hostGetCanonicalFileName(host);
218218
const allFileNames = createMap<string>();
219+
let importedFileFromNodeModules = false;
219220
forEachFileNameOfModule(
220221
importingFileName,
221222
importedFileName,
@@ -224,6 +225,7 @@ namespace ts.moduleSpecifiers {
224225
path => {
225226
// dont return value, so we collect everything
226227
allFileNames.set(path, getCanonicalFileName(path));
228+
importedFileFromNodeModules = importedFileFromNodeModules || pathContainsNodeModules(path);
227229
}
228230
);
229231

@@ -237,7 +239,10 @@ namespace ts.moduleSpecifiers {
237239
let pathsInDirectory: string[] | undefined;
238240
allFileNames.forEach((canonicalFileName, fileName) => {
239241
if (startsWith(canonicalFileName, directoryStart)) {
240-
(pathsInDirectory || (pathsInDirectory = [])).push(fileName);
242+
// If the importedFile is from node modules, use only paths in node_modules folder as option
243+
if (!importedFileFromNodeModules || pathContainsNodeModules(fileName)) {
244+
(pathsInDirectory || (pathsInDirectory = [])).push(fileName);
245+
}
241246
allFileNames.delete(fileName);
242247
}
243248
});

src/compiler/transformers/declarations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ namespace ts {
374374

375375
// omit references to files from node_modules (npm may disambiguate module
376376
// references when installing this package, making the path is unreliable).
377-
if (startsWith(fileName, "node_modules/") || fileName.indexOf("/node_modules/") !== -1) {
377+
if (startsWith(fileName, "node_modules/") || pathContainsNodeModules(fileName)) {
378378
return;
379379
}
380380

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
tests/cases/compiler/monorepo/pkg3/src/keys.ts(3,14): error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
2+
3+
4+
==== tests/cases/compiler/monorepo/pkg3/tsconfig.json (0 errors) ====
5+
{
6+
"compilerOptions": {
7+
"outDir": "dist",
8+
"rootDir": "src",
9+
"target": "es5",
10+
"module": "commonjs",
11+
"strict": true,
12+
"esModuleInterop": true,
13+
"declaration": true
14+
}
15+
}
16+
17+
==== tests/cases/compiler/monorepo/pkg1/dist/index.d.ts (0 errors) ====
18+
export * from './types';
19+
==== tests/cases/compiler/monorepo/pkg1/dist/types.d.ts (0 errors) ====
20+
export declare type A = {
21+
id: string;
22+
};
23+
export declare type B = {
24+
id: number;
25+
};
26+
export declare type IdType = A | B;
27+
export declare class MetadataAccessor<T, D extends IdType = IdType> {
28+
readonly key: string;
29+
private constructor();
30+
toString(): string;
31+
static create<T, D extends IdType = IdType>(key: string): MetadataAccessor<T, D>;
32+
}
33+
==== tests/cases/compiler/monorepo/pkg1/package.json (0 errors) ====
34+
{
35+
"name": "@raymondfeng/pkg1",
36+
"version": "1.0.0",
37+
"description": "",
38+
"main": "dist/index.js",
39+
"typings": "dist/index.d.ts"
40+
}
41+
==== tests/cases/compiler/monorepo/pkg2/dist/index.d.ts (0 errors) ====
42+
export * from './types';
43+
==== tests/cases/compiler/monorepo/pkg2/dist/types.d.ts (0 errors) ====
44+
export {MetadataAccessor} from '@raymondfeng/pkg1';
45+
==== tests/cases/compiler/monorepo/pkg2/package.json (0 errors) ====
46+
{
47+
"name": "@raymondfeng/pkg2",
48+
"version": "1.0.0",
49+
"description": "",
50+
"main": "dist/index.js",
51+
"typings": "dist/index.d.ts"
52+
}
53+
==== tests/cases/compiler/monorepo/pkg3/src/index.ts (0 errors) ====
54+
export * from './keys';
55+
==== tests/cases/compiler/monorepo/pkg3/src/keys.ts (1 errors) ====
56+
import {MetadataAccessor} from "@raymondfeng/pkg2";
57+
58+
export const ADMIN = MetadataAccessor.create<boolean>('1');
59+
~~~~~
60+
!!! error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.

tests/baselines/reference/declarationEmitReexportedSymlinkReference3.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
6565
__exportStar(require("./keys"), exports);
6666

6767

68-
//// [keys.d.ts]
69-
import { MetadataAccessor } from "@raymondfeng/pkg2";
70-
export declare const ADMIN: MetadataAccessor<boolean, import("../../pkg1/dist").IdType>;
7168
//// [index.d.ts]
7269
export * from './keys';

tests/baselines/reference/tsc/declarationEmit/when-pkg-references-sibling-package-through-indirect-symlink-moduleCaseChange.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ var pkg2_1 = require("@raymondfeng/pkg2");
6262
exports.ADMIN = pkg2_1.MetadataAccessor.create('1');
6363

6464

65-
//// [/user/username/projects/myproject/pkg3/dist/keys.d.ts]
66-
import { MetadataAccessor } from "@raymondfeng/pkg2";
67-
export declare const ADMIN: MetadataAccessor<boolean, import("../../pkg1/dist").IdType>;
68-
69-
7065
//// [/user/username/projects/myproject/pkg3/dist/index.js]
7166
"use strict";
7267
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
@@ -89,6 +84,12 @@ export * from './keys';
8984

9085

9186
Output::
87+
pkg3/src/keys.ts:2:14 - error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
88+
89+
2 export const ADMIN = MetadataAccessor.create<boolean>('1');
90+
   ~~~~~
91+
92+
9293
/a/lib/lib.d.ts
9394

9495
/user/username/projects/myProject/pkg1/dist/types.d.ts
@@ -103,6 +104,9 @@ Output::
103104

104105
/user/username/projects/myproject/pkg3/src/index.ts
105106

107+
108+
Found 1 error.
109+
106110

107111

108112
Program root files: ["/user/username/projects/myproject/pkg3/src/index.ts","/user/username/projects/myproject/pkg3/src/keys.ts"]
@@ -122,4 +126,4 @@ FsWatches::
122126

123127
FsWatchesRecursive::
124128

125-
exitCode:: ExitStatus.Success
129+
exitCode:: ExitStatus.DiagnosticsPresent_OutputsSkipped

tests/baselines/reference/tsc/declarationEmit/when-pkg-references-sibling-package-through-indirect-symlink.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ var pkg2_1 = require("@raymondfeng/pkg2");
6262
exports.ADMIN = pkg2_1.MetadataAccessor.create('1');
6363

6464

65-
//// [/user/username/projects/myproject/pkg3/dist/keys.d.ts]
66-
import { MetadataAccessor } from "@raymondfeng/pkg2";
67-
export declare const ADMIN: MetadataAccessor<boolean, import("../../pkg1/dist").IdType>;
68-
69-
7065
//// [/user/username/projects/myproject/pkg3/dist/index.js]
7166
"use strict";
7267
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
@@ -89,6 +84,12 @@ export * from './keys';
8984

9085

9186
Output::
87+
pkg3/src/keys.ts:2:14 - error TS2742: The inferred type of 'ADMIN' cannot be named without a reference to '../../pkg2/node_modules/@raymondfeng/pkg1/dist'. This is likely not portable. A type annotation is necessary.
88+
89+
2 export const ADMIN = MetadataAccessor.create<boolean>('1');
90+
   ~~~~~
91+
92+
9293
/a/lib/lib.d.ts
9394

9495
/user/username/projects/myproject/pkg1/dist/types.d.ts
@@ -103,6 +104,9 @@ Output::
103104

104105
/user/username/projects/myproject/pkg3/src/index.ts
105106

107+
108+
Found 1 error.
109+
106110

107111

108112
Program root files: ["/user/username/projects/myproject/pkg3/src/index.ts","/user/username/projects/myproject/pkg3/src/keys.ts"]
@@ -122,4 +126,4 @@ FsWatches::
122126

123127
FsWatchesRecursive::
124128

125-
exitCode:: ExitStatus.Success
129+
exitCode:: ExitStatus.DiagnosticsPresent_OutputsSkipped

0 commit comments

Comments
 (0)