-
-
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
fix(babel-parser): delete static
property from class static block for TS
#13680
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48314/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 828b241:
|
@@ -2396,6 +2396,7 @@ export default (superClass: Class<Parser>): Class<Parser> => | |||
const callParseClassMemberWithIsStatic = () => { | |||
const isStatic = !!member.static; | |||
if (isStatic && this.eat(tt.braceL)) { | |||
delete member.static; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting a property should be generally avoided (https://stackoverflow.com/a/44008788/1490357), especially when it is executed for normal execution path: static blocks. I think we can check if a tt.braceL
is upcoming before member.static
is assigned in tsParseModifiers
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was difficult to avoid to attach static
in tsParseModifiers
without failing existing tests. So what do you think about to separate attaching modifier properties and parsing ( d5077ff )?
} else if (modifier !== "static") { | ||
member[modifier] = true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object clone is also slow. Can we check tt.braceL
when modifier === "static"
in
modified[modifier] = true; |
For cases like static declare { }
, it doesn't matter if static
is set because static block can not have any modifiers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can check it in parseModifiers
. But if we do that, we cannot know from the parseClassMember
whether a member is static or not in
babel/packages/babel-parser/src/plugins/typescript/index.js
Lines 2397 to 2398 in fc66d4d
const isStatic = !!member.static; | |
if (isStatic && this.eat(tt.braceL)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case we can have parseModifiers
stopped at static {
so we can still recover from cases when modifiers are on static block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for telling it me. I've fixed by referring to the TypeScript parser code ( d743bc8 )
57824b9
to
d743bc8
Compare
@@ -1,9 +1,6 @@ | |||
{ | |||
"type": "File", | |||
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | |||
"errors": [ | |||
"SyntaxError: Duplicate modifier: 'static'. (2:9)" | |||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a regression, the input is
class Foo {
static static {}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JLHwung Can you review again?
@@ -1,3 +1,3 @@ | |||
class Foo { | |||
static public {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Do we already have test cases covering static public
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we do.
babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/input.ts
Line 21 in 1d4bd31
static public m13() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That one is a static public
class method. I mean static public {}
, which should throw. Just wondering why the test case is changed, we can always add new test cases for public static {}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops sorry, I misread the changed one🤦♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've added more tests (5ea75b9, 828b241). However, the syntax static
followed by modifier followed by block({ }
) is not recoverable. (TypeScript Compiler also doesn't seem to be able to throw a friendly error for such code (TypeScript Playground link))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can always improve it later as long as we don't pass invalid input.
@@ -1,3 +1,3 @@ | |||
class Foo { | |||
static public {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we do.
babel/packages/babel-parser/test/fixtures/typescript/class/modifiers-invalid-order/input.ts
Line 21 in 1d4bd31
static public m13() {} |
94215e2
to
828b241
Compare
context: I noticed the bug when implementing to support static blocks for typescript-estree (ref :https://github.com/typescript-eslint/typescript-eslint/pull/3730/files#diff-f3405000b53c5abe00cbd3f4852b524d0e84478a76b8f8da33eecfe9acfc97f4R253-R256)