mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Fix add-required-import with multi-line offsets (#2946)
This commit is contained in:
parent
b6587e51ee
commit
036380e6a8
4 changed files with 30 additions and 1 deletions
4
crates/ruff/resources/test/fixtures/isort/required_imports/multiline_docstring.py
vendored
Normal file
4
crates/ruff/resources/test/fixtures/isort/required_imports/multiline_docstring.py
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
"""a
|
||||||
|
b"""
|
||||||
|
# b
|
||||||
|
import os
|
|
@ -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 {
|
||||||
|
|
|
@ -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());
|
||||||
|
|
|
@ -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: ~
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue