Skip to content

When importing, only check for reserved type names when importing a type #31

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 13 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4118,7 +4118,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
Expand Down Expand Up @@ -5015,7 +5015,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) {
Expand All @@ -5025,7 +5025,6 @@ module ts {
case "string":
case "void":
error(name, message, name.text);
return true;
}
}

Expand All @@ -5046,7 +5045,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);
Expand Down Expand Up @@ -5201,7 +5200,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);
if (symbol.declarations.length > 1) {
Expand Down Expand Up @@ -5248,7 +5247,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 enumType = getDeclaredTypeOfSymbol(getSymbolOfNode(node));
var autoValue = 0;
var ambient = isInAmbientContext(node);
Expand Down Expand Up @@ -5296,16 +5295,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 {
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/reservedNameOnInterfaceImport.errors.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions tests/baselines/reference/reservedNameOnInterfaceImport.js
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions tests/baselines/reference/reservedNameOnModuleImport.js
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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'
}

Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions tests/cases/compiler/reservedNameOnInterfaceImport.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions tests/cases/compiler/reservedNameOnModuleImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module test {
module mstring { }

// Should be fine; this does not clobber any declared values.
export import string = mstring;
}
Original file line number Diff line number Diff line change
@@ -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;
}