Skip to content

Error when importing shadowed internal module. #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5720,6 +5720,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);
Expand All @@ -5730,8 +5737,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);
Expand All @@ -5741,7 +5755,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) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,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'." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

Original file line number Diff line number Diff line change
@@ -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
}

Original file line number Diff line number Diff line change
@@ -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
}

Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion tests/baselines/reference/shadowedInternalModule.errors.txt
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down