diff --git a/crates/rome_js_analyze/src/semantic_analyzers/style/no_shouty_constants.rs b/crates/rome_js_analyze/src/semantic_analyzers/style/no_shouty_constants.rs index e0d72eb64c2..f9606635d8b 100644 --- a/crates/rome_js_analyze/src/semantic_analyzers/style/no_shouty_constants.rs +++ b/crates/rome_js_analyze/src/semantic_analyzers/style/no_shouty_constants.rs @@ -66,11 +66,12 @@ fn is_id_and_string_literal_inner_text_equal( .as_js_string_literal_expression()?; let literal_text = literal.inner_string_text().ok()?; + if id_text.len() != usize::from(literal_text.len()) { + return None; + } + for (from_id, from_literal) in id_text.chars().zip(literal_text.chars()) { - if from_id != from_literal { - return None; - } - if from_id.is_lowercase() || from_literal.is_lowercase() { + if from_id != from_literal || from_id.is_lowercase() { return None; } } diff --git a/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js b/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js index 983754058f8..415dd7fa511 100644 --- a/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js +++ b/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js @@ -26,4 +26,19 @@ export const d = { const D ="D"; export const e = { D -}; \ No newline at end of file +}; + +const Empty = ""; +export const E = Empty; + +const NotMatching = "DoesNotMatch"; +export const F = NotMatching; + +const matchingLowercase = "matchingLowercase"; +export const G = matchingLowercase; + +const AL = "ALPHA"; +export const H = AL; + +const ALPHA = "AL"; +export const I = ALPHA; diff --git a/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js.snap b/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js.snap index 2d72e58a160..d5e70d39265 100644 --- a/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js.snap +++ b/crates/rome_js_analyze/tests/specs/style/noShoutyConstants.js.snap @@ -34,6 +34,22 @@ const D ="D"; export const e = { D }; + +const Empty = ""; +export const E = Empty; + +const NotMatching = "DoesNotMatch"; +export const F = NotMatching; + +const matchingLowercase = "matchingLowercase"; +export const G = matchingLowercase; + +const AL = "ALPHA"; +export const H = AL; + +const ALPHA = "AL"; +export const I = ALPHA; + ``` # Diagnostics @@ -173,6 +189,7 @@ noShoutyConstants.js:26:7 lint/style/noShoutyConstants FIXABLE ━━━━━ > 28 │ D │ ^ 29 │ }; + 30 │ i You should avoid declaring constants with a string that's the same value as the variable name. It introduces a level of unnecessary @@ -189,6 +206,7 @@ noShoutyConstants.js:26:7 lint/style/noShoutyConstants FIXABLE ━━━━━ 26 │ + → 27 │ + → D:"D" 29 28 │ }; + 30 29 │ ```