quick-lint-js icon indicating copy to clipboard operation
quick-lint-js copied to clipboard

8$: Error on special static class field names

Open strager opened this issue 3 years ago • 2 comments

https://www.typescriptlang.org/docs/handbook/2/classes.html#special-static-names

strager avatar Jun 04 '22 00:06 strager

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

clegoz avatar Jul 13 '22 21:07 clegoz

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();

clegoz avatar Jul 13 '22 22:07 clegoz