Skip to content
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

fix: diagnostic message for class name 'Object' when targeting different ECMAScript versions #61358

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46005,7 +46005,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
languageVersion >= ScriptTarget.ES5 && name.escapedText === "Object"
&& host.getEmitModuleFormatOfFile(getSourceFileOfNode(name)) < ModuleKind.ES2015
) {
error(name, Diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_with_module_0, ModuleKind[moduleKind]); // https://github.com/Microsoft/TypeScript/issues/17494
// https://github.com/Microsoft/TypeScript/issues/17494
error(
name,
Diagnostics.Class_name_cannot_be_Object_when_targeting_0_with_module_1,
ScriptTarget[languageVersion],
ModuleKind[moduleKind],
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,7 @@
"category": "Error",
"code": 2724
},
"Class name cannot be 'Object' when targeting ES5 with module {0}.": {
"Class name cannot be 'Object' when targeting {0} with module {1}.": {
"category": "Error",
"code": 2725
},
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7598,7 +7598,7 @@ export const enum ScriptKind {
// June of each year. This includes changes to `LanguageFeatureMinimumTarget`, `ScriptTarget`,
// `ScriptTargetFeatures` transformers/esnext.ts, compiler/commandLineParser.ts,
// compiler/utilitiesPublic.ts, and the contents of each lib/esnext.*.d.ts file.
export const enum ScriptTarget {
export enum ScriptTarget {
/** @deprecated */
ES3 = 0,
ES5 = 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node16.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
!!! error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node16.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node18.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
!!! error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node18.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting Latest with module NodeNext.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
!!! error TS2725: Class name cannot be 'Object' when targeting Latest with module NodeNext.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node16.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
!!! error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node16.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node18.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
!!! error TS2725: Class name cannot be 'Object' when targeting ES2022 with module Node18.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting Latest with module NodeNext.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
!!! error TS2725: Class name cannot be 'Object' when targeting Latest with module NodeNext.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/fourslash/issue61270.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

// @target: es2024
// @module: commonjs

// @filename: test.ts
//// namespace NS { class Object {} }

verify.getSemanticDiagnostics([{
code: 2725,
message: "Class name cannot be 'Object' when targeting ES2024 with module CommonJS.",
range: { pos: 21, end: 27, fileName: "test.ts" }
}]);