diff --git a/crates/rome_js_analyze/src/analyzers/nursery/no_self_assignment.rs b/crates/rome_js_analyze/src/analyzers/nursery/no_self_assignment.rs index a7d8f682898..7282f0ad95b 100644 --- a/crates/rome_js_analyze/src/analyzers/nursery/no_self_assignment.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery/no_self_assignment.rs @@ -184,7 +184,9 @@ impl SameIdentifiers { Self::next_static_expression(left, right) } AnyAssignmentLike::None | AnyAssignmentLike::Identifiers { .. } => { - Some(self.current_assignment_like.clone()) + let new_assignment = self.current_assignment_like.clone(); + self.current_assignment_like = AnyAssignmentLike::None; + Some(new_assignment) } } } @@ -333,11 +335,6 @@ impl Iterator for SameIdentifiers { loop { let new_assignment_like = self.next_assignment_like()?; - // if the queue is empty, we set the current assignment to `None`, - // so the next iteration will stop - if self.assignment_queue.is_empty() { - self.current_assignment_like = AnyAssignmentLike::None; - } match new_assignment_like { // if we are here, it's plausible that we consumed the current iterator and we have to // resume the previous one diff --git a/crates/rome_js_analyze/src/lib.rs b/crates/rome_js_analyze/src/lib.rs index b0dea066c8c..6a55132660d 100644 --- a/crates/rome_js_analyze/src/lib.rs +++ b/crates/rome_js_analyze/src/lib.rs @@ -173,7 +173,7 @@ mod tests { String::from_utf8(buffer).unwrap() } - const SOURCE: &str = r#"a[b] = a[b]"#; + const SOURCE: &str = r#"a = a"#; let parsed = parse(SOURCE, SourceType::jsx()); diff --git a/crates/rome_js_analyze/tests/specs/nursery/noSelfAssignment/invalid.js.snap b/crates/rome_js_analyze/tests/specs/nursery/noSelfAssignment/invalid.js.snap index cf5b53b4912..52f0bd3b5e3 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/noSelfAssignment/invalid.js.snap +++ b/crates/rome_js_analyze/tests/specs/nursery/noSelfAssignment/invalid.js.snap @@ -93,6 +93,30 @@ invalid.js:3:11 lint/nursery/noSelfAssignment ━━━━━━━━━━━ 5 │ ({a, b} = {a, b}); +``` + +``` +invalid.js:3:14 lint/nursery/noSelfAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! b is assigned to itself. + + 1 │ a = a; + 2 │ [a] = [a]; + > 3 │ [a, b] = [a, b]; + │ ^ + 4 │ [a, ...b] = [a, ...b]; + 5 │ ({a, b} = {a, b}); + + i This is where is assigned. + + 1 │ a = a; + 2 │ [a] = [a]; + > 3 │ [a, b] = [a, b]; + │ ^ + 4 │ [a, ...b] = [a, ...b]; + 5 │ ({a, b} = {a, b}); + + ``` ``` @@ -141,6 +165,30 @@ invalid.js:5:12 lint/nursery/noSelfAssignment ━━━━━━━━━━━ 7 │ [{a}, {b}] = [{a}, {b}]; +``` + +``` +invalid.js:5:15 lint/nursery/noSelfAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! b is assigned to itself. + + 3 │ [a, b] = [a, b]; + 4 │ [a, ...b] = [a, ...b]; + > 5 │ ({a, b} = {a, b}); + │ ^ + 6 │ [[a], [b]] = [[a], [b]]; + 7 │ [{a}, {b}] = [{a}, {b}]; + + i This is where is assigned. + + 3 │ [a, b] = [a, b]; + 4 │ [a, ...b] = [a, ...b]; + > 5 │ ({a, b} = {a, b}); + │ ^ + 6 │ [[a], [b]] = [[a], [b]]; + 7 │ [{a}, {b}] = [{a}, {b}]; + + ``` ```