diff --git a/crates/rome_js_analyze/src/analyzers/nursery/no_unsafe_optional_chaining.rs b/crates/rome_js_analyze/src/analyzers/nursery/no_unsafe_optional_chaining.rs index fad98efee932..d1309a18ce55 100644 --- a/crates/rome_js_analyze/src/analyzers/nursery/no_unsafe_optional_chaining.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery/no_unsafe_optional_chaining.rs @@ -143,7 +143,7 @@ impl Rule for NoUnsafeOptionalChaining { } RuleNode::JsCallExpression(expression) => { if expression.is_optional() { - // need to skip if we find another optional node because we handle it as a query in another iter + // The current optional chain is inside another optional chain which will also be processed by the rule so we can skip current optional chain // a?.b?.() return None; } @@ -159,7 +159,7 @@ impl Rule for NoUnsafeOptionalChaining { } RuleNode::JsStaticMemberExpression(expression) => { if expression.is_optional() { - // need to skip if we find another optional node because we handle it as a query in another iter + // The current optional chain is inside another optional chain which will also be processed by the rule so we can skip current optional chain // a?.b?.c return None; } @@ -175,7 +175,7 @@ impl Rule for NoUnsafeOptionalChaining { } RuleNode::JsComputedMemberExpression(expression) => { if expression.is_optional() { - // need to skip if we find another optional node because we handle it as a query in another iter + // The current optional chain is inside another optional chain which will also be processed by the rule so we can skip current optional chain // a?.[b]?.[c] return None; } @@ -294,7 +294,7 @@ impl Rule for NoUnsafeOptionalChaining { Some( RuleDiagnostic::new( rule_category!(), - query_node.optional_chain_token()?.text_trimmed_range(), + query_node.range(), markup! { "Unsafe usage of optional chaining." }, @@ -320,12 +320,14 @@ impl QueryNode { } } - pub fn optional_chain_token(&self) -> Option { - match self { + pub fn range(&self) -> Option { + let token = match self { QueryNode::JsCallExpression(expression) => expression.optional_chain_token(), QueryNode::JsStaticMemberExpression(expression) => expression.operator_token().ok(), QueryNode::JsComputedMemberExpression(expression) => expression.optional_chain_token(), - } + }; + + Some(token?.text_trimmed_range()) } } @@ -353,6 +355,7 @@ impl From for RuleNode { } } +/// Only these variants of the union can be part of an unsafe optional chain. declare_node_union! { pub(crate) RuleNode = JsLogicalExpression