mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-12 20:25:48 -06:00
When importing, only check for reserved type names when importing a type.
This commit is contained in:
parent
2af62a9912
commit
9ecf01b57a
@ -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 {
|
||||
|
||||
@ -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'
|
||||
}
|
||||
|
||||
10
tests/baselines/reference/reservedNameOnInterfaceImport.js
Normal file
10
tests/baselines/reference/reservedNameOnInterfaceImport.js
Normal 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]
|
||||
10
tests/baselines/reference/reservedNameOnModuleImport.js
Normal file
10
tests/baselines/reference/reservedNameOnModuleImport.js
Normal 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]
|
||||
@ -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'
|
||||
}
|
||||
|
||||
@ -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]
|
||||
6
tests/cases/compiler/reservedNameOnInterfaceImport.ts
Normal file
6
tests/cases/compiler/reservedNameOnInterfaceImport.ts
Normal 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;
|
||||
}
|
||||
6
tests/cases/compiler/reservedNameOnModuleImport.ts
Normal file
6
tests/cases/compiler/reservedNameOnModuleImport.ts
Normal file
@ -0,0 +1,6 @@
|
||||
declare module test {
|
||||
module mstring { }
|
||||
|
||||
// Should be fine; this does not clobber any declared values.
|
||||
export import string = mstring;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user