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 ea68d64 commit 35a60af
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 427 deletions.
28 changes: 27 additions & 1 deletion crates/rome_js_parser/src/syntax/class.rs
Expand Up @@ -2156,7 +2156,7 @@ impl ClassMemberModifiers {
| JS_SETTER_CLASS_MEMBER
)
{
// test_err ts decorator_parameters
// test ts decorator_parameters_constructor
// class Foo {
// constructor(@dec a: string) {}
// }
Expand Down Expand Up @@ -2642,3 +2642,29 @@ fn parse_decorator(p: &mut JsParser) -> ParsedSyntax {

Present(m.complete(p, JS_DECORATOR))
}

/// Skips over any TypeScript decorator syntax.
pub(crate) fn skip_ts_decorators(p: &mut JsParser) {
if !p.at(T![@]) {
return;
}

p.parse_as_skipped_trivia_tokens(|p| {
while p.at(T![@]) {
parse_decorator_bogus(p).ok();
}
});
}

fn parse_decorator_bogus(p: &mut JsParser) -> ParsedSyntax {
if p.at(T![@]) {
let m = p.start();
p.bump(T![@]);
parse_lhs_expr(p, ExpressionContext::default().and_in_decorator(true))
.or_add_diagnostic(p, expected_expression);

Present(m.complete(p, JS_BOGUS))
} else {
Absent
}
}
11 changes: 4 additions & 7 deletions crates/rome_js_parser/src/syntax/function.rs
Expand Up @@ -4,14 +4,12 @@ use crate::state::{EnterFunction, EnterParameters, SignatureFlags};
use crate::syntax::binding::{
is_at_identifier_binding, is_nth_at_identifier_binding, parse_binding, parse_binding_pattern,
};
use crate::syntax::class::{parse_decorators, parse_initializer_clause};
use crate::syntax::class::{parse_initializer_clause, skip_ts_decorators};
use crate::syntax::expr::{
is_nth_at_identifier, parse_assignment_expression_or_higher, ExpressionContext,
};
use crate::syntax::js_parse_error;
use crate::syntax::js_parse_error::{
decorators_not_allowed, expected_binding, expected_parameter, expected_parameters,
};
use crate::syntax::js_parse_error::{expected_binding, expected_parameter, expected_parameters};
use crate::syntax::stmt::{is_semi, parse_block_impl, semi, StatementContext};
use crate::syntax::typescript::ts_parse_error::ts_only_syntax_error;
use crate::syntax::typescript::{
Expand Down Expand Up @@ -1026,14 +1024,13 @@ pub(crate) fn parse_formal_parameter(
parameter_context: ParameterContext,
expression_context: ExpressionContext,
) -> ParsedSyntax {
// test_err ts ts_formal_parameter_decorator
// test ts ts_formal_parameter_decorator
// function a(@dec x) {}
// class Foo {
// constructor(@dec x) {}
// method(@dec x) {}
// }
let decorator_list = parse_decorators(p);
decorator_list.add_diagnostic_if_present(p, decorators_not_allowed);
skip_ts_decorators(p);

parse_binding_pattern(p, expression_context).map(|binding| {
let binding_kind = binding.kind(p);
Expand Down
132 changes: 0 additions & 132 deletions crates/rome_js_parser/test_data/inline/err/decorator_parameters.rast

This file was deleted.

0 comments on commit 35a60af

Please sign in to comment.