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
Fixing #442: Impossible to define static 'length' function on class #12065
Changes from 10 commits
fc5b2e5
8a37a16
9122f62
45e8a5b
8fa07d6
189dbdd
d58b5c8
d9a46e1
b623f37
02ee7c7
ff828fd
544d0a4
244db70
d78247a
66263b3
4718efd
9b217e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14771,6 +14771,49 @@ namespace ts { | |
} | ||
} | ||
|
||
// Static members may conflict with non-configurable non-writable built-in Function object properties | ||
// see https://github.com/microsoft/typescript/issues/442. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of a github issue number, can you reference the spec the way you do below instead? |
||
function checkClassForStaticPropertyNameConflicts(node: ClassLikeDeclaration) { | ||
const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1; | ||
const className = getSymbolOfNode(node).name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just |
||
for (const member of node.members) { | ||
const isStatic = forEach(member.modifiers, (m: Modifier) => m.kind === SyntaxKind.StaticKeyword); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yikes, looks like |
||
const isMethod = member.kind === SyntaxKind.MethodDeclaration; | ||
const memberNameNode = member.name; | ||
if (isStatic && memberNameNode) { | ||
const memberName = getPropertyNameForPropertyNameNode(memberNameNode); | ||
if (languageVersion <= ScriptTarget.ES5) { // ES3, ES5 | ||
// see also http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3 | ||
// see also http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5 | ||
if (memberName === "prototype" || | ||
memberName === "name" || | ||
memberName === "length" || | ||
memberName === "caller" || | ||
memberName === "arguments" | ||
) { | ||
error(memberNameNode, message, memberName, className); | ||
} | ||
} | ||
else { // ES6+ | ||
// see also http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-constructor | ||
// see also http://www.ecma-international.org/ecma-262/6.0/#sec-function-instances | ||
if (memberName === "prototype") { | ||
error(memberNameNode, message, memberName, className); | ||
} | ||
else if (( | ||
memberName === "name" || | ||
memberName === "length" || | ||
memberName === "caller" || | ||
memberName === "arguments") && | ||
isMethod === false | ||
) { | ||
error(memberNameNode, message, memberName, className); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function checkObjectTypeForDuplicateDeclarations(node: TypeLiteralNode | InterfaceDeclaration) { | ||
const names = createMap<boolean>(); | ||
for (const member of node.members) { | ||
|
@@ -17259,6 +17302,7 @@ namespace ts { | |
const staticType = <ObjectType>getTypeOfSymbol(symbol); | ||
checkTypeParameterListsIdentical(node, symbol); | ||
checkClassForDuplicateDeclarations(node); | ||
checkClassForStaticPropertyNameConflicts(node); | ||
|
||
const baseTypeNode = getClassExtendsHeritageClauseElement(node); | ||
if (baseTypeNode) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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 | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use jsdoc format.