Merge pull request #264 from Microsoft/errorOnHiddenModuleImport

Error when importing shadowed internal module.
This commit is contained in:
Anders Hejlsberg 2014-07-27 18:09:04 -07:00
commit fc0004749f
8 changed files with 78 additions and 4 deletions

View File

@ -5734,6 +5734,13 @@ module ts {
checkSourceElement(node.body);
}
function getFirstIdentifier(node: EntityName): Identifier {
while (node.kind === SyntaxKind.QualifiedName) {
node = (<QualifiedName>node).left;
}
return <Identifier>node;
}
function checkImportDeclaration(node: ImportDeclaration) {
checkCollisionWithCapturedThisVariable(node, node.name);
var symbol = getSymbolOfNode(node);
@ -5744,8 +5751,15 @@ module ts {
// Import declaration for an internal module
if (target !== unknownSymbol) {
if (target.flags & SymbolFlags.Value) {
// Target is a value symbol, check that it can be evaluated as an expression
checkExpression(node.entityName);
// Target is a value symbol, check that it is not hidden by a local declaration with the same name and
// ensure it can be evaluated as an expression
var moduleName = getFirstIdentifier(node.entityName);
if (resolveEntityName(node, moduleName, SymbolFlags.Value | SymbolFlags.Namespace).flags & SymbolFlags.Namespace) {
checkExpression(node.entityName);
}
else {
error(moduleName, Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, identifierToString(moduleName));
}
}
if (target.flags & SymbolFlags.Type) {
checkTypeNameIsReserved(node.name, Diagnostics.Import_name_cannot_be_0);
@ -5755,7 +5769,6 @@ module ts {
else {
// Import declaration for an external module
if (node.parent.kind === SyntaxKind.SourceFile) {
// Parent is a source file, check that external modules are enabled
target = resolveImport(symbol);
}
else if (node.parent.kind === SyntaxKind.ModuleBlock && (<ModuleDeclaration>node.parent.parent).name.kind === SyntaxKind.StringLiteral) {

View File

@ -294,6 +294,7 @@ module ts {
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" },
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." },
Import_declaration_conflicts_with_local_declaration_of_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Import declaration conflicts with local declaration of '{0}'" },
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" },
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" },
Argument_for_module_option_must_be_commonjs_or_amd: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
Argument_for_target_option_must_be_es3_or_es5: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },

View File

@ -1197,6 +1197,10 @@
"category": "Error",
"code": -9999999
},
"Module '{0}' is hidden by a local declaration with the same name": {
"category": "Error",
"code": -9999999
},
"Filename '{0}' differs from already included filename '{1}' only in casing": {
"category": "Error",
"code": -9999999

View File

@ -0,0 +1,16 @@
==== tests/cases/compiler/internalImportInstantiatedModuleMergedWithClassNotReferencingInstance.ts (1 errors) ====
class A {
aProp: string;
}
module A {
export interface X { s: string }
export var a = 10;
}
module B {
var A = 1;
import Y = A;
~
!!! Module 'A' is hidden by a local declaration with the same name
}

View File

@ -0,0 +1,13 @@
==== tests/cases/compiler/internalImportInstantiatedModuleNotReferencingInstance.ts (1 errors) ====
module A {
export interface X { s: string }
export var a = 10;
}
module B {
var A = 1;
import Y = A;
~
!!! Module 'A' is hidden by a local declaration with the same name
}

View File

@ -0,0 +1,15 @@
==== tests/cases/compiler/internalImportUnInstantiatedModuleMergedWithClassNotReferencingInstance.ts (1 errors) ====
class A {
aProp: string;
}
module A {
export interface X { s: string }
}
module B {
var A = 1;
import Y = A;
~
!!! Module 'A' is hidden by a local declaration with the same name
}

View File

@ -0,0 +1,10 @@
==== tests/cases/compiler/reboundIdentifierOnImportAlias.ts (1 errors) ====
module Foo {
export var x = "hello";
}
module Bar {
var Foo = 1;
import F = Foo;
~~~
!!! Module 'Foo' is hidden by a local declaration with the same name
}

View File

@ -1,4 +1,4 @@
==== tests/cases/conformance/internalModules/importDeclarations/shadowedInternalModule.ts (1 errors) ====
==== tests/cases/conformance/internalModules/importDeclarations/shadowedInternalModule.ts (2 errors) ====
// all errors imported modules conflict with local variables
module A {
@ -12,6 +12,8 @@
module B {
var A = { x: 0, y: 0 };
import Point = A;
~
!!! Module 'A' is hidden by a local declaration with the same name
}
module X {