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

Commit

Permalink
fix(rome_js_analyzer): fix false positive for useOptionalChain
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Sep 28, 2022
1 parent 1022663 commit d77371b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ foo[12n] && foo[123n].baz;
foo[/\w+/] && foo[/ab+c/].baz;

((foo || {})()).bar;

(new foo || {}).bar
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ foo[/\w+/] && foo[/ab+c/].baz;
((foo || {})()).bar;
(new foo || {}).bar
((new foo) || {}).bar
```

# Diagnostics
```
validCases.ts:40:1 lint/nursery/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Change to an optional chain.
┌─ validCases.ts:40:1
40 │ (new foo || {}).bar
│ ^^^^^^^^^^^^^^^^^^^
i Suggested fix: Change to an optional chain.
| @@ -37,5 +37,5 @@
36 36 |
37 37 | ((foo || {})()).bar;
38 38 |
39 | - (new foo || {}).bar
39 | + (new foo)?.bar
40 40 | ((new foo) || {}).bar
```


8 changes: 7 additions & 1 deletion crates/rome_js_syntax/src/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,16 @@ impl JsAnyExpression {
JsAnyExpression::JsPostUpdateExpression(_)
| JsAnyExpression::JsPreUpdateExpression(_) => OperatorPrecedence::Update,
JsAnyExpression::JsCallExpression(_)
| JsAnyExpression::JsNewExpression(_)
| JsAnyExpression::JsImportCallExpression(_)
| JsAnyExpression::JsSuperExpression(_) => OperatorPrecedence::LeftHandSide,

JsAnyExpression::JsNewExpression(expression) => {
if expression.arguments().is_none() {
OperatorPrecedence::New
} else {
OperatorPrecedence::LeftHandSide
}
}
JsAnyExpression::JsComputedMemberExpression(_)
| JsAnyExpression::JsStaticMemberExpression(_)
| JsAnyExpression::ImportMeta(_)
Expand Down
10 changes: 6 additions & 4 deletions crates/rome_js_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,12 @@ pub enum OperatorPrecedence {
Exponential = 15,
Unary = 16,
Update = 17,
LeftHandSide = 18,
Member = 19,
Primary = 20,
Group = 21,
// `new` without arguments list
New = 18,
LeftHandSide = 19,
Member = 20,
Primary = 21,
Group = 22,
}

impl OperatorPrecedence {
Expand Down

0 comments on commit d77371b

Please sign in to comment.