mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
Track t-strings and f-strings for token-based rules and suppression comments (#20357)
Our token-based rules and `noqa` extraction used an `Indexer` that kept track of f-string ranges but not t-strings. We've updated the `Indexer` and downstream uses thereof to handle both f-strings and t-strings. Most of the diff is renaming and adding tests. Note that much of the "new" logic gets to be naive because the lexer has already ensured that f and t-string "starts" are paired with their respective "ends", even amidst nesting and so on. Finally: one could imagine wanting to know if a given interpolated string range corresponds to an f-string or a t-string, but I didn't find a place where we actually needed this. Closes #20310
This commit is contained in:
parent
ec863bcde7
commit
b6bd32d9dc
18 changed files with 519 additions and 89 deletions
|
@ -9,15 +9,17 @@ use ruff_python_trivia::{
|
|||
use ruff_source_file::LineRanges;
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
use crate::fstring_ranges::{FStringRanges, FStringRangesBuilder};
|
||||
use crate::interpolated_string_ranges::{
|
||||
InterpolatedStringRanges, InterpolatedStringRangesBuilder,
|
||||
};
|
||||
use crate::multiline_ranges::{MultilineRanges, MultilineRangesBuilder};
|
||||
|
||||
pub struct Indexer {
|
||||
/// Stores the start offset of continuation lines.
|
||||
continuation_lines: Vec<TextSize>,
|
||||
|
||||
/// The range of all f-string in the source document.
|
||||
fstring_ranges: FStringRanges,
|
||||
/// The range of all interpolated strings in the source document.
|
||||
interpolated_string_ranges: InterpolatedStringRanges,
|
||||
|
||||
/// The range of all multiline strings in the source document.
|
||||
multiline_ranges: MultilineRanges,
|
||||
|
@ -30,7 +32,7 @@ impl Indexer {
|
|||
pub fn from_tokens(tokens: &Tokens, source: &str) -> Self {
|
||||
assert!(TextSize::try_from(source.len()).is_ok());
|
||||
|
||||
let mut fstring_ranges_builder = FStringRangesBuilder::default();
|
||||
let mut interpolated_string_ranges_builder = InterpolatedStringRangesBuilder::default();
|
||||
let mut multiline_ranges_builder = MultilineRangesBuilder::default();
|
||||
let mut continuation_lines = Vec::new();
|
||||
let mut comment_ranges = Vec::new();
|
||||
|
@ -59,7 +61,7 @@ impl Indexer {
|
|||
}
|
||||
}
|
||||
|
||||
fstring_ranges_builder.visit_token(token);
|
||||
interpolated_string_ranges_builder.visit_token(token);
|
||||
multiline_ranges_builder.visit_token(token);
|
||||
|
||||
match token.kind() {
|
||||
|
@ -82,7 +84,7 @@ impl Indexer {
|
|||
|
||||
Self {
|
||||
continuation_lines,
|
||||
fstring_ranges: fstring_ranges_builder.finish(),
|
||||
interpolated_string_ranges: interpolated_string_ranges_builder.finish(),
|
||||
multiline_ranges: multiline_ranges_builder.finish(),
|
||||
comment_ranges: CommentRanges::new(comment_ranges),
|
||||
}
|
||||
|
@ -93,9 +95,9 @@ impl Indexer {
|
|||
&self.comment_ranges
|
||||
}
|
||||
|
||||
/// Returns the byte offset ranges of f-strings.
|
||||
pub const fn fstring_ranges(&self) -> &FStringRanges {
|
||||
&self.fstring_ranges
|
||||
/// Returns the byte offset ranges of interpolated strings.
|
||||
pub const fn interpolated_string_ranges(&self) -> &InterpolatedStringRanges {
|
||||
&self.interpolated_string_ranges
|
||||
}
|
||||
|
||||
/// Returns the byte offset ranges of multiline strings.
|
||||
|
@ -356,7 +358,7 @@ f"implicit " f"concatenation"
|
|||
.trim();
|
||||
assert_eq!(
|
||||
new_indexer(contents)
|
||||
.fstring_ranges()
|
||||
.interpolated_string_ranges()
|
||||
.values()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
|
@ -390,7 +392,7 @@ f-string"""}
|
|||
.trim();
|
||||
assert_eq!(
|
||||
new_indexer(contents)
|
||||
.fstring_ranges()
|
||||
.interpolated_string_ranges()
|
||||
.values()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
|
@ -504,11 +506,17 @@ the end"""
|
|||
),
|
||||
] {
|
||||
assert_eq!(
|
||||
indexer.fstring_ranges().innermost(offset).unwrap(),
|
||||
indexer
|
||||
.interpolated_string_ranges()
|
||||
.innermost(offset)
|
||||
.unwrap(),
|
||||
innermost_range
|
||||
);
|
||||
assert_eq!(
|
||||
indexer.fstring_ranges().outermost(offset).unwrap(),
|
||||
indexer
|
||||
.interpolated_string_ranges()
|
||||
.outermost(offset)
|
||||
.unwrap(),
|
||||
outermost_range
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue