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 12 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 |
---|---|---|
|
@@ -2142,24 +2142,25 @@ namespace ts { | |
return type.flags & TypeFlags.StringLiteral ? `"${escapeString((<LiteralType>type).text)}"` : (<LiteralType>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 | ||
|
@@ -15647,6 +15648,54 @@ 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 | ||
* 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) { | ||
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; | ||
if (isStatic && memberNameNode) { | ||
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. Reduce nesting by negating this: if (!isStatic || !memberNameNode) {
continue;
} |
||
const memberName = getPropertyNameForPropertyNameNode(memberNameNode); | ||
if (languageVersion <= ScriptTarget.ES5) { // ES3, ES5 | ||
if (memberName === "prototype" || | ||
memberName === "name" || | ||
memberName === "length" || | ||
memberName === "caller" || | ||
memberName === "arguments" | ||
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. Consider a |
||
) { | ||
error(memberNameNode, message, memberName, className); | ||
} | ||
} | ||
else { // ES6+ | ||
if (memberName === "prototype") { | ||
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. switch (memberName) {
case "name":
case "length":
case "caller":
case "arguments":
if (isMethod) {
continue;
}
// fall through
case "prototype":
error(/*...*/); |
||
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) { | ||
|
@@ -18210,6 +18259,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.
Can you add a test for default-exported classes and anonymous class expressions?
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.
Also, I would also only get the information you need when you actually report the error. Consider extracting that into a helper function.