[parser] Disallow private properties access on super #10470
Labels
claimed
good first issue
i: bug
outdated
A closed issue/PR that is archived due to age. Recommended to make a new issue
pkg: parser
Spec: Class Fields
Bug Report
Input Code
repl
Expected behavior/code
It should throw a parser error.
Specification
In the class fields proposal,
MemberExpression
(which is the technical tern for "property access") is defined as follows:As you can see,
.PrivateIdentifier
(where aPrivateIdentifier
is like#x
) is only allowed after anotherMemberExpression
.Let's see what
SuperProperty
is. It is not redefined by the proposal, so we must search it in the main specification.As you might see, there is nothing allowing
super.#x
Babel Configuration (.babelrc, package.json, cli command)
In order to test this bug, you must enable the
classPrivateProperties
parser pluginEnvironment
Possible Solution
When we parse the
super
keyword, we check if it is followed by(
,[
or.
and throw otherwise:babel/packages/babel-parser/src/parser/expression.js
Lines 912 to 920 in 9c1ad0a
A naive solution would be to also check the next token, using
this.lookahead()
. However, in@babel/parser
lookahead()
is really expensive because it needs to clone the whole parser state, produce the next token and then reset the state to what it was before: it should be avoided when possible.What we could do is to disallow it when we are parsing the
MemberExpression
rather than thesuper
keyword:babel/packages/babel-parser/src/parser/expression.js
Lines 653 to 662 in 9c1ad0a
After parsing the
node.property
(which is the "thing" after the dot), we can check ifSuper
node, andPrivateName
node.If both those condition are met, we should raise an error explaining that it is disallowed.
If you don't know how to clone Babel, follow these steps: (you need to have
make
andyarn
available on your machine).git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
yarn && make bootstrap
make watch
(ormake build
whenever you change a file)packages/babel-parser/test/fixtures/experimental/class-private-properties
. You can look in the folders to check how tests about errors are structured.yarn jest parser
to run the testsmake test
to run all the testsgit push
and open a PR!If you need any other help, you can join our Slack and ask in the
#developement
channel. **
I'm going bed now, but I'm sure you'll find someone else online! 🙂
The text was updated successfully, but these errors were encountered: