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

Commit

Permalink
feat(rome_js_analyze): noUnsafeOptionalChain, no-unsafe-optional-chai…
Browse files Browse the repository at this point in the history
…ning #3985
  • Loading branch information
denbezrukov committed Dec 29, 2022
1 parent f623775 commit eef31ac
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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."
},
Expand All @@ -320,12 +320,14 @@ impl QueryNode {
}
}

pub fn optional_chain_token(&self) -> Option<JsSyntaxToken> {
match self {
pub fn range(&self) -> Option<TextRange> {
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())
}
}

Expand Down Expand Up @@ -353,6 +355,7 @@ impl From<QueryNode> for RuleNode {
}
}

/// Only these variants of the union can be part of an unsafe optional chain.
declare_node_union! {
pub(crate) RuleNode =
JsLogicalExpression
Expand Down

0 comments on commit eef31ac

Please sign in to comment.