Skip to content

Commit d7d8f33

Browse files
authored
In bundle declaration emit, refuse to rewrite nonrelative references to files outside the common source directory (#42306)
1 parent 1b57a03 commit d7d8f33

5 files changed

+108
-1
lines changed

src/compiler/utilities.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,7 @@ namespace ts {
24302430
}
24312431
}
24322432

2433-
export function getExternalModuleName(node: AnyImportOrReExport | ImportTypeNode | ImportCall): Expression | undefined {
2433+
export function getExternalModuleName(node: AnyImportOrReExport | ImportTypeNode | ImportCall | ModuleDeclaration): Expression | undefined {
24342434
switch (node.kind) {
24352435
case SyntaxKind.ImportDeclaration:
24362436
case SyntaxKind.ExportDeclaration:
@@ -2441,6 +2441,8 @@ namespace ts {
24412441
return isLiteralImportTypeNode(node) ? node.argument.literal : undefined;
24422442
case SyntaxKind.CallExpression:
24432443
return node.arguments[0];
2444+
case SyntaxKind.ModuleDeclaration:
2445+
return node.name.kind === SyntaxKind.StringLiteral ? node.name : undefined;
24442446
default:
24452447
return Debug.assertNever(node);
24462448
}
@@ -4118,11 +4120,21 @@ namespace ts {
41184120
return file.moduleName || getExternalModuleNameFromPath(host, file.fileName, referenceFile && referenceFile.fileName);
41194121
}
41204122

4123+
function getCanonicalAbsolutePath(host: ResolveModuleNameResolutionHost, path: string) {
4124+
return host.getCanonicalFileName(getNormalizedAbsolutePath(path, host.getCurrentDirectory()));
4125+
}
4126+
41214127
export function getExternalModuleNameFromDeclaration(host: ResolveModuleNameResolutionHost, resolver: EmitResolver, declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration | ImportTypeNode): string | undefined {
41224128
const file = resolver.getExternalModuleFileFromDeclaration(declaration);
41234129
if (!file || file.isDeclarationFile) {
41244130
return undefined;
41254131
}
4132+
// If the declaration already uses a non-relative name, and is outside the common source directory, continue to use it
4133+
const specifier = getExternalModuleName(declaration);
4134+
if (specifier && isStringLiteralLike(specifier) && !pathIsRelative(specifier.text) &&
4135+
getCanonicalAbsolutePath(host, file.path).indexOf(getCanonicalAbsolutePath(host, ensureTrailingDirectorySeparator(host.getCommonSourceDirectory()))) === -1) {
4136+
return undefined;
4137+
}
41264138
return getResolvedExternalModuleName(host, file);
41274139
}
41284140

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/declarationEmitCommonSourceDirectoryDoesNotContainAllFiles.ts] ////
2+
3+
//// [index.ts]
4+
export * from "./src/"
5+
//// [index.ts]
6+
export class B {}
7+
//// [index.ts]
8+
import { B } from "b";
9+
10+
export default function () {
11+
return new B();
12+
}
13+
//// [index.ts]
14+
export * from "./src/"
15+
16+
17+
18+
//// [index.d.ts]
19+
declare module "src/index" {
20+
import { B } from "b";
21+
export default function (): B;
22+
}
23+
declare module "index" {
24+
export * from "src/index";
25+
}
26+
27+
28+
//// [DtsFileErrors]
29+
30+
31+
dist/index.d.ts(2,23): error TS2307: Cannot find module 'b' or its corresponding type declarations.
32+
33+
34+
==== dist/index.d.ts (1 errors) ====
35+
declare module "src/index" {
36+
import { B } from "b";
37+
~~~
38+
!!! error TS2307: Cannot find module 'b' or its corresponding type declarations.
39+
export default function (): B;
40+
}
41+
declare module "index" {
42+
export * from "src/index";
43+
}
44+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== /a/index.ts ===
2+
export * from "./src/"
3+
No type information for this code.=== /b/index.ts ===
4+
export * from "./src/"
5+
No type information for this code.=== /b/src/index.ts ===
6+
export class B {}
7+
>B : Symbol(B, Decl(index.ts, 0, 0))
8+
9+
=== /a/src/index.ts ===
10+
import { B } from "b";
11+
>B : Symbol(B, Decl(index.ts, 0, 8))
12+
13+
export default function () {
14+
return new B();
15+
>B : Symbol(B, Decl(index.ts, 0, 8))
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== /a/index.ts ===
2+
export * from "./src/"
3+
No type information for this code.=== /b/index.ts ===
4+
export * from "./src/"
5+
No type information for this code.=== /b/src/index.ts ===
6+
export class B {}
7+
>B : B
8+
9+
=== /a/src/index.ts ===
10+
import { B } from "b";
11+
>B : typeof B
12+
13+
export default function () {
14+
return new B();
15+
>new B() : B
16+
>B : typeof B
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @declaration: true
2+
// @emitDeclarationOnly: true
3+
// @outFile: dist/index.d.ts
4+
// @currentDirectory: /a
5+
// @noImplicitReferences: true
6+
// @filename: /b/index.ts
7+
export * from "./src/"
8+
// @filename: /b/src/index.ts
9+
export class B {}
10+
// @filename: /a/src/index.ts
11+
import { B } from "b";
12+
13+
export default function () {
14+
return new B();
15+
}
16+
// @filename: /a/index.ts
17+
export * from "./src/"
18+
// @link: /b -> /a/node_modules/b

0 commit comments

Comments
 (0)