From d77371b8db9be6ac5cd9d471e6ebeca2392721ff Mon Sep 17 00:00:00 2001 From: ematipico Date: Mon, 26 Sep 2022 14:49:40 +0100 Subject: [PATCH] fix(rome_js_analyzer): fix false positive for `useOptionalChain` --- .../nursery/useOptionalChain/validCases.ts | 2 ++ .../useOptionalChain/validCases.ts.snap | 27 +++++++++++++++++++ crates/rome_js_syntax/src/expr_ext.rs | 8 +++++- crates/rome_js_syntax/src/lib.rs | 10 ++++--- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts b/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts index 2020bcc53d0e..13ee4657c84a 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts +++ b/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts @@ -36,3 +36,5 @@ foo[12n] && foo[123n].baz; foo[/\w+/] && foo[/ab+c/].baz; ((foo || {})()).bar; + +(new foo || {}).bar diff --git a/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts.snap b/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts.snap index 7d14dbb4d541..743ef0a54f5a 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts.snap +++ b/crates/rome_js_analyze/tests/specs/nursery/useOptionalChain/validCases.ts.snap @@ -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 + + ``` diff --git a/crates/rome_js_syntax/src/expr_ext.rs b/crates/rome_js_syntax/src/expr_ext.rs index 5c6ca39469d3..b7910e0271bb 100644 --- a/crates/rome_js_syntax/src/expr_ext.rs +++ b/crates/rome_js_syntax/src/expr_ext.rs @@ -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(_) diff --git a/crates/rome_js_syntax/src/lib.rs b/crates/rome_js_syntax/src/lib.rs index 61040eac6a0f..3636100ff924 100644 --- a/crates/rome_js_syntax/src/lib.rs +++ b/crates/rome_js_syntax/src/lib.rs @@ -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 {