Avoid B015,B018 for last expression in a cell (#8815)

## Summary

This PR updates `B015` and `B018` to ignore last top-level expressions
in each cell of a Jupyter Notebook.

Part of #8669

## Test Plan

Add test cases for both rules and update the snapshots.
This commit is contained in:
Dhruv Manilawala 2023-11-22 09:33:23 -06:00 committed by GitHub
parent 727e389cac
commit 5b726f70f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 454 additions and 1 deletions

View file

@ -1,7 +1,9 @@
use std::fmt;
use std::ops::{Deref, DerefMut};
use ruff_text_size::TextSize;
use itertools::Itertools;
use ruff_text_size::{TextRange, TextSize};
use crate::schema::{Cell, SourceValue};
@ -187,6 +189,17 @@ impl CellOffsets {
pub(crate) fn push(&mut self, offset: TextSize) {
self.0.push(offset);
}
/// Returns the range of the cell containing the given offset, if any.
pub fn containing_range(&self, offset: TextSize) -> Option<TextRange> {
self.iter().tuple_windows().find_map(|(start, end)| {
if *start <= offset && offset < *end {
Some(TextRange::new(*start, *end))
} else {
None
}
})
}
}
impl Deref for CellOffsets {