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

feat(rome_js_parser): EcmaScript @decorators #4252 #4415

Merged
merged 1 commit into from
May 2, 2023

Conversation

denbezrukov
Copy link
Contributor

@denbezrukov denbezrukov commented Apr 27, 2023

Summary

Implement decorators for class methods, class fields, class accessors.

ECMA spec page of the decorators:
https://arai-a.github.io/ecma262-compare/?pr=2417
https://github.com/tc39/proposal-decorators

Please note that the PR doesn't use JsDecoratorList but adds JsDecorator to AnyJsPropertyModifier and AnyJsMethodModifier.
This allows better error diagnostic:

pub(crate) fn decorator_must_precede_modifier(p: &JsParser, range: TextRange) -> ParseDiagnostic {
    p.err_builder(
        "Decorators must precede the name and all keywords of property declarations.",
        range,
    )
}

The PR adds syntax:

class Foo {
  // properties
  @dec foo = 2;
  @dec public foo = 1;
  @dec static foo = 2;
  @dec accessor foo = 2;
  @dec readonly foo = 2;
  @dec override foo = 2;
  // methods
  @dec foo() {}
  @dec public foo() {}
  @dec static foo() {}
  @dec override foo() {}
  // getters
  @dec get foo() {}
  @dec public get foo() {}
  @dec static get foo() {}
  @dec override get foo() {}
  // setters
  @dec set foo(val) {}
  @dec public set foo(val) {}
  @dec static set foo(val) {}
  @dec override set foo(val) {}
}

//typescript 
abstract class Qux {
  @dec declare static foo: string;
}
class Bar {
   @dec readonly foo = '123';
}

Test Plan

cargo test

Changelog

  • The PR requires a changelog line

Documentation

  • The PR requires documentation
  • I will create a new PR to update the documentation

@netlify
Copy link

netlify bot commented Apr 27, 2023

Deploy Preview for docs-rometools canceled.

Name Link
🔨 Latest commit bbfecf3
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/644f781220280400088edde6

@github-actions github-actions bot added A-Formatter Area: formatter A-Parser Area: parser A-Tooling Area: our own build, development, and release tooling labels Apr 27, 2023
@github-actions
Copy link

github-actions bot commented Apr 27, 2023

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 48863 48863 0
Passed 47796 47796 0
Failed 1067 1067 0
Panics 0 0 0
Coverage 97.82% 97.82% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 40 40 0
Passed 37 37 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.50% 92.50% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 6212 6212 0
Passed 1763 1763 0
Failed 4449 4449 0
Panics 0 0 0
Coverage 28.38% 28.38% 0.00%

ts/babel

Test result main count This PR count Difference
Total 639 639 0
Passed 573 573 0
Failed 66 66 0
Panics 0 0 0
Coverage 89.67% 89.67% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 17224 17224 0
Passed 13122 13127 ✅ ⏫ +5
Failed 4102 4097 ✅ ⏬ -5
Panics 0 0 0
Coverage 76.18% 76.21% +0.03%
🎉 Fixed (5):
conformance/classes/classStaticBlock/classStaticBlock19.ts
conformance/decorators/class/constructor/decoratorOnClassConstructor1.ts
conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts
conformance/esDecorators/classDeclaration/accessors/esDecorators-classDeclaration-accessors-nonStaticAbstract.ts
conformance/esDecorators/classDeclaration/methods/esDecorators-classDeclaration-methods-nonStaticAbstract.ts

@denbezrukov denbezrukov changed the title Feat/decorator class member feat(rome_js_parser): EcmaScript @decorators #4252 Apr 30, 2023
@denbezrukov denbezrukov marked this pull request as ready for review April 30, 2023 15:17
@ematipico
Copy link
Contributor

Please note that the PR doesn't use JsDecoratorList but adds JsDecorator to AnyJsPropertyModifier and AnyJsMethodModifier.

Does that mean we can only apply one decorator to a class method?

@denbezrukov
Copy link
Contributor Author

Please note that the PR doesn't use JsDecoratorList but adds JsDecorator to AnyJsPropertyModifier and AnyJsMethodModifier.

Does that mean we can only apply one decorator to a class method?

No, we can apply any number of decorators. The difference between class member decorators and class decorators is that class member decorators are part of the modifiers array.

AnyJsPropertyModifier =
	TsAccessibilityModifier
	| JsStaticModifier
	| JsAccessorModifier
	| JsDecorator
	| TsReadonlyModifier
	| TsOverrideModifier

JsPropertyModifierList = AnyJsPropertyModifier*

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

If you rebase your branch, you should be able to deploy the playground

crates/rome_js_parser/src/syntax/class.rs Outdated Show resolved Hide resolved
crates/rome_js_parser/src/syntax/class.rs Show resolved Hide resolved
crates/rome_js_parser/src/syntax/class.rs Show resolved Hide resolved
@ematipico
Copy link
Contributor

!bench_parser

@denbezrukov denbezrukov force-pushed the feat/decorator-class-member branch from fdb0375 to 0fb680c Compare May 1, 2023 07:30
@github-actions
Copy link

github-actions bot commented May 1, 2023

Parser Benchmark Results

group                                          main                                   pr
-----                                          ----                                   --
parser/big5-added.json/cached                  1.00    147.8±1.47µs   114.4 MB/sec    1.02    150.5±1.61µs   112.3 MB/sec
parser/big5-added.json/uncached                1.00    181.4±0.38µs    93.1 MB/sec    1.05    190.8±0.13µs    88.6 MB/sec
parser/canada.json/cached                      1.07     75.8±4.22ms    28.3 MB/sec    1.00     70.5±2.48ms    30.4 MB/sec
parser/canada.json/uncached                    1.00     86.1±1.95ms    24.9 MB/sec    1.01     87.1±2.25ms    24.7 MB/sec
parser/checker.ts/cached                       1.00    103.9±1.72ms    25.0 MB/sec    1.01    105.4±1.94ms    24.7 MB/sec
parser/checker.ts/uncached                     1.03   122.4±11.83ms    21.2 MB/sec    1.00    118.8±1.81ms    21.9 MB/sec
parser/compiler.js/cached                      1.00     60.0±1.77ms    17.5 MB/sec    1.05     62.7±1.63ms    16.7 MB/sec
parser/compiler.js/uncached                    1.00     66.4±0.87ms    15.8 MB/sec    1.04     68.9±1.11ms    15.2 MB/sec
parser/d3.min.js/cached                        1.00     38.8±0.74ms     6.7 MB/sec    1.00     39.0±0.54ms     6.7 MB/sec
parser/d3.min.js/uncached                      1.00     40.9±0.32ms     6.4 MB/sec    1.04     42.4±0.37ms     6.2 MB/sec
parser/db.json/cached                          1.00      4.1±0.01ms    44.9 MB/sec    1.03      4.2±0.01ms    43.4 MB/sec
parser/db.json/uncached                        1.00      4.8±0.02ms    38.3 MB/sec    1.05      5.0±0.07ms    36.4 MB/sec
parser/dojo.js/cached                          1.00      3.2±0.01ms    21.7 MB/sec    1.02      3.2±0.00ms    21.2 MB/sec
parser/dojo.js/uncached                        1.00      3.7±0.01ms    18.5 MB/sec    1.03      3.8±0.01ms    17.8 MB/sec
parser/eucjp.json/cached                       1.00    273.3±2.17µs   143.3 MB/sec    1.02    279.9±9.43µs   139.9 MB/sec
parser/eucjp.json/uncached                     1.00    318.0±0.35µs   123.2 MB/sec    1.05    333.0±0.53µs   117.6 MB/sec
parser/ios.d.ts/cached                         1.00     92.3±1.34ms    20.2 MB/sec    1.00     92.2±1.60ms    20.2 MB/sec
parser/ios.d.ts/uncached                       1.00     96.5±1.08ms    19.3 MB/sec    1.03     99.4±1.22ms    18.8 MB/sec
parser/jquery.min.js/cached                    1.00     10.3±0.04ms     8.0 MB/sec    1.03     10.6±0.06ms     7.8 MB/sec
parser/jquery.min.js/uncached                  1.00     11.2±0.04ms     7.4 MB/sec    1.04     11.7±0.04ms     7.1 MB/sec
parser/math.js/cached                          1.00     78.6±1.50ms     8.2 MB/sec    1.00     78.6±1.57ms     8.2 MB/sec
parser/math.js/uncached                        1.00     81.3±1.38ms     8.0 MB/sec    1.00     81.2±1.05ms     8.0 MB/sec
parser/package-lock.json/cached                1.00  1751.5±12.21µs    78.7 MB/sec    1.05   1837.7±7.25µs    75.0 MB/sec
parser/package-lock.json/uncached              1.00   1952.6±2.98µs    70.6 MB/sec    1.05      2.1±0.01ms    66.9 MB/sec
parser/parser.ts/cached                        1.00      2.3±0.02ms    21.4 MB/sec    1.02      2.3±0.00ms    20.9 MB/sec
parser/parser.ts/uncached                      1.00      2.6±0.01ms    18.5 MB/sec    1.03      2.7±0.01ms    17.9 MB/sec
parser/pixi.min.js/cached                      1.03     48.4±1.05ms     9.1 MB/sec    1.00     47.2±0.82ms     9.3 MB/sec
parser/pixi.min.js/uncached                    1.00     51.6±0.89ms     8.5 MB/sec    1.01     51.9±0.89ms     8.5 MB/sec
parser/react-dom.production.min.js/cached      1.00     13.6±0.03ms     8.5 MB/sec    1.03     13.9±0.16ms     8.3 MB/sec
parser/react-dom.production.min.js/uncached    1.00     15.1±0.06ms     7.6 MB/sec    1.03     15.5±0.05ms     7.4 MB/sec
parser/react.production.min.js/cached          1.00    655.0±3.83µs     9.4 MB/sec    1.01    661.7±3.05µs     9.3 MB/sec
parser/react.production.min.js/uncached        1.00    761.0±1.08µs     8.1 MB/sec    1.03    781.1±1.71µs     7.9 MB/sec
parser/router.ts/cached                        1.00    864.0±4.74µs    36.9 MB/sec    1.02    881.7±4.69µs    36.2 MB/sec
parser/router.ts/uncached                      1.00   1053.2±2.61µs    30.3 MB/sec    1.04   1092.0±2.65µs    29.2 MB/sec
parser/tex-chtml-full.js/cached                1.01    104.5±1.70ms     8.7 MB/sec    1.00    103.3±1.58ms     8.8 MB/sec
parser/tex-chtml-full.js/uncached              1.00    110.0±1.30ms     8.3 MB/sec    1.01    111.2±1.57ms     8.2 MB/sec
parser/three.min.js/cached                     1.04     54.6±1.37ms    10.8 MB/sec    1.00     52.5±1.17ms    11.2 MB/sec
parser/three.min.js/uncached                   1.01     56.9±0.66ms    10.3 MB/sec    1.00     56.6±0.57ms    10.4 MB/sec
parser/typescript.js/cached                    1.00    425.3±6.16ms    22.3 MB/sec    1.00    425.8±4.87ms    22.3 MB/sec
parser/typescript.js/uncached                  1.00    456.1±6.36ms    20.8 MB/sec    1.01    458.7±4.68ms    20.7 MB/sec
parser/vue.global.prod.js/cached               1.00     16.8±0.14ms     7.2 MB/sec    1.03     17.3±0.07ms     7.0 MB/sec
parser/vue.global.prod.js/uncached             1.00     18.4±0.08ms     6.6 MB/sec    1.04     19.2±0.10ms     6.3 MB/sec

@denbezrukov denbezrukov force-pushed the feat/decorator-class-member branch from 676af18 to 27ad5dd Compare May 1, 2023 08:23
@denbezrukov denbezrukov force-pushed the feat/decorator-class-member branch from 27ad5dd to bbfecf3 Compare May 1, 2023 08:28
@denbezrukov
Copy link
Contributor Author

!bench_parser

@github-actions
Copy link

github-actions bot commented May 1, 2023

Parser Benchmark Results

group                                          main                                   pr
-----                                          ----                                   --
parser/big5-added.json/cached                  1.01    190.1±9.86µs    88.9 MB/sec    1.00    187.7±7.50µs    90.0 MB/sec
parser/big5-added.json/uncached                1.01    225.5±0.87µs    74.9 MB/sec    1.00    223.7±2.07µs    75.5 MB/sec
parser/canada.json/cached                      1.02    119.5±3.76ms    18.0 MB/sec    1.00    117.4±4.85ms    18.3 MB/sec
parser/canada.json/uncached                    1.01    118.5±3.20ms    18.1 MB/sec    1.00    117.1±2.89ms    18.3 MB/sec
parser/checker.ts/cached                       1.01    142.4±2.62ms    18.3 MB/sec    1.00    140.9±2.94ms    18.4 MB/sec
parser/checker.ts/uncached                     1.01    148.4±1.88ms    17.5 MB/sec    1.00    146.9±1.95ms    17.7 MB/sec
parser/compiler.js/cached                      1.01     81.5±1.94ms    12.9 MB/sec    1.00     80.5±2.09ms    13.0 MB/sec
parser/compiler.js/uncached                    1.01     85.9±1.94ms    12.2 MB/sec    1.00     85.1±1.08ms    12.3 MB/sec
parser/d3.min.js/cached                        1.04     50.4±1.07ms     5.2 MB/sec    1.00     48.6±1.30ms     5.4 MB/sec
parser/d3.min.js/uncached                      1.01     50.6±0.90ms     5.2 MB/sec    1.00     50.1±0.94ms     5.2 MB/sec
parser/db.json/cached                          1.02      5.2±0.07ms    35.0 MB/sec    1.00      5.1±0.05ms    35.6 MB/sec
parser/db.json/uncached                        1.00      5.8±0.05ms    31.5 MB/sec    1.02      5.9±0.06ms    30.9 MB/sec
parser/dojo.js/cached                          1.01      3.7±0.03ms    18.5 MB/sec    1.00      3.7±0.02ms    18.6 MB/sec
parser/dojo.js/uncached                        1.01      4.2±0.03ms    16.2 MB/sec    1.00      4.2±0.03ms    16.4 MB/sec
parser/eucjp.json/cached                       1.01    310.3±2.84µs   126.2 MB/sec    1.00    306.2±5.22µs   127.9 MB/sec
parser/eucjp.json/uncached                     1.01    356.3±1.71µs   109.9 MB/sec    1.00    353.6±2.70µs   110.7 MB/sec
parser/ios.d.ts/cached                         1.00    125.5±1.69ms    14.9 MB/sec    1.03    129.6±2.17ms    14.4 MB/sec
parser/ios.d.ts/uncached                       1.00    128.3±1.57ms    14.5 MB/sec    1.02    131.0±1.91ms    14.2 MB/sec
parser/jquery.min.js/cached                    1.03     12.5±0.16ms     6.6 MB/sec    1.00     12.1±0.15ms     6.8 MB/sec
parser/jquery.min.js/uncached                  1.02     13.1±0.14ms     6.3 MB/sec    1.00     12.9±0.12ms     6.4 MB/sec
parser/math.js/cached                          1.06    105.1±1.42ms     6.2 MB/sec    1.00     99.0±1.97ms     6.5 MB/sec
parser/math.js/uncached                        1.03    102.8±1.96ms     6.3 MB/sec    1.00     99.5±1.53ms     6.5 MB/sec
parser/package-lock.json/cached                1.01      2.1±0.01ms    64.2 MB/sec    1.00      2.1±0.02ms    64.8 MB/sec
parser/package-lock.json/uncached              1.00      2.4±0.01ms    58.5 MB/sec    1.00      2.4±0.02ms    58.5 MB/sec
parser/parser.ts/cached                        1.00      2.7±0.01ms    17.9 MB/sec  
parser/parser.ts/uncached                      1.00      3.1±0.01ms    15.8 MB/sec  
parser/pixi.min.js/cached                      1.00     62.8±2.08ms     7.0 MB/sec    1.00     62.7±1.95ms     7.0 MB/sec
parser/pixi.min.js/uncached                    1.02     64.5±1.04ms     6.8 MB/sec    1.00     63.0±1.13ms     7.0 MB/sec
parser/react-dom.production.min.js/cached      1.01     16.8±0.41ms     6.9 MB/sec    1.00     16.6±0.33ms     6.9 MB/sec
parser/react-dom.production.min.js/uncached    1.02     18.0±0.22ms     6.4 MB/sec    1.00     17.6±0.26ms     6.5 MB/sec
parser/react.production.min.js/cached          1.01    799.9±9.66µs     7.7 MB/sec    1.00    791.9±8.32µs     7.8 MB/sec
parser/react.production.min.js/uncached        1.01    918.4±5.93µs     6.7 MB/sec    1.00    909.6±5.77µs     6.8 MB/sec
parser/router.ts/cached                        1.00  1057.6±11.16µs    30.2 MB/sec    1.01   1063.6±9.02µs    30.0 MB/sec
parser/router.ts/uncached                      1.00   1263.5±2.61µs    25.3 MB/sec    1.00   1265.3±5.25µs    25.2 MB/sec
parser/tex-chtml-full.js/cached                1.04    138.7±1.78ms     6.6 MB/sec    1.00    133.7±1.90ms     6.8 MB/sec
parser/tex-chtml-full.js/uncached              1.03    140.2±2.37ms     6.5 MB/sec    1.00    135.7±1.72ms     6.7 MB/sec
parser/three.min.js/cached                     1.02     69.7±1.36ms     8.4 MB/sec    1.00     68.2±1.83ms     8.6 MB/sec
parser/three.min.js/uncached                   1.03     71.6±1.02ms     8.2 MB/sec    1.00     69.6±1.27ms     8.4 MB/sec
parser/typescript.js/cached                    1.02    575.4±8.59ms    16.5 MB/sec    1.00    563.4±7.20ms    16.9 MB/sec
parser/typescript.js/uncached                  1.02    589.7±4.80ms    16.1 MB/sec    1.00    578.2±5.84ms    16.4 MB/sec
parser/vue.global.prod.js/cached               1.00     20.8±0.60ms     5.8 MB/sec    1.00     20.7±0.50ms     5.8 MB/sec
parser/vue.global.prod.js/uncached             1.04     22.5±0.40ms     5.4 MB/sec    1.00     21.6±0.33ms     5.6 MB/sec

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Looks good to me! Feel free to merge it!

@denbezrukov denbezrukov merged commit 7547bc5 into main May 2, 2023
@denbezrukov denbezrukov deleted the feat/decorator-class-member branch May 2, 2023 14:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Formatter Area: formatter A-Parser Area: parser A-Tooling Area: our own build, development, and release tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants