Skip to content
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

Use comment_ranges for isort directive extraction #9414

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions crates/ruff_linter/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn extract_directives(
NoqaMapping::default()
},
isort: if flags.intersects(Flags::ISORT) {
extract_isort_directives(lxr, locator)
extract_isort_directives(locator, indexer)
} else {
IsortDirectives::default()
},
Expand Down Expand Up @@ -215,15 +215,13 @@ fn extract_noqa_line_for(lxr: &[LexResult], locator: &Locator, indexer: &Indexer
}

/// Extract a set of ranges over which to disable isort.
fn extract_isort_directives(lxr: &[LexResult], locator: &Locator) -> IsortDirectives {
fn extract_isort_directives(locator: &Locator, indexer: &Indexer) -> IsortDirectives {
let mut exclusions: Vec<TextRange> = Vec::default();
let mut splits: Vec<TextSize> = Vec::default();
let mut off: Option<TextSize> = None;

for &(ref tok, range) in lxr.iter().flatten() {
let Tok::Comment(comment_text) = tok else {
continue;
};
for range in indexer.comment_ranges() {
let comment_text = locator.slice(range);

// `isort` allows for `# isort: skip` and `# isort: skip_file` to include or
// omit a space after the colon. The remaining action comments are
Expand Down Expand Up @@ -592,8 +590,10 @@ assert foo, \
y = 2
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).exclusions,
extract_isort_directives(&locator, &indexer).exclusions,
Vec::default()
);

Expand All @@ -603,8 +603,10 @@ y = 2
# isort: on
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).exclusions,
extract_isort_directives(&locator, &indexer).exclusions,
Vec::from_iter([TextRange::new(TextSize::from(0), TextSize::from(25))])
);

Expand All @@ -616,8 +618,10 @@ y = 2
z = x + 1
# isort: on";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).exclusions,
extract_isort_directives(&locator, &indexer).exclusions,
Vec::from_iter([TextRange::new(TextSize::from(0), TextSize::from(38))])
);

Expand All @@ -626,8 +630,10 @@ x = 1
y = 2
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).exclusions,
extract_isort_directives(&locator, &indexer).exclusions,
Vec::from_iter([TextRange::at(TextSize::from(0), contents.text_len())])
);

Expand All @@ -636,8 +642,10 @@ x = 1
y = 2
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).exclusions,
extract_isort_directives(&locator, &indexer).exclusions,
Vec::default()
);

Expand All @@ -648,8 +656,10 @@ y = 2
# isort: skip_file
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).exclusions,
extract_isort_directives(&locator, &indexer).exclusions,
Vec::default()
);
}
Expand All @@ -660,8 +670,10 @@ z = x + 1";
y = 2
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).splits,
extract_isort_directives(&locator, &indexer).splits,
Vec::new()
);

Expand All @@ -670,17 +682,21 @@ y = 2
# isort: split
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).splits,
extract_isort_directives(&locator, &indexer).splits,
vec![TextSize::from(12)]
);

let contents = "x = 1
y = 2 # isort: split
z = x + 1";
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let indexer = Indexer::from_tokens(&lxr, &locator);
assert_eq!(
extract_isort_directives(&lxr, &Locator::new(contents)).splits,
extract_isort_directives(&locator, &indexer).splits,
vec![TextSize::from(13)]
);
}
Expand Down
Loading