feat(rome_js_analyze): Suggested fix for Conditional Assignment
MODIFY
crates/rome_js_analyze/src/analyzers/nursery/no_conditional_assignment.rs
crates/rome_js_analyze/src/analyzers/nursery/no_conditional_assignment.rs
use rome_analyze::context::RuleContext;
-use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
+use rome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
+use rome_diagnostics::Applicability;
+use rome_js_factory::make;
use rome_js_syntax::*;
-use rome_rowan::{declare_node_union, AstNode};
+use rome_rowan::{declare_node_union, AstNode, BatchMutationExt};
+
+use crate::JsRuleAction;
declare_rule! {
/// Disallow assignment operators in conditional expressions.
@@ -79,6 +83,23 @@ impl Rule for NoConditionalAssignment {
},
))
}
+
+ fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<JsRuleAction> {
+ let op = state.operator().ok()?;
+ if let JsAssignmentOperator::Assign = op {
+ let mut mutation = ctx.root().begin();
+ let token = state.operator_token().ok()?;
+ mutation.replace_token(token, make::token(JsSyntaxKind::EQ3));
+ Some(JsRuleAction {
+ mutation,
+ applicability: Applicability::MaybeIncorrect,
+ category: ActionCategory::QuickFix,
+ message: markup!("Did you mean '==='?").to_owned(),
+ })
+ } else {
+ None
+ }
+ }
}
fn into_js_assignment(expr: JsAnyExpression) -> Option<JsAssignmentExpression> {

MODIFY
crates/rome_js_analyze/tests/specs/nursery/noConditionalAssignment/invalid.js
crates/rome_js_analyze/tests/specs/nursery/noConditionalAssignment/invalid.js
@@ -18,4 +18,5 @@ for(; (x = y); ) { }
var x; var b = (x = 0) ? 1 : 0;
var x; var b = x && (y = 0) ? 1 : 0;
(((3496.29)).bkufyydt = 2e308) ? foo : bar;
-
\ No newline at end of file
+if (a += b) {}
+while ((c *= d)) {}
\ No newline at end of file

feat(rome_js_analyze): Suggested fix for Conditional Assignment (#3771)