diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b4f8fdfeab539..7dd4ce6952d3c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2171,24 +2171,25 @@ namespace ts { return type.flags & TypeFlags.StringLiteral ? `"${escapeString((type).text)}"` : (type).text; } - function getSymbolDisplayBuilder(): SymbolDisplayBuilder { - function getNameOfSymbol(symbol: Symbol): string { - if (symbol.declarations && symbol.declarations.length) { - const declaration = symbol.declarations[0]; - if (declaration.name) { - return declarationNameToString(declaration.name); - } - switch (declaration.kind) { - case SyntaxKind.ClassExpression: - return "(Anonymous class)"; - case SyntaxKind.FunctionExpression: - case SyntaxKind.ArrowFunction: - return "(Anonymous function)"; - } + function getNameOfSymbol(symbol: Symbol): string { + if (symbol.declarations && symbol.declarations.length) { + const declaration = symbol.declarations[0]; + if (declaration.name) { + return declarationNameToString(declaration.name); + } + switch (declaration.kind) { + case SyntaxKind.ClassExpression: + return "(Anonymous class)"; + case SyntaxKind.FunctionExpression: + case SyntaxKind.ArrowFunction: + return "(Anonymous function)"; } - return symbol.name; } + return symbol.name; + } + + function getSymbolDisplayBuilder(): SymbolDisplayBuilder { /** * Writes only the name of the symbol out to the writer. Uses the original source text @@ -15680,7 +15681,7 @@ namespace ts { } } else { - const isStatic = forEach(member.modifiers, m => m.kind === SyntaxKind.StaticKeyword); + const isStatic = getModifierFlags(member) & ModifierFlags.Static; const names = isStatic ? staticNames : instanceNames; const memberName = member.name && getPropertyNameForPropertyNameNode(member.name); @@ -15718,6 +15719,38 @@ namespace ts { } } + /** + * 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. + * + * @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3 + * @see http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5 + * @see http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor + * @see http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances + */ + function checkClassForStaticPropertyNameConflicts(node: ClassLikeDeclaration) { + for (const member of node.members) { + const memberNameNode = member.name; + const isStatic = getModifierFlags(member) & ModifierFlags.Static; + if (isStatic && memberNameNode) { + const memberName = getPropertyNameForPropertyNameNode(memberNameNode); + 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); + break; + } + } + } + } + function checkObjectTypeForDuplicateDeclarations(node: TypeLiteralNode | InterfaceDeclaration) { const names = createMap(); for (const member of node.members) { @@ -18291,6 +18324,7 @@ namespace ts { const staticType = getTypeOfSymbol(symbol); checkTypeParameterListsIdentical(node, symbol); checkClassForDuplicateDeclarations(node); + checkClassForStaticPropertyNameConflicts(node); const baseTypeNode = getClassExtendsHeritageClauseElement(node); if (baseTypeNode) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 52fc401d1f151..43bb7415c8839 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2015,6 +2015,10 @@ "category": "Error", "code": 2698 }, + "Static property '{0}' conflicts with built-in property 'Function.{0}' of constructor function '{1}'.": { + "category": "Error", + "code": 2699 + }, "Rest types may only be created from object types.": { "category": "Error", "code": 2700 diff --git a/tests/baselines/reference/propertyNamedPrototype.errors.txt b/tests/baselines/reference/propertyNamedPrototype.errors.txt new file mode 100644 index 0000000000000..d80e1c95c8d5a --- /dev/null +++ b/tests/baselines/reference/propertyNamedPrototype.errors.txt @@ -0,0 +1,10 @@ +tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedPrototype.ts(3,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'C'. + + +==== tests/cases/conformance/classes/propertyMemberDeclarations/propertyNamedPrototype.ts (1 errors) ==== + class C { + prototype: number; // ok + static prototype: C; // error + ~~~~~~~~~ +!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'C'. + } \ No newline at end of file diff --git a/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.errors.txt b/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.errors.txt index 5962654080621..36ead7083319d 100644 --- a/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.errors.txt +++ b/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.errors.txt @@ -1,43 +1,43 @@ tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(12,1): error TS2322: Type 'C' is not assignable to type 'A'. - Property 'name' is missing in type 'C'. + Property 'prop' is missing in type 'C'. tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(13,1): error TS2322: Type 'typeof B' is not assignable to type 'A'. - Property 'name' is missing in type 'typeof B'. + Property 'prop' is missing in type 'typeof B'. tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(16,5): error TS2322: Type 'C' is not assignable to type 'B'. - Property 'name' is missing in type 'C'. + Property 'prop' is missing in type 'C'. tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(17,1): error TS2322: Type 'typeof B' is not assignable to type 'B'. - Property 'name' is missing in type 'typeof B'. + Property 'prop' is missing in type 'typeof B'. ==== tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts (4 errors) ==== interface A { - name(); + prop(); } class B { - public name() { } + public prop() { } } class C { - public static name() { } + public static prop() { } } var a: A = new B(); - a = new C(); // error name is missing + a = new C(); // error prop is missing ~ !!! error TS2322: Type 'C' is not assignable to type 'A'. -!!! error TS2322: Property 'name' is missing in type 'C'. - a = B; // error name is missing +!!! error TS2322: Property 'prop' is missing in type 'C'. + a = B; // error prop is missing ~ !!! error TS2322: Type 'typeof B' is not assignable to type 'A'. -!!! error TS2322: Property 'name' is missing in type 'typeof B'. +!!! error TS2322: Property 'prop' is missing in type 'typeof B'. a = C; - var b: B = new C(); // error name is missing + var b: B = new C(); // error prop is missing ~ !!! error TS2322: Type 'C' is not assignable to type 'B'. -!!! error TS2322: Property 'name' is missing in type 'C'. - b = B; // error name is missing +!!! error TS2322: Property 'prop' is missing in type 'C'. + b = B; // error prop is missing ~ !!! error TS2322: Type 'typeof B' is not assignable to type 'B'. -!!! error TS2322: Property 'name' is missing in type 'typeof B'. +!!! error TS2322: Property 'prop' is missing in type 'typeof B'. b = C; b = a; diff --git a/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.js b/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.js index 1b98daa226dfa..00de82a4f6de7 100644 --- a/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.js +++ b/tests/baselines/reference/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.js @@ -1,21 +1,21 @@ //// [staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts] interface A { - name(); + prop(); } class B { - public name() { } + public prop() { } } class C { - public static name() { } + public static prop() { } } var a: A = new B(); -a = new C(); // error name is missing -a = B; // error name is missing +a = new C(); // error prop is missing +a = B; // error prop is missing a = C; -var b: B = new C(); // error name is missing -b = B; // error name is missing +var b: B = new C(); // error prop is missing +b = B; // error prop is missing b = C; b = a; @@ -29,21 +29,21 @@ c = a; var B = (function () { function B() { } - B.prototype.name = function () { }; + B.prototype.prop = function () { }; return B; }()); var C = (function () { function C() { } - C.name = function () { }; + C.prop = function () { }; return C; }()); var a = new B(); -a = new C(); // error name is missing -a = B; // error name is missing +a = new C(); // error prop is missing +a = B; // error prop is missing a = C; -var b = new C(); // error name is missing -b = B; // error name is missing +var b = new C(); // error prop is missing +b = B; // error prop is missing b = C; b = a; var c = new B(); diff --git a/tests/baselines/reference/staticPropertyNameConflicts.errors.txt b/tests/baselines/reference/staticPropertyNameConflicts.errors.txt new file mode 100644 index 0000000000000..4c051ace0ac6b --- /dev/null +++ b/tests/baselines/reference/staticPropertyNameConflicts.errors.txt @@ -0,0 +1,293 @@ +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(3,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticName'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(8,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticNameFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(14,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLength'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(19,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLengthFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(25,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototype'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(30,12): error TS2300: Duplicate identifier 'prototype'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(30,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(36,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(41,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(47,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(52,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArgumentsFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(62,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(67,12): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(73,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(78,12): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(84,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(89,12): error TS2300: Duplicate identifier 'prototype'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(89,12): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(95,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(100,12): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(106,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(111,12): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function '(Anonymous class)'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(121,16): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticName'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(128,16): error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticNameFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(136,16): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLength'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(143,16): error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLengthFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(151,16): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototype'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(158,16): error TS2300: Duplicate identifier 'prototype'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(158,16): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'StaticPrototypeFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(166,16): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCaller'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(173,16): error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(181,16): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArguments'. +tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts(188,16): error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function 'StaticArgumentsFn'. + + +==== tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts (33 errors) ==== + // 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 + } + + // length + 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 + } + + // prototype + 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 + } + + // caller + 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 + } + + // arguments + 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 + } + + + + // === Static properties on anonymous classes === + + // name + var StaticName_Anonymous = class { + static name: number; // error + ~~~~ +!!! error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function '(Anonymous class)'. + name: string; // ok + } + + var StaticNameFn_Anonymous = class { + static name() {} // error + ~~~~ +!!! error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function '(Anonymous class)'. + name() {} // ok + } + + // length + var StaticLength_Anonymous = class { + static length: number; // error + ~~~~~~ +!!! error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function '(Anonymous class)'. + length: string; // ok + } + + var StaticLengthFn_Anonymous = class { + static length() {} // error + ~~~~~~ +!!! error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function '(Anonymous class)'. + length() {} // ok + } + + // prototype + var StaticPrototype_Anonymous = class { + static prototype: number; // error + ~~~~~~~~~ +!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function '(Anonymous class)'. + prototype: string; // ok + } + + var StaticPrototypeFn_Anonymous = class { + static prototype() {} // error + ~~~~~~~~~ +!!! error TS2300: Duplicate identifier 'prototype'. + ~~~~~~~~~ +!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function '(Anonymous class)'. + prototype() {} // ok + } + + // caller + var StaticCaller_Anonymous = class { + static caller: number; // error + ~~~~~~ +!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function '(Anonymous class)'. + caller: string; // ok + } + + var StaticCallerFn_Anonymous = class { + static caller() {} // error + ~~~~~~ +!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function '(Anonymous class)'. + caller() {} // ok + } + + // arguments + var StaticArguments_Anonymous = class { + static arguments: number; // error + ~~~~~~~~~ +!!! error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function '(Anonymous class)'. + arguments: string; // ok + } + + var StaticArgumentsFn_Anonymous = class { + static arguments() {} // error + ~~~~~~~~~ +!!! error TS2699: Static property 'arguments' conflicts with built-in property 'Function.arguments' of constructor function '(Anonymous class)'. + arguments() {} // ok + } + + + // === Static properties on default exported classes === + + // name + module TestOnDefaultExportedClass_1 { + 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 + } + } + + module TestOnDefaultExportedClass_2 { + class StaticNameFn { + static name() {} // error + ~~~~ +!!! error TS2699: Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'StaticNameFn'. + name() {} // ok + } + } + + // length + module TestOnDefaultExportedClass_3 { + export default 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 + } + } + + module TestOnDefaultExportedClass_4 { + export default class StaticLengthFn { + static length() {} // error + ~~~~~~ +!!! error TS2699: Static property 'length' conflicts with built-in property 'Function.length' of constructor function 'StaticLengthFn'. + length() {} // ok + } + } + + // prototype + module TestOnDefaultExportedClass_5 { + export default 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 + } + } + + module TestOnDefaultExportedClass_6 { + export default 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 + } + } + + // caller + module TestOnDefaultExportedClass_7 { + export default 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 + } + } + + module TestOnDefaultExportedClass_8 { + export default class StaticCallerFn { + static caller() {} // error + ~~~~~~ +!!! error TS2699: Static property 'caller' conflicts with built-in property 'Function.caller' of constructor function 'StaticCallerFn'. + caller() {} // ok + } + } + + // arguments + module TestOnDefaultExportedClass_9 { + export default 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 + } + } + + module TestOnDefaultExportedClass_10 { + export default 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/staticPropertyNameConflicts.js b/tests/baselines/reference/staticPropertyNameConflicts.js new file mode 100644 index 0000000000000..b93b16550f0ca --- /dev/null +++ b/tests/baselines/reference/staticPropertyNameConflicts.js @@ -0,0 +1,429 @@ +//// [staticPropertyNameConflicts.ts] +// name +class StaticName { + static name: number; // error + name: string; // ok +} + +class StaticNameFn { + static name() {} // error + name() {} // ok +} + +// length +class StaticLength { + static length: number; // error + length: string; // ok +} + +class StaticLengthFn { + static length() {} // error + length() {} // ok +} + +// prototype +class StaticPrototype { + static prototype: number; // error + prototype: string; // ok +} + +class StaticPrototypeFn { + static prototype() {} // error + prototype() {} // ok +} + +// caller +class StaticCaller { + static caller: number; // error + caller: string; // ok +} + +class StaticCallerFn { + static caller() {} // error + caller() {} // ok +} + +// arguments +class StaticArguments { + static arguments: number; // error + arguments: string; // ok +} + +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 + } +} + +//// [staticPropertyNameConflicts.js] +// name +var StaticName = (function () { + function StaticName() { + } + return StaticName; +}()); +var StaticNameFn = (function () { + function StaticNameFn() { + } + StaticNameFn.name = function () { }; // error + StaticNameFn.prototype.name = function () { }; // ok + return StaticNameFn; +}()); +// length +var StaticLength = (function () { + function StaticLength() { + } + return StaticLength; +}()); +var StaticLengthFn = (function () { + function StaticLengthFn() { + } + StaticLengthFn.length = function () { }; // error + StaticLengthFn.prototype.length = function () { }; // ok + return StaticLengthFn; +}()); +// prototype +var StaticPrototype = (function () { + function StaticPrototype() { + } + return StaticPrototype; +}()); +var StaticPrototypeFn = (function () { + function StaticPrototypeFn() { + } + StaticPrototypeFn.prototype = function () { }; // error + StaticPrototypeFn.prototype.prototype = function () { }; // ok + return StaticPrototypeFn; +}()); +// caller +var StaticCaller = (function () { + function StaticCaller() { + } + return StaticCaller; +}()); +var StaticCallerFn = (function () { + function StaticCallerFn() { + } + StaticCallerFn.caller = function () { }; // error + StaticCallerFn.prototype.caller = function () { }; // ok + return StaticCallerFn; +}()); +// arguments +var StaticArguments = (function () { + function StaticArguments() { + } + return StaticArguments; +}()); +var StaticArgumentsFn = (function () { + function StaticArgumentsFn() { + } + StaticArgumentsFn.arguments = function () { }; // error + StaticArgumentsFn.prototype.arguments = function () { }; // ok + return StaticArgumentsFn; +}()); +// === Static properties on anonymous classes === +// name +var StaticName_Anonymous = (function () { + function class_1() { + } + return class_1; +}()); +var StaticNameFn_Anonymous = (function () { + function class_2() { + } + class_2.name = function () { }; // error + class_2.prototype.name = function () { }; // ok + return class_2; +}()); +// length +var StaticLength_Anonymous = (function () { + function class_3() { + } + return class_3; +}()); +var StaticLengthFn_Anonymous = (function () { + function class_4() { + } + class_4.length = function () { }; // error + class_4.prototype.length = function () { }; // ok + return class_4; +}()); +// prototype +var StaticPrototype_Anonymous = (function () { + function class_5() { + } + return class_5; +}()); +var StaticPrototypeFn_Anonymous = (function () { + function class_6() { + } + class_6.prototype = function () { }; // error + class_6.prototype.prototype = function () { }; // ok + return class_6; +}()); +// caller +var StaticCaller_Anonymous = (function () { + function class_7() { + } + return class_7; +}()); +var StaticCallerFn_Anonymous = (function () { + function class_8() { + } + class_8.caller = function () { }; // error + class_8.prototype.caller = function () { }; // ok + return class_8; +}()); +// arguments +var StaticArguments_Anonymous = (function () { + function class_9() { + } + return class_9; +}()); +var StaticArgumentsFn_Anonymous = (function () { + function class_10() { + } + class_10.arguments = function () { }; // error + class_10.prototype.arguments = function () { }; // ok + return class_10; +}()); +// === Static properties on default exported classes === +// name +var TestOnDefaultExportedClass_1; +(function (TestOnDefaultExportedClass_1) { + var StaticName = (function () { + function StaticName() { + } + return StaticName; + }()); +})(TestOnDefaultExportedClass_1 || (TestOnDefaultExportedClass_1 = {})); +var TestOnDefaultExportedClass_2; +(function (TestOnDefaultExportedClass_2) { + var StaticNameFn = (function () { + function StaticNameFn() { + } + StaticNameFn.name = function () { }; // error + StaticNameFn.prototype.name = function () { }; // ok + return StaticNameFn; + }()); +})(TestOnDefaultExportedClass_2 || (TestOnDefaultExportedClass_2 = {})); +// length +var TestOnDefaultExportedClass_3; +(function (TestOnDefaultExportedClass_3) { + var StaticLength = (function () { + function StaticLength() { + } + return StaticLength; + }()); + TestOnDefaultExportedClass_3.StaticLength = StaticLength; +})(TestOnDefaultExportedClass_3 || (TestOnDefaultExportedClass_3 = {})); +var TestOnDefaultExportedClass_4; +(function (TestOnDefaultExportedClass_4) { + var StaticLengthFn = (function () { + function StaticLengthFn() { + } + StaticLengthFn.length = function () { }; // error + StaticLengthFn.prototype.length = function () { }; // ok + return StaticLengthFn; + }()); + TestOnDefaultExportedClass_4.StaticLengthFn = StaticLengthFn; +})(TestOnDefaultExportedClass_4 || (TestOnDefaultExportedClass_4 = {})); +// prototype +var TestOnDefaultExportedClass_5; +(function (TestOnDefaultExportedClass_5) { + var StaticPrototype = (function () { + function StaticPrototype() { + } + return StaticPrototype; + }()); + TestOnDefaultExportedClass_5.StaticPrototype = StaticPrototype; +})(TestOnDefaultExportedClass_5 || (TestOnDefaultExportedClass_5 = {})); +var TestOnDefaultExportedClass_6; +(function (TestOnDefaultExportedClass_6) { + var StaticPrototypeFn = (function () { + function StaticPrototypeFn() { + } + StaticPrototypeFn.prototype = function () { }; // error + StaticPrototypeFn.prototype.prototype = function () { }; // ok + return StaticPrototypeFn; + }()); + TestOnDefaultExportedClass_6.StaticPrototypeFn = StaticPrototypeFn; +})(TestOnDefaultExportedClass_6 || (TestOnDefaultExportedClass_6 = {})); +// caller +var TestOnDefaultExportedClass_7; +(function (TestOnDefaultExportedClass_7) { + var StaticCaller = (function () { + function StaticCaller() { + } + return StaticCaller; + }()); + TestOnDefaultExportedClass_7.StaticCaller = StaticCaller; +})(TestOnDefaultExportedClass_7 || (TestOnDefaultExportedClass_7 = {})); +var TestOnDefaultExportedClass_8; +(function (TestOnDefaultExportedClass_8) { + var StaticCallerFn = (function () { + function StaticCallerFn() { + } + StaticCallerFn.caller = function () { }; // error + StaticCallerFn.prototype.caller = function () { }; // ok + return StaticCallerFn; + }()); + TestOnDefaultExportedClass_8.StaticCallerFn = StaticCallerFn; +})(TestOnDefaultExportedClass_8 || (TestOnDefaultExportedClass_8 = {})); +// arguments +var TestOnDefaultExportedClass_9; +(function (TestOnDefaultExportedClass_9) { + var StaticArguments = (function () { + function StaticArguments() { + } + return StaticArguments; + }()); + TestOnDefaultExportedClass_9.StaticArguments = StaticArguments; +})(TestOnDefaultExportedClass_9 || (TestOnDefaultExportedClass_9 = {})); +var TestOnDefaultExportedClass_10; +(function (TestOnDefaultExportedClass_10) { + var StaticArgumentsFn = (function () { + function StaticArgumentsFn() { + } + StaticArgumentsFn.arguments = function () { }; // error + StaticArgumentsFn.prototype.arguments = function () { }; // ok + return StaticArgumentsFn; + }()); + TestOnDefaultExportedClass_10.StaticArgumentsFn = StaticArgumentsFn; +})(TestOnDefaultExportedClass_10 || (TestOnDefaultExportedClass_10 = {})); diff --git a/tests/baselines/reference/staticPrototypeProperty.errors.txt b/tests/baselines/reference/staticPrototypeProperty.errors.txt index c3f5cb99c96b1..c131b7a774d5d 100644 --- a/tests/baselines/reference/staticPrototypeProperty.errors.txt +++ b/tests/baselines/reference/staticPrototypeProperty.errors.txt @@ -1,13 +1,19 @@ tests/cases/compiler/staticPrototypeProperty.ts(2,11): error TS2300: Duplicate identifier 'prototype'. +tests/cases/compiler/staticPrototypeProperty.ts(2,11): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'C'. +tests/cases/compiler/staticPrototypeProperty.ts(6,11): error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'C2'. -==== tests/cases/compiler/staticPrototypeProperty.ts (1 errors) ==== +==== tests/cases/compiler/staticPrototypeProperty.ts (3 errors) ==== class C { static prototype() { } ~~~~~~~~~ !!! error TS2300: Duplicate identifier 'prototype'. + ~~~~~~~~~ +!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'C'. } class C2 { static prototype; + ~~~~~~~~~ +!!! error TS2699: Static property 'prototype' conflicts with built-in property 'Function.prototype' of constructor function 'C2'. } \ No newline at end of file diff --git a/tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts b/tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts index c8ebc3121043a..aa78d4333c641 100644 --- a/tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts +++ b/tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts @@ -1,20 +1,20 @@ interface A { - name(); + prop(); } class B { - public name() { } + public prop() { } } class C { - public static name() { } + public static prop() { } } var a: A = new B(); -a = new C(); // error name is missing -a = B; // error name is missing +a = new C(); // error prop is missing +a = B; // error prop is missing a = C; -var b: B = new C(); // error name is missing -b = B; // error name is missing +var b: B = new C(); // error prop is missing +b = B; // error prop is missing b = C; b = a; diff --git a/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts b/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts new file mode 100644 index 0000000000000..86755cb016d0b --- /dev/null +++ b/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts @@ -0,0 +1,192 @@ +// @target: es5 +// name +class StaticName { + static name: number; // error + name: string; // ok +} + +class StaticNameFn { + static name() {} // error + name() {} // ok +} + +// length +class StaticLength { + static length: number; // error + length: string; // ok +} + +class StaticLengthFn { + static length() {} // error + length() {} // ok +} + +// prototype +class StaticPrototype { + static prototype: number; // error + prototype: string; // ok +} + +class StaticPrototypeFn { + static prototype() {} // error + prototype() {} // ok +} + +// caller +class StaticCaller { + static caller: number; // error + caller: string; // ok +} + +class StaticCallerFn { + static caller() {} // error + caller() {} // ok +} + +// arguments +class StaticArguments { + static arguments: number; // error + arguments: string; // ok +} + +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