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 26, 2022
1 parent 1022663 commit f8fc203
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl Rule for UseOptionalChain {
JsLogicalOperator::NullishCoalescing | JsLogicalOperator::LogicalOr => {
let chain = LogicalOrLikeChain::from_expression(logical)?;

dbg!(&chain);
if chain.is_inside_another_chain() {
return None;
}
Expand Down Expand Up @@ -641,6 +642,9 @@ impl LogicalOrLikeChain {
/// ```js
/// (foo || {}).bar;
/// ```
///
/// The chain is not allowed if:
/// - `left` contains a `JsNewExpression`, e.g. `(new foo() || {}).bar`
fn from_expression(logical: &JsLogicalExpression) -> Option<LogicalOrLikeChain> {
let is_right_empty_object = logical
.right()
Expand All @@ -651,7 +655,12 @@ impl LogicalOrLikeChain {
.as_js_object_expression()?
.is_empty();

if !is_right_empty_object {
let chain_not_allowed = matches!(
logical.left().ok()?.omit_parentheses(),
JsAnyExpression::JsNewExpression(_)
);

if !is_right_empty_object || chain_not_allowed {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ foo[12n] && foo[123n].baz;
foo[/\w+/] && foo[/ab+c/].baz;

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

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


0 comments on commit f8fc203

Please sign in to comment.