Skip to content

Commit b1c0282

Browse files
author
Andy Hanson
committed
Forbid augmentation of untyped module
1 parent 44ce59d commit b1c0282

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ namespace ts {
498498
const moduleNotFoundError = !isInAmbientContext(moduleName.parent.parent)
499499
? Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found
500500
: undefined;
501-
let mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError);
501+
let mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError, /*isForAugmentation*/ true);
502502
if (!mainModule) {
503503
return;
504504
}
@@ -1351,16 +1351,16 @@ namespace ts {
13511351
return resolveExternalModuleNameWorker(location, moduleReferenceExpression, Diagnostics.Cannot_find_module_0);
13521352
}
13531353

1354-
function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage): Symbol {
1354+
function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage, isForAugmentation = false): Symbol {
13551355
if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral) {
13561356
return;
13571357
}
13581358

13591359
const moduleReferenceLiteral = <LiteralExpression>moduleReferenceExpression;
1360-
return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral);
1360+
return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral, isForAugmentation);
13611361
}
13621362

1363-
function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage, errorNode: Node): Symbol {
1363+
function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage, errorNode: Node, isForAugmentation = false): Symbol {
13641364
// Module names are escaped in our symbol table. However, string literal values aren't.
13651365
// Escape the name in the "require(...)" clause to ensure we find the right symbol.
13661366
const moduleName = escapeIdentifier(moduleReference);
@@ -1380,7 +1380,7 @@ namespace ts {
13801380
}
13811381

13821382
const resolvedModule = getResolvedModule(getSourceFileOfNode(location), moduleReference);
1383-
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
1383+
let resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
13841384
const sourceFile = resolvedModule && !resolutionDiagnostic && host.getSourceFile(resolvedModule.resolvedFileName);
13851385
if (sourceFile) {
13861386
if (sourceFile.symbol) {
@@ -1403,7 +1403,10 @@ namespace ts {
14031403

14041404
// May be an untyped module. If so, ignore resolutionDiagnostic.
14051405
if (!isRelative && resolvedModule && !extensionIsTypeScript(resolvedModule.extension)) {
1406-
if (compilerOptions.noImplicitAny) {
1406+
if (isForAugmentation) {
1407+
resolutionDiagnostic = Diagnostics.Invalid_module_name_in_augmentation_module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented;
1408+
}
1409+
else if (compilerOptions.noImplicitAny) {
14071410
if (moduleNotFoundError) {
14081411
error(errorNode,
14091412
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
@@ -1412,15 +1415,17 @@ namespace ts {
14121415
}
14131416
return undefined;
14141417
}
1415-
1416-
// Create a new symbol to represent the untyped module and store it in globals.
1417-
// This provides a name to the module. See the test tests/cases/fourslash/untypedModuleImport.ts
1418-
const newSymbol = createSymbol(SymbolFlags.ValueModule, quotedName);
1419-
// Module symbols are expected to have 'exports', although since this is an untyped module it can be empty.
1420-
newSymbol.exports = createMap<Symbol>();
1421-
// Cache it so subsequent accesses will return the same module.
1422-
globals[quotedName] = newSymbol;
1423-
return newSymbol;
1418+
else {
1419+
// Create a new symbol to represent the untyped module and store it in globals.
1420+
// This provides a name to the module. See the test tests/cases/fourslash/untypedModuleImport.ts
1421+
const newSymbol = createSymbol(SymbolFlags.ValueModule, quotedName);
1422+
//newSymbol.declarations = []; //want this?
1423+
// Module symbols are expected to have 'exports', although since this is an untyped module it can be empty.
1424+
newSymbol.exports = createMap<Symbol>();
1425+
// Cache it so subsequent accesses will return the same module.
1426+
globals[quotedName] = newSymbol;
1427+
return newSymbol;
1428+
}
14241429
}
14251430

14261431
if (moduleNotFoundError) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,10 @@
18391839
"category": "Error",
18401840
"code": 2664
18411841
},
1842+
"Invalid module name in augmentation. Module '{0}' resolves to an untyped module at '{1}', which cannot be augmented.": {
1843+
"category": "Error",
1844+
"code": 2665
1845+
},
18421846
"Exports and export assignments are not permitted in module augmentations.": {
18431847
"category": "Error",
18441848
"code": 2666
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/a.ts(1,16): error TS2665: Invalid module name in augmentation, module 'foo' resolves to an untyped module at '/node_modules/foo/index.js', which cannot be augmented.
2+
3+
4+
==== /a.ts (1 errors) ====
5+
declare module "foo" {
6+
~~~~~
7+
!!! error TS2665: Invalid module name in augmentation, module 'foo' resolves to an untyped module at '/node_modules/foo/index.js', which cannot be augmented.
8+
export const x: number;
9+
}
10+
import { x } from "foo";
11+
x;
12+
13+
==== /node_modules/foo/index.js (0 errors) ====
14+
// This tests that augmenting an untyped module is forbidden.
15+
16+
This file is not processed.
17+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_withAugmentation.ts] ////
2+
3+
//// [index.js]
4+
// This tests that augmenting an untyped module is forbidden.
5+
6+
This file is not processed.
7+
8+
//// [a.ts]
9+
declare module "foo" {
10+
export const x: number;
11+
}
12+
import { x } from "foo";
13+
x;
14+
15+
16+
//// [a.js]
17+
"use strict";
18+
var foo_1 = require("foo");
19+
foo_1.x;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @noImplicitReferences: true
2+
// @currentDirectory: /
3+
// This tests that augmenting an untyped module is forbidden.
4+
5+
// @filename: /node_modules/foo/index.js
6+
This file is not processed.
7+
8+
// @filename: /a.ts
9+
declare module "foo" {
10+
export const x: number;
11+
}
12+
import { x } from "foo";
13+
x;

0 commit comments

Comments
 (0)