tools icon indicating copy to clipboard operation
tools copied to clipboard

feat(rome_js_analyze): `useDefaultSwitchClauseLast`

Open Conaclos opened this issue 3 years ago • 5 comments

Summary

This implements ESLint's default-case-last.

Test Plan

Unit test included.

Conaclos avatar Nov 18 '22 17:11 Conaclos

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
Latest commit 12d448899832b800131c58aa9e206e9b3011a018
Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/637e92c3184866000879140a
Deploy Preview https://deploy-preview-3791--docs-rometools.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Nov 18 '22 17:11 netlify[bot]

@ematipico

I think we can also provide a code action that moves the default statement to the last position. We can add the code action in another PR.

The difficulty is when a fallthrough is present. Like in the following code:

switch (foo) {
  default:
    doSomethingIfNotZero();
  case 0:
    doSomethingAnyway();
}

This requires some logic to know whether the block return/break. Have you an idea how to implement this without exploring all descendants? Or we could just handle the case where a block statement ends with return or break. This looks fair.

In fact I - in constrast to ESLint - we could allow code with default clause that fallthrough (as the previous code). Another rule (noFallthrough) could reject this code. This could make useDefaultCaseLast less restrictive.

Also, I wonder if we should rename the rule "useDefaultCaseLast" to "useDefaultClauseLast" or "useDefaultSwitchClauseLast".

Conaclos avatar Nov 22 '22 22:11 Conaclos

!bench_analyzer

MichaReiser avatar Nov 23 '22 07:11 MichaReiser

Also, I wonder if we should rename the rule "useDefaultCaseLast" to "useDefaultClauseLast" or "useDefaultSwitchClauseLast".

I'm in favor of renaming Case to Clause as this aligns with the terminology used in the diagnostics and useDefaultSwitchClauseLast goes well with our philosophy;

Utilize verbosity when naming commands and flags. No unnecessary and confusing abbreviations.

MichaReiser avatar Nov 23 '22 07:11 MichaReiser

Analyzer Benchmark Results

group                     main                                   pr
-----                     ----                                   --
analyzer/css.js           1.17      2.5±0.01ms     4.7 MB/sec    1.00      2.1±0.02ms     5.5 MB/sec
analyzer/index.js         1.08      6.6±0.12ms     5.0 MB/sec    1.00      6.1±0.03ms     5.3 MB/sec
analyzer/lint.ts          1.04      3.2±0.01ms    13.1 MB/sec    1.00      3.1±0.01ms    13.6 MB/sec
analyzer/parser.ts        1.08      7.8±0.24ms     6.3 MB/sec    1.00      7.2±0.11ms     6.8 MB/sec
analyzer/router.ts        1.08      5.4±0.03ms    11.6 MB/sec    1.00      5.0±0.01ms    12.5 MB/sec
analyzer/statement.ts     1.05      7.3±0.12ms     4.8 MB/sec    1.00      7.0±0.04ms     5.1 MB/sec
analyzer/typescript.ts    1.03     10.9±0.18ms     5.0 MB/sec    1.00     10.5±0.03ms     5.2 MB/sec

github-actions[bot] avatar Nov 23 '22 07:11 github-actions[bot]

@ematipico @MichaReiser Ready for merging.

I will open a new PR for the code action. This allows discussing that in details.

Unfortunately, a commit in main causes a failure on the format check. Indeed, ./crates/rome_js_analyze/src/semantic_analyzers/nursery/use_exhaustive_dependencies.rs was not formatted with rustfmt.

Conaclos avatar Nov 23 '22 21:11 Conaclos

Unfortunately, a commit in main causes a failure on the format check. Indeed, ./crates/rome_js_analyze/src/semantic_analyzers/nursery/use_exhaustive_dependencies.rs was not formatted with rustfmt.

Fixed in https://github.com/rome/tools/pull/3840

lucasweng avatar Nov 24 '22 04:11 lucasweng