-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
refactor: remove inClassProperty parser state #10906
refactor: remove inClassProperty parser state #10906
Conversation
I enjoyed the proof and think this is a really cool approach to making changes! I'm asking the below because I'm interested in the role of verification in transpilers. It's not a criticism, and please feel free to disregard:
|
@mheiber Thank you for your insightful comments!
The goal of this PR is to remove the usage of class C { prop = (function() { /* inClassProperty is false here */ })() }
class C { prop = class () { constructor() { /* inClassProperty is true here */ } } } IMO
The current
However, if any new language features depends on the ancestry query, we can surely bring back |
Thanks @JLHwung, I can see how inClassProperty was confusing |
This PR refactors the
new.target
detection logic, so we can remove unnecessaryinClassProperty
parser state.We are now going to prove that
!this.scope.inNonArrowFunction && !this.state.inClassProperty
is equivalent of!this.scope.inNonArrowFunction && !this.scope.inClass
.By definition of
state.inClassProperty
,scope.inClass: true
implies thatstate.inClassProperty: true
, therefore!state.inClassProperty
implies!scope.inClass
.Now we asserts that under the condition of
this.scope.inNonArrowFunction: false
,!scope.inClass
implies!state.inClassProperty
.If
scope.inClass: false
andscope.inClassProperty: true
, we know thatthisScope
has been changed fromSCOPE_CLASS
to another scope type. Now by the definition ofthisScope
babel/packages/babel-parser/src/util/scope.js
Lines 211 to 212 in 416ce35
and the definition of
SCOPE_VAR
babel/packages/babel-parser/src/util/scopeflags.js
Line 16 in 416ce35
thisScope
can only be one ofSCOPE_PROGRAM, SCOPE_FUNCTION, SCOPE_TS_MODULE
, sinceSCOPE_PROGRAM
andSCOPE_TS_MODULE
can not be nested inside class properties,thisScope
must beSCOPE_FUNCTION
and it also satisfies!SCOPE_ARROW
, which meansthis.scope.inNonArrowFunction: true
, but it contradicts the condition.Q.E.D.