Skip to content

Commit dc4a1ce

Browse files
committed
fix(3242): check class name collisions with Object
1 parent 0fccf68 commit dc4a1ce

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

internal/checker/checker.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6718,6 +6718,12 @@ func (c *Checker) checkTypeNameIsReserved(name *ast.Node, message *diagnostics.M
67186718
}
67196719
}
67206720

6721+
func (c *Checker) checkClassNameCollisionWithObject(name *ast.Node) {
6722+
if name.Text() == "Object" && c.program.GetEmitModuleFormatOfFile(ast.GetSourceFileOfNode(name)) < core.ModuleKindES2015 {
6723+
c.error(name, diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_and_above_with_module_0, c.moduleKind.String())
6724+
}
6725+
}
6726+
67216727
func (c *Checker) checkExportsOnMergedDeclarations(node *ast.Node) {
67226728
// If localSymbol is defined on node then node itself is exported - check is required.
67236729
symbol := node.LocalSymbol()
@@ -10175,7 +10181,12 @@ func (c *Checker) checkCollisionsForDeclarationName(node *ast.Node, name *ast.No
1017510181
case name == nil:
1017610182
return
1017710183
case ast.IsClassLike(node):
10178-
c.checkTypeNameIsReserved(name, diagnostics.Class_name_cannot_be_0)
10184+
{
10185+
c.checkTypeNameIsReserved(name, diagnostics.Class_name_cannot_be_0)
10186+
if node.Flags&ast.NodeFlagsAmbient != 0 {
10187+
c.checkClassNameCollisionWithObject(name)
10188+
}
10189+
}
1017910190
case ast.IsEnumDeclaration(node):
1018010191
c.checkTypeNameIsReserved(name, diagnostics.Enum_name_cannot_be_0)
1018110192
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exportAmbientClassNameWithObject.ts(1,22): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.
2+
3+
4+
==== exportAmbientClassNameWithObject.ts (1 errors) ====
5+
export declare class Object {}
6+
~~~~~~
7+
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- old.exportAmbientClassNameWithObject(target=es2015).errors.txt
2+
+++ new.exportAmbientClassNameWithObject(target=es2015).errors.txt
3+
@@= skipped -0, +0 lines =@@
4+
-<no content>
5+
+exportAmbientClassNameWithObject.ts(1,22): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.
6+
+
7+
+
8+
+==== exportAmbientClassNameWithObject.ts (1 errors) ====
9+
+ export declare class Object {}
10+
+ ~~~~~~
11+
+!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.
12+
+

0 commit comments

Comments
 (0)