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

feat(rome_js_analyze): useCamelCase #2926

Merged
merged 12 commits into from
Jul 29, 2022
Merged

feat(rome_js_analyze): useCamelCase #2926

merged 12 commits into from
Jul 29, 2022

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Jul 25, 2022

Summary

Implements #2903.

Test Plan

> cargo rome-cli check ./crates/rome_js_analyze/tests/specs/js/useCamelCase.js

Camel Case

This PR implements only the happy path for camel case. It does not support nice camel case for abbreviations and acronyms.

Renaming and conflicts

Given that we rename different bindings to possibly the same name, they can conflict. The renaming is correctly dealing with this. So if we have:

let snake_case
snake_case = 1;
let _snake_case
console.log(_snake_case);

After renaming everything in one go, we have:

let snakeCase
snakeCase = 1;
let snakeCase2
console.log(snakeCase2);

But this is not reflected in the diagnostic phase. Because there is no conflict there. This is something we may need to address in another PR.

warning[js/useCamelCase]: Prefer symbols names in camel case.
  ┌─ ./crates/rome_js_analyze/tests/specs/js/useCamelCase.js:1:5
  │
1 │ let snake_case;
  │     ----------
2 │ snake_case = 1;
  │ ---------- Used here.

Safe fix: Rename this symbol to camel case
    | @@ -1,5 +1,5 @@
0   | - let snake_case;
1   | - snake_case = 1;
  0 | + let snakeCase;
  1 | + snakeCase = 1;
2 2 |   let _snake_case;
3 3 |   console.log(_snake_case);
4 4 |   


warning[js/useCamelCase]: Prefer symbols names in camel case.
  ┌─ ./crates/rome_js_analyze/tests/specs/js/useCamelCase.js:3:5
  │
3 │ let _snake_case;
  │     -----------
4 │ console.log(_snake_case);
  │             ----------- Used here.

Safe fix: Rename this symbol to camel case
    | @@ -1,7 +1,7 @@
0 0 |   let snake_case;
1 1 |   snake_case = 1;
2   | - let _snake_case;
3   | - console.log(_snake_case);
  2 | + let snakeCase;
  3 | + console.log(snakeCase);
4 4 |   
5 5 |   function f(snake_case, PascalCase) {}
6 6 |  

Performance

I compared this PR to https://github.com/SkylerLipthay/case. But focusing on being faster when everything is ok. Now we are only slower when we actually need to transform the string. We run fewer instructions, but unfortunately, we access the memory more times.

rome_all_lowercase
  Instructions:                 194 
  L1 Accesses:                  226
  L2 Accesses:                    4 
  RAM Accesses:                   7
  Estimated Cycles:             491

case_all_lowercase
  Instructions:                 613
  L1 Accesses:                  768
  L2 Accesses:                    5 
  RAM Accesses:                  12
  Estimated Cycles:            1213 

rome_already_camel_case
  Instructions:                 194 
  L1 Accesses:                  226 
  L2 Accesses:                    3 
  RAM Accesses:                   8 
  Estimated Cycles:             521 

case_already_camel_case
  Instructions:                 613 
  L1 Accesses:                  769 
  L2 Accesses:                    4 
  RAM Accesses:                  12 
  Estimated Cycles:            1209 

rome_pascal_case
  Instructions:                 574 
  L1 Accesses:                  767 
  L2 Accesses:                    7 
  RAM Accesses:                  25 
  Estimated Cycles:            1677 

case_pascal_case
  Instructions:                 613 
  L1 Accesses:                  771 
  L2 Accesses:                    3 
  RAM Accesses:                  11 
  Estimated Cycles:            1171 

@xunilrj xunilrj requested a review from a team July 25, 2022 12:08
@xunilrj xunilrj temporarily deployed to aws July 25, 2022 12:08 Inactive
@github-actions
Copy link

github-actions bot commented Jul 25, 2022

@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 395 395 0
Failed 5551 5551 0
Panics 0 0 0
Coverage 6.64% 6.64% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12393 12393 0
Failed 3864 3864 0
Panics 0 0 0
Coverage 76.23% 76.23% 0.00%

@xunilrj xunilrj force-pushed the feature/use-camel-case branch from 4a47d05 to 5c67f07 Compare July 25, 2022 12:17
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 25, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 51d682d
Status: ✅  Deploy successful!
Preview URL: https://227535e1.tools-8rn.pages.dev
Branch Preview URL: https://feature-use-camel-case.tools-8rn.pages.dev

View logs

@xunilrj xunilrj temporarily deployed to aws July 25, 2022 12:17 Inactive
@xunilrj
Copy link
Contributor Author

xunilrj commented Jul 25, 2022

Relevant for: #2925

@@ -19,8 +19,14 @@ rome_console = { path = "../rome_console" }
rome_diagnostics = { path = "../rome_diagnostics" }
roaring = "0.9.0"
rustc-hash = "1.1.0"
iai = "0.1.1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it have to be a dependency? Can it be moved in dev-dependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Wrong place.

const CATEGORY: RuleCategory = RuleCategory::Lint;

type Query = Semantic<JsIdentifierBinding>;
type State = (String, Vec<Reference>);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you document the state please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed for a struct.

/// let candidates = once(Cow::from(new_name)).chain(candidates);
/// batch.try_rename_node_declaration_until_success(model, node, candidates);
/// ```
fn try_rename_node_declaration_until_success<S, I>(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using try_* should hint to return Result, at least that's what the convention seems to be.

Copy link
Contributor

Choose a reason for hiding this comment

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

The name of the function seems different from what it actually does. If inside candidates we have more than one eligible for renaming, only the first gets renamed and the rest don't. I think we should review the logic or the name of the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I renamed this function.

@xunilrj xunilrj force-pushed the feature/use-camel-case branch from 5c67f07 to 7da9e99 Compare July 28, 2022 12:36
@xunilrj xunilrj temporarily deployed to aws July 28, 2022 12:36 Inactive
@xunilrj xunilrj temporarily deployed to aws July 28, 2022 12:38 Inactive
pub(crate) UseCamelCase {
version: "0.8.0",
name: "useCamelCase",
recommended: true
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think if we don't recommend the rule? Mostly because it's not finished as you suggested. But I don't mind having it recommended too. No strong opinions, as long as the current implementation is reliable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@xunilrj xunilrj temporarily deployed to aws July 28, 2022 12:42 Inactive
@xunilrj xunilrj temporarily deployed to aws July 28, 2022 12:43 Inactive
@xunilrj xunilrj temporarily deployed to aws July 28, 2022 12:45 Inactive
@xunilrj xunilrj temporarily deployed to aws July 28, 2022 12:53 Inactive
@xunilrj xunilrj merged commit 2f17ecf into main Jul 29, 2022
@xunilrj xunilrj deleted the feature/use-camel-case branch July 29, 2022 16:57
@IWANABETHATGUY
Copy link
Contributor

!bench_analyzer

@IWANABETHATGUY IWANABETHATGUY mentioned this pull request Jul 30, 2022
@github-actions
Copy link

Analyzer Benchmark Results

group                     main                                   pr
-----                     ----                                   --
analyzer/css.js           1.00   1980.8±4.15µs     5.9 MB/sec    1.37      2.7±0.02ms     4.3 MB/sec
analyzer/index.js         1.00      4.7±0.00ms     6.8 MB/sec    1.31      6.2±0.10ms     5.2 MB/sec
analyzer/lint.ts          1.00      2.7±0.01ms    15.6 MB/sec    1.39      3.7±0.01ms    11.3 MB/sec
analyzer/parser.ts        1.00      6.4±0.01ms     7.6 MB/sec    1.35      8.6±0.03ms     5.6 MB/sec
analyzer/router.ts        1.00      4.5±0.01ms    13.7 MB/sec    1.34      6.0±0.01ms    10.2 MB/sec
analyzer/statement.ts     1.00      6.0±0.06ms     5.9 MB/sec    1.30      7.8±0.01ms     4.6 MB/sec
analyzer/typescript.ts    1.00      8.8±0.02ms     6.2 MB/sec    1.30     11.4±0.02ms     4.8 MB/sec

IWANABETHATGUY pushed a commit to IWANABETHATGUY/tools that referenced this pull request Aug 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载