diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 884860d6b14e0..da6235b6d1ccd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15694,8 +15694,8 @@ namespace ts { } /** - * Static members being set on a constructor function may conflict with built-in Function - * object properties. Esp. in ECMAScript 5 there are non-configurable and non-writable + * Static members being set on a constructor function may conflict with built-in properties + * of Function. Esp. in ECMAScript 5 there are non-configurable and non-writable * built-in properties. This check issues a transpile error when a class has a static * member with the same name as a non-writable built-in property. * @@ -15705,37 +15705,21 @@ namespace ts { * @see http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances */ function checkClassForStaticPropertyNameConflicts(node: ClassLikeDeclaration) { - const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1; - const className = getNameOfSymbol(getSymbolOfNode(node)); for (const member of node.members) { - const isStatic = getModifierFlags(member) & ModifierFlags.Static; - const isMethod = member.kind === SyntaxKind.MethodDeclaration; const memberNameNode = member.name; + const isStatic = getModifierFlags(member) & ModifierFlags.Static; if (isStatic && memberNameNode) { const memberName = getPropertyNameForPropertyNameNode(memberNameNode); - if (languageVersion <= ScriptTarget.ES5) { // ES3, ES5 - if (memberName === "prototype" || - memberName === "name" || - memberName === "length" || - memberName === "caller" || - memberName === "arguments" - ) { - error(memberNameNode, message, memberName, className); - } - } - else { // ES6+ - if (memberName === "prototype") { + switch (memberName) { + case "name": + case "length": + case "caller": + case "arguments": + case "prototype": + const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1; + const className = getNameOfSymbol(getSymbolOfNode(node)); error(memberNameNode, message, memberName, className); - } - else if (( - memberName === "name" || - memberName === "length" || - memberName === "caller" || - memberName === "arguments") && - isMethod === false - ) { - error(memberNameNode, message, memberName, className); - } + break; } } } diff --git a/tests/baselines/reference/staticPropertyNameConflictsEs5.errors.txt b/tests/baselines/reference/staticPropertyNameConflictsEs5.errors.txt deleted file mode 100644 index 004365d3e760c..0000000000000 --- a/tests/baselines/reference/staticPropertyNameConflictsEs5.errors.txt +++ /dev/null @@ -1,92 +0,0 @@ -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(4,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticName'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(9,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticNameFn'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(15,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLength'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(20,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLengthFn'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(26,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototype'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(31,12): error TS2300: Duplicate identifier 'prototype'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(31,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(37,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(42,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(48,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts(53,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArgumentsFn'. - - -==== tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs5.ts (11 errors) ==== - - // static name - class StaticName { - static name: number; // error - ~~~~ -!!! error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticName'. - name: string; // ok - } - - class StaticNameFn { - static name() {} // error - ~~~~ -!!! error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticNameFn'. - name() {} // ok - } - - - class StaticLength { - static length: number; // error - ~~~~~~ -!!! error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLength'. - length: string; // ok - } - - class StaticLengthFn { - static length() {} // error - ~~~~~~ -!!! error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLengthFn'. - length() {} // ok - } - - - class StaticPrototype { - static prototype: number; // error - ~~~~~~~~~ -!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototype'. - prototype: string; // ok - } - - class StaticPrototypeFn { - static prototype() {} // error - ~~~~~~~~~ -!!! error TS2300: Duplicate identifier 'prototype'. - ~~~~~~~~~ -!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'. - prototype() {} // ok - } - - - class StaticCaller { - static caller: number; // error - ~~~~~~ -!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'. - caller: string; // ok - } - - class StaticCallerFn { - static caller() {} // error - ~~~~~~ -!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'. - caller() {} // ok - } - - - class StaticArguments { - static arguments: number; // error - ~~~~~~~~~ -!!! error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'. - arguments: string; // ok - } - - class StaticArgumentsFn { - static arguments() {} // error - ~~~~~~~~~ -!!! error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArgumentsFn'. - arguments() {} // ok - } - \ No newline at end of file diff --git a/tests/baselines/reference/staticPropertyNameConflictsEs5.js b/tests/baselines/reference/staticPropertyNameConflictsEs5.js deleted file mode 100644 index 06d4a011a8e78..0000000000000 --- a/tests/baselines/reference/staticPropertyNameConflictsEs5.js +++ /dev/null @@ -1,120 +0,0 @@ -//// [staticPropertyNameConflictsEs5.ts] - -// static name -class StaticName { - static name: number; // error - name: string; // ok -} - -class StaticNameFn { - static name() {} // error - name() {} // ok -} - - -class StaticLength { - static length: number; // error - length: string; // ok -} - -class StaticLengthFn { - static length() {} // error - length() {} // ok -} - - -class StaticPrototype { - static prototype: number; // error - prototype: string; // ok -} - -class StaticPrototypeFn { - static prototype() {} // error - prototype() {} // ok -} - - -class StaticCaller { - static caller: number; // error - caller: string; // ok -} - -class StaticCallerFn { - static caller() {} // error - caller() {} // ok -} - - -class StaticArguments { - static arguments: number; // error - arguments: string; // ok -} - -class StaticArgumentsFn { - static arguments() {} // error - arguments() {} // ok -} - - -//// [staticPropertyNameConflictsEs5.js] -// static name -var StaticName = (function () { - function StaticName() { - } - return StaticName; -}()); -var StaticNameFn = (function () { - function StaticNameFn() { - } - StaticNameFn.name = function () { }; // error - StaticNameFn.prototype.name = function () { }; // ok - return StaticNameFn; -}()); -var StaticLength = (function () { - function StaticLength() { - } - return StaticLength; -}()); -var StaticLengthFn = (function () { - function StaticLengthFn() { - } - StaticLengthFn.length = function () { }; // error - StaticLengthFn.prototype.length = function () { }; // ok - return StaticLengthFn; -}()); -var StaticPrototype = (function () { - function StaticPrototype() { - } - return StaticPrototype; -}()); -var StaticPrototypeFn = (function () { - function StaticPrototypeFn() { - } - StaticPrototypeFn.prototype = function () { }; // error - StaticPrototypeFn.prototype.prototype = function () { }; // ok - return StaticPrototypeFn; -}()); -var StaticCaller = (function () { - function StaticCaller() { - } - return StaticCaller; -}()); -var StaticCallerFn = (function () { - function StaticCallerFn() { - } - StaticCallerFn.caller = function () { }; // error - StaticCallerFn.prototype.caller = function () { }; // ok - return StaticCallerFn; -}()); -var StaticArguments = (function () { - function StaticArguments() { - } - return StaticArguments; -}()); -var StaticArgumentsFn = (function () { - function StaticArgumentsFn() { - } - StaticArgumentsFn.arguments = function () { }; // error - StaticArgumentsFn.prototype.arguments = function () { }; // ok - return StaticArgumentsFn; -}()); diff --git a/tests/baselines/reference/staticPropertyNameConflictsEs6.errors.txt b/tests/baselines/reference/staticPropertyNameConflictsEs6.errors.txt deleted file mode 100644 index 607735bb12fbd..0000000000000 --- a/tests/baselines/reference/staticPropertyNameConflictsEs6.errors.txt +++ /dev/null @@ -1,80 +0,0 @@ -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(4,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticName'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(15,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLength'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(26,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototype'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(31,12): error TS2300: Duplicate identifier 'prototype'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(31,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(37,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'. -tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts(48,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'. - - -==== tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflictsEs6.ts (7 errors) ==== - - - class StaticName { - static name: number; // error - ~~~~ -!!! error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticName'. - name: string; // ok - } - - class StaticNameFn { - static name() {} // ok - name() {} // ok - } - - - class StaticLength { - static length: number; // error - ~~~~~~ -!!! error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLength'. - length: string; // ok - } - - class StaticLengthFn { - static length() {} // ok - length() {} // ok - } - - - class StaticPrototype { - static prototype: number; // error - ~~~~~~~~~ -!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototype'. - prototype: string; // ok - } - - class StaticPrototypeFn { - static prototype() {} // error - ~~~~~~~~~ -!!! error TS2300: Duplicate identifier 'prototype'. - ~~~~~~~~~ -!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'. - prototype() {} // ok - } - - - class StaticCaller { - static caller: number; // error - ~~~~~~ -!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'. - caller: string; // ok - } - - class StaticCallerFn { - static caller() {} // ok - caller() {} // ok - } - - - class StaticArguments { - static arguments: number; // error - ~~~~~~~~~ -!!! error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'. - arguments: string; // ok - } - - class StaticArgumentsFn { - static arguments() {} // ok - arguments() {} // ok - } - \ No newline at end of file diff --git a/tests/baselines/reference/staticPropertyNameConflictsEs6.js b/tests/baselines/reference/staticPropertyNameConflictsEs6.js deleted file mode 100644 index 7adef10b141a3..0000000000000 --- a/tests/baselines/reference/staticPropertyNameConflictsEs6.js +++ /dev/null @@ -1,89 +0,0 @@ -//// [staticPropertyNameConflictsEs6.ts] - - -class StaticName { - static name: number; // error - name: string; // ok -} - -class StaticNameFn { - static name() {} // ok - name() {} // ok -} - - -class StaticLength { - static length: number; // error - length: string; // ok -} - -class StaticLengthFn { - static length() {} // ok - length() {} // ok -} - - -class StaticPrototype { - static prototype: number; // error - prototype: string; // ok -} - -class StaticPrototypeFn { - static prototype() {} // error - prototype() {} // ok -} - - -class StaticCaller { - static caller: number; // error - caller: string; // ok -} - -class StaticCallerFn { - static caller() {} // ok - caller() {} // ok -} - - -class StaticArguments { - static arguments: number; // error - arguments: string; // ok -} - -class StaticArgumentsFn { - static arguments() {} // ok - arguments() {} // ok -} - - -//// [staticPropertyNameConflictsEs6.js] -class StaticName { -} -class StaticNameFn { - static name() { } // ok - name() { } // ok -} -class StaticLength { -} -class StaticLengthFn { - static length() { } // ok - length() { } // ok -} -class StaticPrototype { -} -class StaticPrototypeFn { - static prototype() { } // error - prototype() { } // ok -} -class StaticCaller { -} -class StaticCallerFn { - static caller() { } // ok - caller() { } // ok -} -class StaticArguments { -} -class StaticArgumentsFn { - static arguments() { } // ok - arguments() { } // ok -} diff --git a/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts b/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts index 5eb09ddb2d375..86755cb016d0b 100644 --- a/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts +++ b/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts @@ -1,6 +1,5 @@ -// @target: es5 - -// static name +// @target: es5 +// name class StaticName { static name: number; // error name: string; // ok @@ -11,7 +10,7 @@ class StaticNameFn { name() {} // ok } - +// length class StaticLength { static length: number; // error length: string; // ok @@ -22,7 +21,7 @@ class StaticLengthFn { length() {} // ok } - +// prototype class StaticPrototype { static prototype: number; // error prototype: string; // ok @@ -33,7 +32,7 @@ class StaticPrototypeFn { prototype() {} // ok } - +// caller class StaticCaller { static caller: number; // error caller: string; // ok @@ -44,7 +43,7 @@ class StaticCallerFn { caller() {} // ok } - +// arguments class StaticArguments { static arguments: number; // error arguments: string; // ok @@ -54,3 +53,140 @@ class StaticArgumentsFn { static arguments() {} // error arguments() {} // ok } + + + +// === Static properties on anonymous classes === + +// name +var StaticName_Anonymous = class { + static name: number; // error + name: string; // ok +} + +var StaticNameFn_Anonymous = class { + static name() {} // error + name() {} // ok +} + +// length +var StaticLength_Anonymous = class { + static length: number; // error + length: string; // ok +} + +var StaticLengthFn_Anonymous = class { + static length() {} // error + length() {} // ok +} + +// prototype +var StaticPrototype_Anonymous = class { + static prototype: number; // error + prototype: string; // ok +} + +var StaticPrototypeFn_Anonymous = class { + static prototype() {} // error + prototype() {} // ok +} + +// caller +var StaticCaller_Anonymous = class { + static caller: number; // error + caller: string; // ok +} + +var StaticCallerFn_Anonymous = class { + static caller() {} // error + caller() {} // ok +} + +// arguments +var StaticArguments_Anonymous = class { + static arguments: number; // error + arguments: string; // ok +} + +var StaticArgumentsFn_Anonymous = class { + static arguments() {} // error + arguments() {} // ok +} + + +// === Static properties on default exported classes === + +// name +module TestOnDefaultExportedClass_1 { + class StaticName { + static name: number; // error + name: string; // ok + } +} + +module TestOnDefaultExportedClass_2 { + class StaticNameFn { + static name() {} // error + name() {} // ok + } +} + +// length +module TestOnDefaultExportedClass_3 { + export default class StaticLength { + static length: number; // error + length: string; // ok + } +} + +module TestOnDefaultExportedClass_4 { + export default class StaticLengthFn { + static length() {} // error + length() {} // ok + } +} + +// prototype +module TestOnDefaultExportedClass_5 { + export default class StaticPrototype { + static prototype: number; // error + prototype: string; // ok + } +} + +module TestOnDefaultExportedClass_6 { + export default class StaticPrototypeFn { + static prototype() {} // error + prototype() {} // ok + } +} + +// caller +module TestOnDefaultExportedClass_7 { + export default class StaticCaller { + static caller: number; // error + caller: string; // ok + } +} + +module TestOnDefaultExportedClass_8 { + export default class StaticCallerFn { + static caller() {} // error + caller() {} // ok + } +} + +// arguments +module TestOnDefaultExportedClass_9 { + export default class StaticArguments { + static arguments: number; // error + arguments: string; // ok + } +} + +module TestOnDefaultExportedClass_10 { + export default class StaticArgumentsFn { + static arguments() {} // error + arguments() {} // ok + } +} \ No newline at end of file