quick-lint-js
quick-lint-js copied to clipboard
8$: Error on special static class field names
https://www.typescriptlang.org/docs/handbook/2/classes.html#special-static-names
The following member names are not valid:
- name
- length
- caller
- arguments
- prototype
function checkClassForStaticPropertyNameConflicts(node: ClassLikeDeclaration) {
for (const member of node.members) {
const memberNameNode = member.name;
const isStaticMember = isStatic(member);
if (isStaticMember && 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 = getNameOfSymbolAsWritten(getSymbolOfNode(node));
error(memberNameNode, message, memberName, className);
break;
}
}
}
}
Source: https://github.com/microsoft/TypeScript/blob/main/src/compiler/checker.ts
I think it may be best to check the following members as well:
- apply
- bind
- call
- toString
For example if the code below compiled to ES5, super() will call the Base.call instead of constructor.
class Base {
static call() {
console.log("Base call()");
}
constructor() {
console.log("Base constructor()");
}
}
class Derived extends Base {
constructor() {
super();
console.log("Derived constructor()");
}
}
new Derived();