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_analyze): useOptionalChain #2748 (#3085)
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov authored Sep 12, 2022
1 parent 3160efa commit 28e5351
Show file tree
Hide file tree
Showing 17 changed files with 9,067 additions and 8 deletions.
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/js.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

862 changes: 862 additions & 0 deletions crates/rome_js_analyze/src/analyzers/js/use_optional_chain.rs

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions crates/rome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ mod tests {

use crate::{analyze, AnalysisFilter, ControlFlow};

#[ignore]
#[test]
fn quick_test() {
const SOURCE: &str = "
foo.bar && foo.bar?.();
";

let parsed = parse(SOURCE, 0, SourceType::js_module());

let mut error_ranges = Vec::new();
analyze(0, &parsed.tree(), AnalysisFilter::default(), |signal| {
if let Some(diag) = signal.diagnostic() {
let diag = diag.into_diagnostic(Severity::Warning);
let primary = diag.primary.as_ref().unwrap();

error_ranges.push(primary.span.range);
}

dbg!(signal.action());

ControlFlow::<Never>::Continue(())
});

assert_eq!(error_ranges.as_slice(), &[]);
}

#[test]
fn suppression() {
const SOURCE: &str = "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// currently do not handle complex computed properties
foo && foo[bar as string] && foo[bar as string].baz;
foo && foo[1 + 2] && foo[1 + 2].baz;
foo && foo[typeof bar] && foo[typeof bar].baz;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: complexLogicalAndCases.ts
---
# Input
```js
// currently do not handle complex computed properties
foo && foo[bar as string] && foo[bar as string].baz;
foo && foo[1 + 2] && foo[1 + 2].baz;
foo && foo[typeof bar] && foo[typeof bar].baz;
```

# Diagnostics
```
warning[js/useOptionalChain]: Change to an optional chain.
┌─ complexLogicalAndCases.ts:2:1
2 │ foo && foo[bar as string] && foo[bar as string].baz;
│ -------------------------
Suggested fix: Change to an optional chain.
| @@ -1,4 +1,4 @@
0 0 | // currently do not handle complex computed properties
1 | - foo && foo[bar as string] && foo[bar as string].baz;
1 | + foo?.[bar as string] && foo[bar as string].baz;
2 2 | foo && foo[1 + 2] && foo[1 + 2].baz;
3 3 | foo && foo[typeof bar] && foo[typeof bar].baz;
```

```
warning[js/useOptionalChain]: Change to an optional chain.
┌─ complexLogicalAndCases.ts:3:1
3 │ foo && foo[1 + 2] && foo[1 + 2].baz;
│ -----------------
Suggested fix: Change to an optional chain.
| @@ -1,4 +1,4 @@
0 0 | // currently do not handle complex computed properties
1 1 | foo && foo[bar as string] && foo[bar as string].baz;
2 | - foo && foo[1 + 2] && foo[1 + 2].baz;
2 | + foo?.[1 + 2] && foo[1 + 2].baz;
3 3 | foo && foo[typeof bar] && foo[typeof bar].baz;
```

```
warning[js/useOptionalChain]: Change to an optional chain.
┌─ complexLogicalAndCases.ts:4:1
4 │ foo && foo[typeof bar] && foo[typeof bar].baz;
│ ----------------------
Suggested fix: Change to an optional chain.
| @@ -1,4 +1,4 @@
0 0 | // currently do not handle complex computed properties
1 1 | foo && foo[bar as string] && foo[bar as string].baz;
2 2 | foo && foo[1 + 2] && foo[1 + 2].baz;
3 | - foo && foo[typeof bar] && foo[typeof bar].baz;
3 | + foo?.[typeof bar] && foo[typeof bar].baz;
```


Loading

0 comments on commit 28e5351

Please sign in to comment.