Skip to content

Commit 5338085

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

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

src/compiler/checker.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,13 @@ 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
}
505505
// obtain item referenced by 'export='
506506
mainModule = resolveExternalModuleSymbol(mainModule);
507507
if (mainModule.flags & SymbolFlags.Namespace) {
508-
// if module symbol has already been merged - it is safe to use it.
509-
// otherwise clone it
510508
mainModule = mainModule.flags & SymbolFlags.Merged ? mainModule : cloneSymbol(mainModule);
511509
mergeSymbol(mainModule, moduleAugmentation.symbol);
512510
}
@@ -1351,16 +1349,16 @@ namespace ts {
13511349
return resolveExternalModuleNameWorker(location, moduleReferenceExpression, Diagnostics.Cannot_find_module_0);
13521350
}
13531351

1354-
function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage): Symbol {
1352+
function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage, isForAugmentation = false): Symbol {
13551353
if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral) {
13561354
return;
13571355
}
13581356

13591357
const moduleReferenceLiteral = <LiteralExpression>moduleReferenceExpression;
1360-
return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral);
1358+
return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral, isForAugmentation);
13611359
}
13621360

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

13821380
const resolvedModule = getResolvedModule(getSourceFileOfNode(location), moduleReference);
1383-
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
1381+
let resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
13841382
const sourceFile = resolvedModule && !resolutionDiagnostic && host.getSourceFile(resolvedModule.resolvedFileName);
13851383
if (sourceFile) {
13861384
if (sourceFile.symbol) {
@@ -1403,7 +1401,10 @@ namespace ts {
14031401

14041402
// May be an untyped module. If so, ignore resolutionDiagnostic.
14051403
if (!isRelative && resolvedModule && !extensionIsTypeScript(resolvedModule.extension)) {
1406-
if (compilerOptions.noImplicitAny) {
1404+
if (isForAugmentation) {
1405+
resolutionDiagnostic = Diagnostics.Invalid_module_name_in_augmentation_module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented;
1406+
}
1407+
else if (compilerOptions.noImplicitAny) {
14071408
if (moduleNotFoundError) {
14081409
error(errorNode,
14091410
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
@@ -1412,15 +1413,17 @@ namespace ts {
14121413
}
14131414
return undefined;
14141415
}
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;
1416+
else {
1417+
// Create a new symbol to represent the untyped module and store it in globals.
1418+
// This provides a name to the module. See the test tests/cases/fourslash/untypedModuleImport.ts
1419+
const newSymbol = createSymbol(SymbolFlags.ValueModule, quotedName);
1420+
//newSymbol.declarations = []; //want this?
1421+
// Module symbols are expected to have 'exports', although since this is an untyped module it can be empty.
1422+
newSymbol.exports = createMap<Symbol>();
1423+
// Cache it so subsequent accesses will return the same module.
1424+
globals[quotedName] = newSymbol;
1425+
return newSymbol;
1426+
}
14241427
}
14251428

14261429
if (moduleNotFoundError) {

src/compiler/diagnosticMessages.json

+4
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
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+
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;
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)