Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_js_parser): EcmaScript @decorators #4252
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Apr 30, 2023
1 parent 8dc54b6 commit fdb0375
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ decorators-comments.ts:41:5 parse ━━━━━━━━━━━━━━━
42 │ }
43 │
decorators-comments.ts:39:5 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Decorators are not valid here.
38 │ class Something3 {
> 39 │ @foo()
│ ^^^^^^
40 │ // comment
41 │ abstract method(): Array<string>
i Decorators are only valid on class declarations, class expressions, and class methods.
```

Expand Down
16 changes: 5 additions & 11 deletions crates/rome_js_parser/src/syntax/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,9 +2140,6 @@ impl ClassMemberModifiers {
| JS_METHOD_CLASS_MEMBER
| JS_GETTER_CLASS_MEMBER
| JS_SETTER_CLASS_MEMBER
| TS_METHOD_SIGNATURE_CLASS_MEMBER
| TS_GETTER_SIGNATURE_CLASS_MEMBER
| TS_SETTER_SIGNATURE_CLASS_MEMBER
| TS_PROPERTY_SIGNATURE_CLASS_MEMBER
| TS_INITIALIZED_PROPERTY_SIGNATURE_CLASS_MEMBER
)
Expand All @@ -2154,14 +2151,6 @@ impl ClassMemberModifiers {
// class Bar {
// @dec readonly foo = '123';
// }
// declare class Foo {
// @dec method();
// }
// declare class Baz {
// @dec method();
// @dec get foo();
// @dec set foo(a);
// }

// test_err ts decorator_class_member
// class Foo {
Expand All @@ -2177,6 +2166,11 @@ impl ClassMemberModifiers {
// constructor(a: String)
// constructor(a?: String) {}
// }
// declare class Baz {
// @dec method();
// @dec get foo();
// @dec set foo(a);
// }
return Some(decorators_not_allowed(p, modifier.as_text_range()));
} else if member_kind == TS_INDEX_SIGNATURE_CLASS_MEMBER
&& !matches!(modifier.kind, ModifierKind::Static | ModifierKind::Readonly)
Expand Down
206 changes: 202 additions & 4 deletions crates/rome_js_parser/test_data/inline/err/decorator_class_member.rast
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,105 @@ JsModule {
],
r_curly_token: [email protected] "}" [Newline("\n")] [],
},
TsDeclareStatement {
declare_token: [email protected] "declare" [Newline("\n")] [Whitespace(" ")],
declaration: JsClassDeclaration {
decorators: JsDecoratorList [],
abstract_token: missing (optional),
class_token: [email protected] "class" [] [Whitespace(" ")],
id: JsIdentifierBinding {
name_token: [email protected] "Baz" [] [Whitespace(" ")],
},
type_parameters: missing (optional),
extends_clause: missing (optional),
implements_clause: missing (optional),
l_curly_token: [email protected] "{" [] [],
members: JsClassMemberList [
JsBogusMember {
items: [
TsMethodSignatureModifierList [
JsDecorator {
at_token: [email protected] "@" [Newline("\n"), Whitespace(" ")] [],
expression: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: [email protected] "dec" [] [Whitespace(" ")],
},
},
},
],
JsLiteralMemberName {
value: [email protected] "method" [] [],
},
JsParameters {
l_paren_token: [email protected] "(" [] [],
items: JsParameterList [],
r_paren_token: [email protected] ")" [] [],
},
[email protected] ";" [] [],
],
},
JsBogusMember {
items: [
TsMethodSignatureModifierList [
JsDecorator {
at_token: [email protected] "@" [Newline("\n"), Whitespace(" ")] [],
expression: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: [email protected] "dec" [] [Whitespace(" ")],
},
},
},
],
[email protected] "get" [] [Whitespace(" ")],
JsLiteralMemberName {
value: [email protected] "foo" [] [],
},
[email protected] "(" [] [],
[email protected] ")" [] [],
[email protected] ";" [] [],
],
},
JsBogusMember {
items: [
TsMethodSignatureModifierList [
JsDecorator {
at_token: [email protected] "@" [Newline("\n"), Whitespace(" ")] [],
expression: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: [email protected] "dec" [] [Whitespace(" ")],
},
},
},
],
[email protected] "set" [] [Whitespace(" ")],
JsLiteralMemberName {
value: [email protected] "foo" [] [],
},
[email protected] "(" [] [],
JsFormalParameter {
binding: JsIdentifierBinding {
name_token: [email protected] "a" [] [],
},
question_mark_token: missing (optional),
type_annotation: missing (optional),
initializer: missing (optional),
},
[email protected] ")" [] [],
[email protected] ";" [] [],
],
},
],
r_curly_token: [email protected] "}" [Newline("\n")] [],
},
},
],
eof_token: EOF@236..237 "" [Newline("\n")] [],
eof_token: EOF@312..313 "" [Newline("\n")] [],
}

0: JS_MODULE@0..237
0: JS_MODULE@0..313
0: (empty)
1: [email protected]
2: JS_MODULE_ITEM_LIST@0..236
2: JS_MODULE_ITEM_LIST@0..312
0: [email protected]
0: [email protected]
1: (empty)
Expand Down Expand Up @@ -435,7 +526,67 @@ JsModule {
2: [email protected]
3: [email protected] "}" [] []
9: [email protected] "}" [Newline("\n")] []
3: [email protected] "" [Newline("\n")] []
3: [email protected]
0: [email protected] "declare" [Newline("\n")] [Whitespace(" ")]
1: [email protected]
0: [email protected]
1: (empty)
2: [email protected] "class" [] [Whitespace(" ")]
3: [email protected]
0: [email protected] "Baz" [] [Whitespace(" ")]
4: (empty)
5: (empty)
6: (empty)
7: [email protected] "{" [] []
8: [email protected]
0: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "@" [Newline("\n"), Whitespace(" ")] []
1: [email protected]
0: [email protected]
0: [email protected] "dec" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "method" [] []
2: [email protected]
0: [email protected] "(" [] []
1: [email protected]
2: [email protected] ")" [] []
3: [email protected] ";" [] []
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "@" [Newline("\n"), Whitespace(" ")] []
1: [email protected]
0: [email protected]
0: [email protected] "dec" [] [Whitespace(" ")]
1: [email protected] "get" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "foo" [] []
3: [email protected] "(" [] []
4: [email protected] ")" [] []
5: [email protected] ";" [] []
2: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "@" [Newline("\n"), Whitespace(" ")] []
1: [email protected]
0: [email protected]
0: [email protected] "dec" [] [Whitespace(" ")]
1: [email protected] "set" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "foo" [] []
3: [email protected] "(" [] []
4: [email protected]
0: [email protected]
0: [email protected] "a" [] []
1: (empty)
2: (empty)
3: (empty)
5: [email protected] ")" [] []
6: [email protected] ";" [] []
9: [email protected] "}" [Newline("\n")] []
3: [email protected] "" [Newline("\n")] []
--
decorator_class_member.ts:2:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand Down Expand Up @@ -491,6 +642,48 @@ decorator_class_member.ts:9:4 parse ━━━━━━━━━━━━━━

i Decorators are only valid on class declarations, class expressions, and class methods.

--
decorator_class_member.ts:15:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Decorators are not valid here.

13 │ }
14 │ declare class Baz {
> 15 │ @dec method();
│ ^^^^
16 │ @dec get foo();
17 │ @dec set foo(a);

i Decorators are only valid on class declarations, class expressions, and class methods.

--
decorator_class_member.ts:16:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Decorators are not valid here.

14 │ declare class Baz {
15 │ @dec method();
> 16 │ @dec get foo();
│ ^^^^
17 │ @dec set foo(a);
18 │ }

i Decorators are only valid on class declarations, class expressions, and class methods.

--
decorator_class_member.ts:17:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Decorators are not valid here.

15 │ @dec method();
16 │ @dec get foo();
> 17 │ @dec set foo(a);
│ ^^^^
18 │ }
19 │

i Decorators are only valid on class declarations, class expressions, and class methods.

--
class Foo {
@dec constructor() {}
Expand All @@ -505,3 +698,8 @@ class Bar extends Foo {
constructor(a: String)
constructor(a?: String) {}
}
declare class Baz {
@dec method();
@dec get foo();
@dec set foo(a);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ class Bar extends Foo {
constructor(a: String)
constructor(a?: String) {}
}
declare class Baz {
@dec method();
@dec get foo();
@dec set foo(a);
}
Loading

0 comments on commit fdb0375

Please sign in to comment.