Skip to content

Commit a7a68d9

Browse files
committed
Merge branch 'fix-es5-export-class-name-object' of https://github.com/Kingwl/TypeScript into Kingwl-fix-es5-export-class-name-object
2 parents 725dbcc + 5df00d2 commit a7a68d9

File tree

39 files changed

+258
-0
lines changed

39 files changed

+258
-0
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24153,6 +24153,16 @@ namespace ts {
2415324153
}
2415424154
}
2415524155

24156+
/**
24157+
* The name cannot be used as 'Object' of user defined types with special target.
24158+
*/
24159+
function checkClassNameCollisionWithObject(name: Identifier): void {
24160+
if (languageVersion === ScriptTarget.ES5 && name.escapedText === "Object"
24161+
&& moduleKind !== ModuleKind.ES2015 && moduleKind !== ModuleKind.ESNext) {
24162+
error(name, Diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_with_module_0, ModuleKind[moduleKind]); // https://github.com/Microsoft/TypeScript/issues/17494
24163+
}
24164+
}
24165+
2415624166
/**
2415724167
* Check each type parameter and check that type parameters have no duplicate type parameter declarations
2415824168
*/
@@ -24279,6 +24289,9 @@ namespace ts {
2427924289
checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
2428024290
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
2428124291
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
24292+
if (!(node.flags & NodeFlags.Ambient)) {
24293+
checkClassNameCollisionWithObject(node.name);
24294+
}
2428224295
}
2428324296
checkTypeParameters(getEffectiveTypeParameterDeclarations(node));
2428424297
checkExportsOnMergedDeclarations(node);

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,11 @@
23572357
"category": "Error",
23582358
"code": 2724
23592359
},
2360+
"Class name cannot be 'Object' when targeting ES5 with module {0}.": {
2361+
"category": "Error",
2362+
"code": 2725
2363+
},
2364+
23602365
"Import declaration '{0}' is using private name '{1}'.": {
23612366
"category": "Error",
23622367
"code": 4000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [es6modulekindExportClassNameWithObject.ts]
2+
export class Object {}
3+
4+
5+
//// [es6modulekindExportClassNameWithObject.js]
6+
var Object = /** @class */ (function () {
7+
function Object() {
8+
}
9+
return Object;
10+
}());
11+
export { Object };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/es6/es6modulekindExportClassNameWithObject.ts ===
2+
export class Object {}
3+
>Object : Symbol(Object, Decl(es6modulekindExportClassNameWithObject.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/es6/es6modulekindExportClassNameWithObject.ts ===
2+
export class Object {}
3+
>Object : Object
4+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [exnextmodulekindExportClassNameWithObject.ts]
2+
export class Object {}
3+
4+
5+
//// [exnextmodulekindExportClassNameWithObject.js]
6+
var Object = /** @class */ (function () {
7+
function Object() {
8+
}
9+
return Object;
10+
}());
11+
export { Object };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/esnext/exnextmodulekindExportClassNameWithObject.ts ===
2+
export class Object {}
3+
>Object : Symbol(Object, Decl(exnextmodulekindExportClassNameWithObject.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/esnext/exnextmodulekindExportClassNameWithObject.ts ===
2+
export class Object {}
3+
>Object : Object
4+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [exportAmbientClassNameWithObject.ts]
2+
export declare class Object {}
3+
4+
5+
//// [exportAmbientClassNameWithObject.js]
6+
"use strict";
7+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportAmbientClassNameWithObject.ts ===
2+
export declare class Object {}
3+
>Object : Symbol(Object, Decl(exportAmbientClassNameWithObject.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportAmbientClassNameWithObject.ts ===
2+
export declare class Object {}
3+
>Object : Object
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/externalModules/exportClassNameWithObjectAMD.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module AMD.
2+
3+
4+
==== tests/cases/conformance/externalModules/exportClassNameWithObjectAMD.ts (1 errors) ====
5+
export class Object {}
6+
~~~~~~
7+
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module AMD.
8+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [exportClassNameWithObjectAMD.ts]
2+
export class Object {}
3+
4+
5+
//// [exportClassNameWithObjectAMD.js]
6+
define(["require", "exports"], function (require, exports) {
7+
"use strict";
8+
Object.defineProperty(exports, "__esModule", { value: true });
9+
var Object = /** @class */ (function () {
10+
function Object() {
11+
}
12+
return Object;
13+
}());
14+
exports.Object = Object;
15+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectAMD.ts ===
2+
export class Object {}
3+
>Object : Symbol(Object, Decl(exportClassNameWithObjectAMD.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectAMD.ts ===
2+
export class Object {}
3+
>Object : Object
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/externalModules/exportClassNameWithObjectCommonJS.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
2+
3+
4+
==== tests/cases/conformance/externalModules/exportClassNameWithObjectCommonJS.ts (1 errors) ====
5+
export class Object {}
6+
~~~~~~
7+
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
8+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [exportClassNameWithObjectCommonJS.ts]
2+
export class Object {}
3+
4+
5+
//// [exportClassNameWithObjectCommonJS.js]
6+
"use strict";
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
var Object = /** @class */ (function () {
9+
function Object() {
10+
}
11+
return Object;
12+
}());
13+
exports.Object = Object;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectCommonJS.ts ===
2+
export class Object {}
3+
>Object : Symbol(Object, Decl(exportClassNameWithObjectCommonJS.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectCommonJS.ts ===
2+
export class Object {}
3+
>Object : Object
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/externalModules/exportClassNameWithObjectSystem.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module System.
2+
3+
4+
==== tests/cases/conformance/externalModules/exportClassNameWithObjectSystem.ts (1 errors) ====
5+
export class Object {}
6+
~~~~~~
7+
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module System.
8+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [exportClassNameWithObjectSystem.ts]
2+
export class Object {}
3+
4+
5+
//// [exportClassNameWithObjectSystem.js]
6+
System.register([], function (exports_1, context_1) {
7+
var Object;
8+
"use strict";
9+
var __moduleName = context_1 && context_1.id;
10+
return {
11+
setters: [],
12+
execute: function () {
13+
Object = /** @class */ (function () {
14+
function Object() {
15+
}
16+
return Object;
17+
}());
18+
exports_1("Object", Object);
19+
}
20+
};
21+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectSystem.ts ===
2+
export class Object {}
3+
>Object : Symbol(Object, Decl(exportClassNameWithObjectSystem.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectSystem.ts ===
2+
export class Object {}
3+
>Object : Object
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/externalModules/exportClassNameWithObjectUMD.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module UMD.
2+
3+
4+
==== tests/cases/conformance/externalModules/exportClassNameWithObjectUMD.ts (1 errors) ====
5+
export class Object {}
6+
~~~~~~
7+
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module UMD.
8+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [exportClassNameWithObjectUMD.ts]
2+
export class Object {}
3+
4+
5+
//// [exportClassNameWithObjectUMD.js]
6+
(function (factory) {
7+
if (typeof module === "object" && typeof module.exports === "object") {
8+
var v = factory(require, exports);
9+
if (v !== undefined) module.exports = v;
10+
}
11+
else if (typeof define === "function" && define.amd) {
12+
define(["require", "exports"], factory);
13+
}
14+
})(function (require, exports) {
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
var Object = /** @class */ (function () {
18+
function Object() {
19+
}
20+
return Object;
21+
}());
22+
exports.Object = Object;
23+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectUMD.ts ===
2+
export class Object {}
3+
>Object : Symbol(Object, Decl(exportClassNameWithObjectUMD.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportClassNameWithObjectUMD.ts ===
2+
export class Object {}
3+
>Object : Object
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/externalModules/exportDefaultClassNameWithObject.ts(1,22): error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
2+
3+
4+
==== tests/cases/conformance/externalModules/exportDefaultClassNameWithObject.ts (1 errors) ====
5+
export default class Object {}
6+
~~~~~~
7+
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
8+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [exportDefaultClassNameWithObject.ts]
2+
export default class Object {}
3+
4+
5+
//// [exportDefaultClassNameWithObject.js]
6+
"use strict";
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
var Object = /** @class */ (function () {
9+
function Object() {
10+
}
11+
return Object;
12+
}());
13+
exports.default = Object;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportDefaultClassNameWithObject.ts ===
2+
export default class Object {}
3+
>Object : Symbol(Object, Decl(exportDefaultClassNameWithObject.ts, 0, 0))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/externalModules/exportDefaultClassNameWithObject.ts ===
2+
export default class Object {}
3+
>Object : Object
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES5
2+
// @module: es2015
3+
export class Object {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES5
2+
// @module: esnext
3+
export class Object {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @target: ES5
2+
export declare class Object {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES5
2+
// @module: amd
3+
export class Object {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES5
2+
// @module: commonjs
3+
export class Object {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES5
2+
// @module: system
3+
export class Object {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES5
2+
// @module: umd
3+
export class Object {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @target: ES5
2+
export default class Object {}

0 commit comments

Comments
 (0)