Skip to content
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

Parse static blocks with typescript plugin #13243

Merged
merged 8 commits into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2317,25 +2317,41 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.tsParseModifier(["public", "protected", "private"]);
}

tsHasSomeModifiers(member: any, modifiers: TsModifier[]): boolean {
return modifiers.some(modifier => {
if (tsIsAccessModifier(modifier)) {
return member.accessibility === modifier;
}
return !!member[modifier];
});
}

parseClassMember(
classBody: N.ClassBody,
member: any,
state: N.ParseClassMemberState,
): void {
this.tsParseModifiers(member, [
const invalidModifersForStaticBlocks = [
"declare",
"private",
"public",
"protected",
"override",
"static",
"abstract",
"readonly",
]);
];
this.tsParseModifiers(
member,
invalidModifersForStaticBlocks.concat(["static"]),
);

const callParseClassMemberWithIsStatic = () => {
const isStatic = !!member.static;
if (isStatic && this.eat(tt.braceL)) {
if (
isStatic &&
!this.tsHasSomeModifiers(member, invalidModifersForStaticBlocks) &&
this.eat(tt.braceL)
) {
this.parseClassStaticBlock(classBody, ((member: any): N.StaticBlock));
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
Expand Down
@@ -0,0 +1,3 @@
class Foo {
static private {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can report a better error, like Static class blocks cannot have any modifier

}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:17)"
}
@@ -0,0 +1,3 @@
class Foo {
static protected {}
}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:19)"
}
@@ -0,0 +1,3 @@
class Foo {
static public {}
}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:16)"
}
@@ -0,0 +1,3 @@
class Foo {
abstract static {}
}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:18)"
}
@@ -0,0 +1,3 @@
class Foo {
override static {}
}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:18)"
}
@@ -0,0 +1,3 @@
class Foo {
declare static {}
}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:17)"
}
@@ -0,0 +1,3 @@
class Foo {
readonly static {}
}
@@ -0,0 +1,12 @@
{
"plugins": [
[
"classStaticBlock"
],
[
"typescript"
]
],
"sourceType": "module",
"throws": "Unexpected token (2:18)"
}