tc39 / proposal-static-class-features

The static parts of new class features, in a separate proposal
https://arai-a.github.io/ecma262-compare/?pr=1668
127 stars 27 forks source link

Should we disallow static method definitions whose PropName is "prototype" or "constructor"? #60

Closed JLHwung closed 4 years ago

JLHwung commented 4 years ago

The proposal throws early error if PropName of FieldDefinition is "prototype" or "constructor".

Should we also throw early error for accessors? i.e.

class C {
  static get constructor() {}
  static set constructor(_) {}
}

and

class C {
  static get prototype() {}
  static set prototype(_) {}
}

Related

35 mentions static constructor() {} is meant to work with https://github.com/erights/Orthogonal-Classes/commit/93bd1c4a19acd3155b62b6f27e94f9ef98a13a11, but should we throw on the following cases?

class C {
  static prototype() {}
}

Implementation status as of June 2020

Although current spec have not forbid the cases mentioned above, current implementations banned prototype but allowed constructor in method definitions.

(✔️ for pass and ❌for throwing error)

Babel SM V8
static get constructor() {} ✔️ ✔️ ✔️
static set constructor(_) {} ✔️ ✔️ ✔️
static get prototype() {}
static set prototype(_) {}
static prototype() {}

Given the current implementation status, I think we can at least consider add to the spec

ClassElement: static MethodDefinition It is a Syntax Error if PropName of MethodDefinition is "prototype"

As for constructor, IMO if we throw on static constructor;, then we should also disallow static get constructor() {} and static set constructor(_) {}

bakkot commented 4 years ago

That syntax has been legal since ES2015, and I don't think it makes sense to change it as part of this proposal.

jridgewell commented 4 years ago

As for constructor, IMO if we throw on static constructor;

I think this should be done as a needs-consensus PR, not this proposal.

JLHwung commented 4 years ago

I misread the spec: "prototype" is banned as propName of MethodDefinition in Class: https://tc39.es/ecma262/#sec-class-definitions-static-semantics-early-errors