When importing, only check for reserved type names when importing a type.

This commit is contained in:
Daniel Rosenwasser 2014-07-16 11:48:34 -07:00
parent 2af62a9912
commit 9ecf01b57a
9 changed files with 84 additions and 10 deletions

View File

@ -4130,7 +4130,7 @@ module ts {
// DECLARATION AND STATEMENT TYPE CHECKING
function checkTypeParameter(node: TypeParameterDeclaration) {
checkNameIsReserved(node.name, Diagnostics.Type_parameter_name_cannot_be_0);
checkTypeNameIsReserved(node.name, Diagnostics.Type_parameter_name_cannot_be_0);
checkSourceElement(node.constraint);
checkTypeParameterHasIllegalReferencesInConstraint(node);
// TODO: Check multiple declarations are identical
@ -5027,7 +5027,7 @@ module ts {
}
}
function checkNameIsReserved(name: Identifier, message: DiagnosticMessage): boolean {
function checkTypeNameIsReserved(name: Identifier, message: DiagnosticMessage): void {
// TS 1.0 spec (April 2014): 3.6.1
// The predefined type keywords are reserved and cannot be used as names of user defined types.
switch (name.text) {
@ -5037,7 +5037,6 @@ module ts {
case "string":
case "void":
error(name, message, name.text);
return true;
}
}
@ -5058,7 +5057,7 @@ module ts {
function checkClassDeclaration(node: ClassDeclaration) {
checkDeclarationModifiers(node);
checkNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
checkTypeParameters(node.typeParameters);
var symbol = getSymbolOfNode(node);
var type = <InterfaceType>getDeclaredTypeOfSymbol(symbol);
@ -5213,7 +5212,7 @@ module ts {
function checkInterfaceDeclaration(node: InterfaceDeclaration) {
checkDeclarationModifiers(node);
checkNameIsReserved(node.name, Diagnostics.Interface_name_cannot_be_0);
checkTypeNameIsReserved(node.name, Diagnostics.Interface_name_cannot_be_0);
checkTypeParameters(node.typeParameters);
var symbol = getSymbolOfNode(node);
var firstInterfaceDecl = <InterfaceDeclaration>getDeclarationOfKind(symbol, SyntaxKind.InterfaceDeclaration);
@ -5259,7 +5258,7 @@ module ts {
function checkEnumDeclaration(node: EnumDeclaration) {
checkDeclarationModifiers(node);
checkNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
var enumSymbol = getSymbolOfNode(node);
var enumType = getDeclaredTypeOfSymbol(enumSymbol);
var autoValue = 0;
@ -5334,16 +5333,20 @@ module ts {
function checkImportDeclaration(node: ImportDeclaration) {
checkDeclarationModifiers(node);
checkNameIsReserved(node.name, Diagnostics.Import_name_cannot_be_0);
var symbol = getSymbolOfNode(node);
var target: Symbol;
if (node.entityName) {
target = resolveImport(symbol);
// Import declaration for an internal module
if (target !== unknownSymbol && target.flags & SymbolFlags.Value) {
// Target is a value symbol, check that it can be evaluated as an expression
checkExpression(node.entityName);
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);
}
if (target.flags & SymbolFlags.Type) {
checkTypeNameIsReserved(node.name, Diagnostics.Import_name_cannot_be_0);
}
}
}
else {

View File

@ -0,0 +1,10 @@
==== tests/cases/compiler/reservedNameOnInterfaceImport.ts (1 errors) ====
declare module test {
interface istring { }
// Should error; 'test.istring' is a type, so this import conflicts with the 'string' type.
import string = test.istring;
~~~~~~
!!! Import name cannot be 'string'
}

View File

@ -0,0 +1,10 @@
//// [reservedNameOnInterfaceImport.ts]
declare module test {
interface istring { }
// Should error; 'test.istring' is a type, so this import conflicts with the 'string' type.
import string = test.istring;
}
//// [reservedNameOnInterfaceImport.js]

View File

@ -0,0 +1,10 @@
//// [reservedNameOnModuleImport.ts]
declare module test {
module mstring { }
// Should be fine; this does not clobber any declared values.
export import string = mstring;
}
//// [reservedNameOnModuleImport.js]

View File

@ -0,0 +1,11 @@
==== tests/cases/compiler/reservedNameOnModuleImportWithInterface.ts (1 errors) ====
declare module test {
interface mi_string { }
module mi_string { }
// Should error; imports both a type and a module, which means it conflicts with the 'string' type.
import string = mi_string;
~~~~~~
!!! Import name cannot be 'string'
}

View File

@ -0,0 +1,11 @@
//// [reservedNameOnModuleImportWithInterface.ts]
declare module test {
interface mi_string { }
module mi_string { }
// Should error; imports both a type and a module, which means it conflicts with the 'string' type.
import string = mi_string;
}
//// [reservedNameOnModuleImportWithInterface.js]

View File

@ -0,0 +1,6 @@
declare module test {
interface istring { }
// Should error; 'test.istring' is a type, so this import conflicts with the 'string' type.
import string = test.istring;
}

View File

@ -0,0 +1,6 @@
declare module test {
module mstring { }
// Should be fine; this does not clobber any declared values.
export import string = mstring;
}

View File

@ -0,0 +1,7 @@
declare module test {
interface mi_string { }
module mi_string { }
// Should error; imports both a type and a module, which means it conflicts with the 'string' type.
import string = mi_string;
}