fix(babel-parser): delete static property from class static block for TS#13680
fix(babel-parser): delete static property from class static block for TS#13680JLHwung merged 6 commits intobabel:mainfrom
static property from class static block for TS#13680Conversation
|
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:
|
| const callParseClassMemberWithIsStatic = () => { | ||
| const isStatic = !!member.static; | ||
| if (isStatic && this.eat(tt.braceL)) { | ||
| delete member.static; |
There was a problem hiding this comment.
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.
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.
Object clone is also slow. Can we check tt.braceL when modifier === "static" in
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.
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
There was a problem hiding this comment.
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.
Thanks for telling it me. I've fixed by referring to the TypeScript parser code ( d743bc8 )
57824b9 to
d743bc8
Compare
| "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.
This is a regression, the input is
class Foo {
static static {}
}| @@ -1,3 +1,3 @@ | |||
| class Foo { | |||
| static public {} | |||
There was a problem hiding this comment.
Q: Do we already have test cases covering static public?
There was a problem hiding this comment.
There was a problem hiding this comment.
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.
Oops sorry, I misread the changed one🤦♂️
There was a problem hiding this comment.
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.
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.
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)