Fix add-required-import with multi-line offsets (#2946)

This commit is contained in:
Charlie Marsh 2023-02-15 22:24:55 -05:00 committed by GitHub
parent b6587e51ee
commit 036380e6a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1,4 @@
"""a
b"""
# b
import os

View file

@ -110,7 +110,7 @@ pub fn find_splice_location(body: &[Stmt], locator: &Locator) -> Location {
// Find the first token that isn't a comment or whitespace. // Find the first token that isn't a comment or whitespace.
let contents = locator.skip(splice); let contents = locator.skip(splice);
for (.., tok, end) in lexer::make_tokenizer(contents).flatten() { for (.., tok, end) in lexer::make_tokenizer_located(contents, splice).flatten() {
if matches!(tok, Tok::Comment(..) | Tok::Newline) { if matches!(tok, Tok::Comment(..) | Tok::Newline) {
splice = end; splice = end;
} else { } else {

View file

@ -620,6 +620,7 @@ mod tests {
#[test_case(Path::new("docstring.py"))] #[test_case(Path::new("docstring.py"))]
#[test_case(Path::new("docstring_only.py"))] #[test_case(Path::new("docstring_only.py"))]
#[test_case(Path::new("multiline_docstring.py"))]
#[test_case(Path::new("empty.py"))] #[test_case(Path::new("empty.py"))]
fn required_import(path: &Path) -> Result<()> { fn required_import(path: &Path) -> Result<()> {
let snapshot = format!("required_import_{}", path.to_string_lossy()); let snapshot = format!("required_import_{}", path.to_string_lossy());

View file

@ -0,0 +1,24 @@
---
source: crates/ruff/src/rules/isort/mod.rs
expression: diagnostics
---
- kind:
MissingRequiredImport: from __future__ import annotations
location:
row: 1
column: 0
end_location:
row: 1
column: 0
fix:
content:
- ""
- from __future__ import annotations
location:
row: 3
column: 3
end_location:
row: 3
column: 3
parent: ~