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

fix(rome_js_analyzer): precedence for useOptionalChain #3277

Merged
merged 2 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,31 @@ foo[/\w+/] && foo[/ab+c/].baz;

((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,4 +37,4 @@
36 36 |
37 37 | ((foo || {})()).bar;
38 38 |
39 | - (new foo || {}).bar
39 | + (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::NewWithoutArguments
} 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
NewWithoutArguments = 18,
LeftHandSide = 19,
Member = 20,
Primary = 21,
Group = 22,
}

impl OperatorPrecedence {
Expand Down