-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update ISC001
, ISC002
to check in f-strings
#7515
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,7 @@ pub(crate) fn check_tokens( | |
tokens, | ||
&settings.flake8_implicit_str_concat, | ||
locator, | ||
indexer, | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
use itertools::Itertools; | ||
use ruff_python_parser::lexer::LexResult; | ||
use ruff_python_parser::Tok; | ||
use ruff_text_size::{Ranged, TextRange}; | ||
|
||
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::str::{leading_quote, trailing_quote}; | ||
use ruff_python_index::Indexer; | ||
use ruff_source_file::Locator; | ||
|
||
use crate::rules::flake8_implicit_str_concat::settings::Settings; | ||
|
@@ -94,6 +96,7 @@ pub(crate) fn implicit( | |
tokens: &[LexResult], | ||
settings: &Settings, | ||
locator: &Locator, | ||
indexer: &Indexer, | ||
) { | ||
for ((a_tok, a_range), (b_tok, b_range)) in tokens | ||
.iter() | ||
|
@@ -103,24 +106,39 @@ pub(crate) fn implicit( | |
}) | ||
.tuple_windows() | ||
{ | ||
if a_tok.is_string() && b_tok.is_string() { | ||
if locator.contains_line_break(TextRange::new(a_range.end(), b_range.start())) { | ||
diagnostics.push(Diagnostic::new( | ||
MultiLineImplicitStringConcatenation, | ||
TextRange::new(a_range.start(), b_range.end()), | ||
)); | ||
} else { | ||
let mut diagnostic = Diagnostic::new( | ||
SingleLineImplicitStringConcatenation, | ||
TextRange::new(a_range.start(), b_range.end()), | ||
); | ||
|
||
if let Some(fix) = concatenate_strings(*a_range, *b_range, locator) { | ||
diagnostic.set_fix(fix); | ||
} | ||
|
||
diagnostics.push(diagnostic); | ||
}; | ||
let (a_range, b_range) = match (a_tok, b_tok) { | ||
(Tok::String { .. }, Tok::String { .. }) => (*a_range, *b_range), | ||
(Tok::String { .. }, Tok::FStringStart) => ( | ||
*a_range, | ||
indexer.fstring_ranges().innermost(b_range.start()).unwrap(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the use of
But I guess it's fine, considering that implicit concatenated strings are somewhat rare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. This could be solved with the introduction of a new |
||
), | ||
(Tok::FStringEnd, Tok::String { .. }) => ( | ||
indexer.fstring_ranges().innermost(a_range.start()).unwrap(), | ||
*b_range, | ||
), | ||
(Tok::FStringEnd, Tok::FStringStart) => ( | ||
indexer.fstring_ranges().innermost(a_range.start()).unwrap(), | ||
indexer.fstring_ranges().innermost(b_range.start()).unwrap(), | ||
), | ||
_ => continue, | ||
}; | ||
|
||
if locator.contains_line_break(TextRange::new(a_range.end(), b_range.start())) { | ||
diagnostics.push(Diagnostic::new( | ||
MultiLineImplicitStringConcatenation, | ||
TextRange::new(a_range.start(), b_range.end()), | ||
)); | ||
} else { | ||
let mut diagnostic = Diagnostic::new( | ||
SingleLineImplicitStringConcatenation, | ||
TextRange::new(a_range.start(), b_range.end()), | ||
); | ||
|
||
if let Some(fix) = concatenate_strings(a_range, b_range, locator) { | ||
diagnostic.set_fix(fix); | ||
} | ||
|
||
diagnostics.push(diagnostic); | ||
}; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't detect this in earlier version but with the granularity of the tokens, we can detect this as well.