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 b773a24 commit ffe4934
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 842 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,20 @@ class X {
```diff
--- Prettier
+++ Rome
@@ -1,4 +1,4 @@
@@ -1,4 +1,3 @@
class X {
- @(computed().volatile())
- x;
+@(computed().volatile())
+x;
+ @(computed().volatile()) x;
}
```

# Output

```js
class X {
@(computed().volatile())
x;
@(computed().volatile()) x;
}
```

# Errors
```
parens.js:2:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× expected an identifier, a string literal, a number literal, a private field name, or a computed name but instead found '@'
1 │ class X {
> 2 │ @(computed().volatile())
│ ^
3 │ x
4 │ }
i Expected an identifier, a string literal, a number literal, a private field name, or a computed name here
1 │ class X {
> 2 │ @(computed().volatile())
│ ^
3 │ x
4 │ }
parens.js:4:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× expected a statement but instead found '}'
2 │ @(computed().volatile())
3 │ x
> 4 │ }
│ ^
5 │
i Expected a statement here
2 │ @(computed().volatile())
3 │ x
> 4 │ }
│ ^
5 │
```


This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ class Class6 {
```diff
--- Prettier
+++ Rome
@@ -1,5 +1,8 @@
@@ -1,5 +1,7 @@
export class TestTextFileService {
- constructor(@ILifecycleService lifecycleService) {}
+ constructor(
+ @ILifecycleService lifecycleService,
+ ) {
+ }
+ @ILifecycleService lifecycleService,
+ ) {}
}

@commonEditorContribution
@@ -50,12 +53,24 @@
@@ -50,12 +52,24 @@
}

class Class5 {
Expand Down Expand Up @@ -139,9 +138,8 @@ class Class6 {
```ts
export class TestTextFileService {
constructor(
@ILifecycleService lifecycleService,
) {
}
@ILifecycleService lifecycleService,
) {}
}

@commonEditorContribution
Expand Down Expand Up @@ -215,22 +213,4 @@ class Class6 {
}
```

# Errors
```
decorators.ts:3:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Decorators are not valid here.
1 │ export class TestTextFileService {
2 │ constructor(
> 3 │ @ILifecycleService lifecycleService,
│ ^^^^^^^^^^^^^^^^^^
4 │ ) {
5 │ }
i Decorators are only valid on class declarations, class expressions, and class methods.
```


Original file line number Diff line number Diff line change
Expand Up @@ -165,63 +165,4 @@ class MyContainerComponent {
}
```

# Errors
```
inline-decorators.ts:32:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Decorators are not valid here.
31 │ constructor (
> 32 │ @d1 private x: number,
│ ^^^
33 │ @d2(foo) private y: number,
34 │ @d3('foo') private z: number,
i Decorators are only valid on class declarations, class expressions, and class methods.
inline-decorators.ts:33:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Decorators are not valid here.
31 │ constructor (
32 │ @d1 private x: number,
> 33 │ @d2(foo) private y: number,
│ ^^^^^^^^
34 │ @d3('foo') private z: number,
35 │ @d4({
i Decorators are only valid on class declarations, class expressions, and class methods.
inline-decorators.ts:34:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Decorators are not valid here.
32 │ @d1 private x: number,
33 │ @d2(foo) private y: number,
> 34 │ @d3('foo') private z: number,
│ ^^^^^^^^^^
35 │ @d4({
36 │ x: string
i Decorators are only valid on class declarations, class expressions, and class methods.
inline-decorators.ts:35:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Decorators are not valid here.
33 │ @d2(foo) private y: number,
34 │ @d3('foo') private z: number,
> 35 │ @d4({
│ ^^^^^
> 36 │ x: string
> 37 │ }) private a: string,
│ ^^
38 │ ) {}
39 │ }
i Decorators are only valid on class declarations, class expressions, and class methods.
```


3 changes: 3 additions & 0 deletions crates/rome_js_parser/src/syntax/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,9 @@ impl ClassMemberModifiers {
// abstract class Qux {
// @dec declare static foo: string;
// }
// class Bar {
// @dec declare foo = '123';
// }
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
5 changes: 2 additions & 3 deletions crates/rome_js_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,9 @@ fn diagnostics_print_correctly() {
#[test]
pub fn quick_test() {
let code = r#"
class C {
constructor(@foo readonly x: number) {}
class Foo {
@decorator declare a;
}
"#;
let root = parse(code, SourceType::ts());
let syntax = root.syntax();
Expand Down
Loading

0 comments on commit ffe4934

Please sign in to comment.