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.
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
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
Expand Up @@ -258,14 +258,105 @@ JsModule {
],
r_curly_token: R_CURLY@234..236 "}" [Newline("\n")] [],
},
TsDeclareStatement {
declare_token: DECLARE_KW@236..245 "declare" [Newline("\n")] [Whitespace(" ")],
declaration: JsClassDeclaration {
decorators: JsDecoratorList [],
abstract_token: missing (optional),
class_token: CLASS_KW@245..251 "class" [] [Whitespace(" ")],
id: JsIdentifierBinding {
name_token: IDENT@251..255 "Baz" [] [Whitespace(" ")],
},
type_parameters: missing (optional),
extends_clause: missing (optional),
implements_clause: missing (optional),
l_curly_token: L_CURLY@255..256 "{" [] [],
members: JsClassMemberList [
JsBogusMember {
items: [
TsMethodSignatureModifierList [
JsDecorator {
at_token: AT@256..260 "@" [Newline("\n"), Whitespace(" ")] [],
expression: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: IDENT@260..264 "dec" [] [Whitespace(" ")],
},
},
},
],
JsLiteralMemberName {
value: IDENT@264..270 "method" [] [],
},
JsParameters {
l_paren_token: L_PAREN@270..271 "(" [] [],
items: JsParameterList [],
r_paren_token: R_PAREN@271..272 ")" [] [],
},
SEMICOLON@272..273 ";" [] [],
],
},
JsBogusMember {
items: [
TsMethodSignatureModifierList [
JsDecorator {
at_token: AT@273..277 "@" [Newline("\n"), Whitespace(" ")] [],
expression: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: IDENT@277..281 "dec" [] [Whitespace(" ")],
},
},
},
],
GET_KW@281..285 "get" [] [Whitespace(" ")],
JsLiteralMemberName {
value: IDENT@285..288 "foo" [] [],
},
L_PAREN@288..289 "(" [] [],
R_PAREN@289..290 ")" [] [],
SEMICOLON@290..291 ";" [] [],
],
},
JsBogusMember {
items: [
TsMethodSignatureModifierList [
JsDecorator {
at_token: AT@291..295 "@" [Newline("\n"), Whitespace(" ")] [],
expression: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: IDENT@295..299 "dec" [] [Whitespace(" ")],
},
},
},
],
SET_KW@299..303 "set" [] [Whitespace(" ")],
JsLiteralMemberName {
value: IDENT@303..306 "foo" [] [],
},
L_PAREN@306..307 "(" [] [],
JsFormalParameter {
binding: JsIdentifierBinding {
name_token: IDENT@307..308 "a" [] [],
},
question_mark_token: missing (optional),
type_annotation: missing (optional),
initializer: missing (optional),
},
R_PAREN@308..309 ")" [] [],
SEMICOLON@309..310 ";" [] [],
],
},
],
r_curly_token: R_CURLY@310..312 "}" [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: JS_DIRECTIVE_LIST@0..0
2: JS_MODULE_ITEM_LIST@0..236
2: JS_MODULE_ITEM_LIST@0..312
0: JS_CLASS_DECLARATION@0..81
0: JS_DECORATOR_LIST@0..0
1: (empty)
Expand Down Expand Up @@ -435,7 +526,67 @@ JsModule {
2: JS_STATEMENT_LIST@233..233
3: R_CURLY@233..234 "}" [] []
9: R_CURLY@234..236 "}" [Newline("\n")] []
3: EOF@236..237 "" [Newline("\n")] []
3: TS_DECLARE_STATEMENT@236..312
0: DECLARE_KW@236..245 "declare" [Newline("\n")] [Whitespace(" ")]
1: JS_CLASS_DECLARATION@245..312
0: JS_DECORATOR_LIST@245..245
1: (empty)
2: CLASS_KW@245..251 "class" [] [Whitespace(" ")]
3: JS_IDENTIFIER_BINDING@251..255
0: IDENT@251..255 "Baz" [] [Whitespace(" ")]
4: (empty)
5: (empty)
6: (empty)
7: L_CURLY@255..256 "{" [] []
8: JS_CLASS_MEMBER_LIST@256..310
0: JS_BOGUS_MEMBER@256..273
0: TS_METHOD_SIGNATURE_MODIFIER_LIST@256..264
0: JS_DECORATOR@256..264
0: AT@256..260 "@" [Newline("\n"), Whitespace(" ")] []
1: JS_IDENTIFIER_EXPRESSION@260..264
0: JS_REFERENCE_IDENTIFIER@260..264
0: IDENT@260..264 "dec" [] [Whitespace(" ")]
1: JS_LITERAL_MEMBER_NAME@264..270
0: IDENT@264..270 "method" [] []
2: JS_PARAMETERS@270..272
0: L_PAREN@270..271 "(" [] []
1: JS_PARAMETER_LIST@271..271
2: R_PAREN@271..272 ")" [] []
3: SEMICOLON@272..273 ";" [] []
1: JS_BOGUS_MEMBER@273..291
0: TS_METHOD_SIGNATURE_MODIFIER_LIST@273..281
0: JS_DECORATOR@273..281
0: AT@273..277 "@" [Newline("\n"), Whitespace(" ")] []
1: JS_IDENTIFIER_EXPRESSION@277..281
0: JS_REFERENCE_IDENTIFIER@277..281
0: IDENT@277..281 "dec" [] [Whitespace(" ")]
1: GET_KW@281..285 "get" [] [Whitespace(" ")]
2: JS_LITERAL_MEMBER_NAME@285..288
0: IDENT@285..288 "foo" [] []
3: L_PAREN@288..289 "(" [] []
4: R_PAREN@289..290 ")" [] []
5: SEMICOLON@290..291 ";" [] []
2: JS_BOGUS_MEMBER@291..310
0: TS_METHOD_SIGNATURE_MODIFIER_LIST@291..299
0: JS_DECORATOR@291..299
0: AT@291..295 "@" [Newline("\n"), Whitespace(" ")] []
1: JS_IDENTIFIER_EXPRESSION@295..299
0: JS_REFERENCE_IDENTIFIER@295..299
0: IDENT@295..299 "dec" [] [Whitespace(" ")]
1: SET_KW@299..303 "set" [] [Whitespace(" ")]
2: JS_LITERAL_MEMBER_NAME@303..306
0: IDENT@303..306 "foo" [] []
3: L_PAREN@306..307 "(" [] []
4: JS_FORMAL_PARAMETER@307..308
0: JS_IDENTIFIER_BINDING@307..308
0: IDENT@307..308 "a" [] []
1: (empty)
2: (empty)
3: (empty)
5: R_PAREN@308..309 ")" [] []
6: SEMICOLON@309..310 ";" [] []
9: R_CURLY@310..312 "}" [Newline("\n")] []
3: EOF@312..313 "" [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);
}
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);
}

0 comments on commit fdb0375

Please sign in to comment.