Skip to content

Commit fc00047

Browse files
committed
Merge pull request #264 from Microsoft/errorOnHiddenModuleImport
Error when importing shadowed internal module.
2 parents 1f29202 + 3da5f1e commit fc00047

8 files changed

+78
-4
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5734,6 +5734,13 @@ module ts {
57345734
checkSourceElement(node.body);
57355735
}
57365736

5737+
function getFirstIdentifier(node: EntityName): Identifier {
5738+
while (node.kind === SyntaxKind.QualifiedName) {
5739+
node = (<QualifiedName>node).left;
5740+
}
5741+
return <Identifier>node;
5742+
}
5743+
57375744
function checkImportDeclaration(node: ImportDeclaration) {
57385745
checkCollisionWithCapturedThisVariable(node, node.name);
57395746
var symbol = getSymbolOfNode(node);
@@ -5744,8 +5751,15 @@ module ts {
57445751
// Import declaration for an internal module
57455752
if (target !== unknownSymbol) {
57465753
if (target.flags & SymbolFlags.Value) {
5747-
// Target is a value symbol, check that it can be evaluated as an expression
5748-
checkExpression(node.entityName);
5754+
// Target is a value symbol, check that it is not hidden by a local declaration with the same name and
5755+
// ensure it can be evaluated as an expression
5756+
var moduleName = getFirstIdentifier(node.entityName);
5757+
if (resolveEntityName(node, moduleName, SymbolFlags.Value | SymbolFlags.Namespace).flags & SymbolFlags.Namespace) {
5758+
checkExpression(node.entityName);
5759+
}
5760+
else {
5761+
error(moduleName, Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, identifierToString(moduleName));
5762+
}
57495763
}
57505764
if (target.flags & SymbolFlags.Type) {
57515765
checkTypeNameIsReserved(node.name, Diagnostics.Import_name_cannot_be_0);
@@ -5755,7 +5769,6 @@ module ts {
57555769
else {
57565770
// Import declaration for an external module
57575771
if (node.parent.kind === SyntaxKind.SourceFile) {
5758-
// Parent is a source file, check that external modules are enabled
57595772
target = resolveImport(symbol);
57605773
}
57615774
else if (node.parent.kind === SyntaxKind.ModuleBlock && (<ModuleDeclaration>node.parent.parent).name.kind === SyntaxKind.StringLiteral) {

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ module ts {
294294
A_module_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged: { code: -9999999, category: DiagnosticCategory.Error, key: "A module declaration cannot be located prior to a class or function with which it is merged" },
295295
Cannot_compile_external_modules_unless_the_module_flag_is_provided: { code: -9999999, category: DiagnosticCategory.Error, key: "Cannot compile external modules unless the '--module' flag is provided." },
296296
Import_declaration_conflicts_with_local_declaration_of_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Import declaration conflicts with local declaration of '{0}'" },
297+
Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: -9999999, category: DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" },
297298
Filename_0_differs_from_already_included_filename_1_only_in_casing: { code: -9999999, category: DiagnosticCategory.Error, key: "Filename '{0}' differs from already included filename '{1}' only in casing" },
298299
Argument_for_module_option_must_be_commonjs_or_amd: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
299300
Argument_for_target_option_must_be_es3_or_es5: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,10 @@
11971197
"category": "Error",
11981198
"code": -9999999
11991199
},
1200+
"Module '{0}' is hidden by a local declaration with the same name": {
1201+
"category": "Error",
1202+
"code": -9999999
1203+
},
12001204
"Filename '{0}' differs from already included filename '{1}' only in casing": {
12011205
"category": "Error",
12021206
"code": -9999999
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
==== tests/cases/compiler/internalImportInstantiatedModuleMergedWithClassNotReferencingInstance.ts (1 errors) ====
2+
class A {
3+
aProp: string;
4+
}
5+
module A {
6+
export interface X { s: string }
7+
export var a = 10;
8+
}
9+
10+
module B {
11+
var A = 1;
12+
import Y = A;
13+
~
14+
!!! Module 'A' is hidden by a local declaration with the same name
15+
}
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
==== tests/cases/compiler/internalImportInstantiatedModuleNotReferencingInstance.ts (1 errors) ====
2+
module A {
3+
export interface X { s: string }
4+
export var a = 10;
5+
}
6+
7+
module B {
8+
var A = 1;
9+
import Y = A;
10+
~
11+
!!! Module 'A' is hidden by a local declaration with the same name
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
==== tests/cases/compiler/internalImportUnInstantiatedModuleMergedWithClassNotReferencingInstance.ts (1 errors) ====
2+
class A {
3+
aProp: string;
4+
}
5+
module A {
6+
export interface X { s: string }
7+
}
8+
9+
module B {
10+
var A = 1;
11+
import Y = A;
12+
~
13+
!!! Module 'A' is hidden by a local declaration with the same name
14+
}
15+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
==== tests/cases/compiler/reboundIdentifierOnImportAlias.ts (1 errors) ====
2+
module Foo {
3+
export var x = "hello";
4+
}
5+
module Bar {
6+
var Foo = 1;
7+
import F = Foo;
8+
~~~
9+
!!! Module 'Foo' is hidden by a local declaration with the same name
10+
}

tests/baselines/reference/shadowedInternalModule.errors.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
==== tests/cases/conformance/internalModules/importDeclarations/shadowedInternalModule.ts (1 errors) ====
1+
==== tests/cases/conformance/internalModules/importDeclarations/shadowedInternalModule.ts (2 errors) ====
22
// all errors imported modules conflict with local variables
33

44
module A {
@@ -12,6 +12,8 @@
1212
module B {
1313
var A = { x: 0, y: 0 };
1414
import Point = A;
15+
~
16+
!!! Module 'A' is hidden by a local declaration with the same name
1517
}
1618

1719
module X {

0 commit comments

Comments
 (0)