From 9dfba0521764a70d88a573277e44542fef46d2aa Mon Sep 17 00:00:00 2001 From: Dhiraj Arun Date: Mon, 27 Feb 2023 20:46:53 +0530 Subject: [PATCH] refactor(rome_js_analyze): use_simple_number_keys --- .../nursery/use_simple_number_keys.rs | 87 ++++++++++++------- .../specs/nursery/useSimpleNumberKeys.js.snap | 4 +- 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/crates/rome_js_analyze/src/analyzers/nursery/use_simple_number_keys.rs b/crates/rome_js_analyze/src/analyzers/nursery/use_simple_number_keys.rs index 431c6fdc690..26289ddffa2 100644 --- a/crates/rome_js_analyze/src/analyzers/nursery/use_simple_number_keys.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery/use_simple_number_keys.rs @@ -251,9 +251,18 @@ impl NumberLiteral { } } +enum WrongNumberLiteralName { + Binary, + Hexadecimal, + Octal, + BigInt, + WithUnderscore, +} +pub struct RuleState(WrongNumberLiteralName, NumberLiteral); + impl Rule for UseSimpleNumberKeys { type Query = Ast; - type State = NumberLiteral; + type State = RuleState; type Signals = Vec; type Options = (); @@ -268,16 +277,28 @@ impl Rule for UseSimpleNumberKeys { .filter_map(|member| NumberLiteral::try_from(member).ok()) { match number_literal { - NumberLiteral::Decimal { big_int: true, .. } - | NumberLiteral::Decimal { - underscore: true, .. - } => signals.push(number_literal), + NumberLiteral::Decimal { big_int: true, .. } => { + signals.push(RuleState(WrongNumberLiteralName::BigInt, number_literal)) + } NumberLiteral::FloatingPoint { underscore: true, .. - } => signals.push(number_literal), - NumberLiteral::Binary { .. } => signals.push(number_literal), - NumberLiteral::Hexadecimal { .. } => signals.push(number_literal), - NumberLiteral::Octal { .. } => signals.push(number_literal), + } + | NumberLiteral::Decimal { + underscore: true, .. + } => signals.push(RuleState( + WrongNumberLiteralName::WithUnderscore, + number_literal, + )), + NumberLiteral::Binary { .. } => { + signals.push(RuleState(WrongNumberLiteralName::Binary, number_literal)) + } + NumberLiteral::Hexadecimal { .. } => signals.push(RuleState( + WrongNumberLiteralName::Hexadecimal, + number_literal, + )), + NumberLiteral::Octal { .. } => { + signals.push(RuleState(WrongNumberLiteralName::Octal, number_literal)) + } _ => (), } } @@ -287,43 +308,43 @@ impl Rule for UseSimpleNumberKeys { fn diagnostic( _ctx: &RuleContext, - number_literal: &Self::State, + RuleState(reason, literal): &Self::State, ) -> Option { - let title = match number_literal { - NumberLiteral::Decimal { big_int: true, .. } => "Bigint is not allowed here.", - NumberLiteral::Decimal { - underscore: true, .. - } => "Number literal with underscore is not allowed here.", - NumberLiteral::FloatingPoint { - underscore: true, .. - } => "Number literal with underscore is not allowed.", - NumberLiteral::Binary { .. } => "Binary number literal in is not allowed here.", - NumberLiteral::Hexadecimal { .. } => "Hexadecimal number literal is not allowed here.", - NumberLiteral::Octal { .. } => "Octal number literal is not allowed here.", - _ => "", + let title = match reason { + WrongNumberLiteralName::BigInt => "Bigint is not allowed here.", + WrongNumberLiteralName::WithUnderscore => { + "Number literal with underscore is not allowed here." + } + WrongNumberLiteralName::Binary => "Binary number literal in is not allowed here.", + WrongNumberLiteralName::Hexadecimal => { + "Hexadecimal number literal is not allowed here." + } + WrongNumberLiteralName::Octal => "Octal number literal is not allowed here.", }; - let diagnostic = - RuleDiagnostic::new(rule_category!(), number_literal.range(), title.to_string()); + let diagnostic = RuleDiagnostic::new(rule_category!(), literal.range(), title.to_string()); Some(diagnostic) } - fn action(ctx: &RuleContext, number_literal: &Self::State) -> Option { + fn action( + ctx: &RuleContext, + RuleState(reason, literal): &Self::State, + ) -> Option { let mut mutation = ctx.root().begin(); - let node = number_literal.node(); + let node = literal.node(); let token = node.value().ok()?; - let message = match number_literal { - NumberLiteral::Binary { .. } - | NumberLiteral::Octal { .. } - | NumberLiteral::Hexadecimal { .. } => { - let text = number_literal.to_base_ten()?; + let message = match reason { + WrongNumberLiteralName::Binary + | WrongNumberLiteralName::Octal + | WrongNumberLiteralName::Hexadecimal => { + let text = literal.to_base_ten()?; mutation.replace_token(token, make::js_number_literal(text)); markup! ("Replace "{ node.to_string() } " with "{text.to_string()}).to_owned() } - NumberLiteral::FloatingPoint { .. } | NumberLiteral::Decimal { .. } => { - let text = number_literal.value(); + WrongNumberLiteralName::WithUnderscore | WrongNumberLiteralName::BigInt => { + let text = literal.value(); mutation.replace_token(token, make::js_number_literal(text)); markup! ("Replace "{ node.to_string() } " with "{text}).to_owned() } diff --git a/crates/rome_js_analyze/tests/specs/nursery/useSimpleNumberKeys.js.snap b/crates/rome_js_analyze/tests/specs/nursery/useSimpleNumberKeys.js.snap index 6e7e47dcc1c..73cde74400e 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/useSimpleNumberKeys.js.snap +++ b/crates/rome_js_analyze/tests/specs/nursery/useSimpleNumberKeys.js.snap @@ -170,7 +170,7 @@ useSimpleNumberKeys.js:14:4 lint/nursery/useSimpleNumberKeys FIXABLE ━━━ ``` useSimpleNumberKeys.js:15:4 lint/nursery/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Number literal with underscore is not allowed. + ! Number literal with underscore is not allowed here. 13 │ ({ 0o1: 1 }); 14 │ ({ 1_0: 1 }); @@ -194,7 +194,7 @@ useSimpleNumberKeys.js:15:4 lint/nursery/useSimpleNumberKeys FIXABLE ━━━ ``` useSimpleNumberKeys.js:16:4 lint/nursery/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Number literal with underscore is not allowed. + ! Number literal with underscore is not allowed here. 14 │ ({ 1_0: 1 }); 15 │ ({ 0.1e1_2: "ed" });