Skip to content

Commit

Permalink
Split add_noqa process into distinctive edit generation and edit ap…
Browse files Browse the repository at this point in the history
…plication stages (#11265)

## Summary

`--add-noqa` now runs in two stages: first, the linter finds all
diagnostics that need noqa comments and generate edits on a per-line
basis. Second, these edits are applied, in order, to the document.

A public-facing function, `generate_noqa_edits`, has also been
introduced, which returns noqa edits generated on a per-diagnostic
basis. This will be used by `ruff server` for noqa comment quick-fixes.

## Test Plan

Unit tests have been updated.
  • Loading branch information
snowsignal committed May 10, 2024
1 parent 0726e82 commit 890cc32
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 71 deletions.
65 changes: 65 additions & 0 deletions crates/ruff/tests/lint.rs
Expand Up @@ -1553,3 +1553,68 @@ def unused(x): # noqa: ANN001, ARG001, D103

Ok(())
}

#[test]
fn add_noqa_multiline_comment() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
[lint]
select = ["UP031"]
"#,
)?;

let test_path = tempdir.path().join("noqa.py");

fs::write(
&test_path,
r#"
print(
"""First line
second line
third line
%s"""
% name
)
"#,
)?;

insta::with_settings!({
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
}, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.current_dir(tempdir.path())
.args(STDIN_BASE_OPTIONS)
.args(["--config", &ruff_toml.file_name().unwrap().to_string_lossy()])
.arg(&test_path)
.arg("--preview")
.args(["--add-noqa"])
.arg("-")
.pass_stdin(r#"
"#), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Added 1 noqa directive.
"###);
});

let test_code = std::fs::read_to_string(&test_path).expect("should read test file");

insta::assert_snapshot!(test_code, @r###"
print(
"""First line
second line
third line
%s""" # noqa: UP031
% name
)
"###);

Ok(())
}
1 change: 1 addition & 0 deletions crates/ruff_linter/src/lib.rs
Expand Up @@ -5,6 +5,7 @@
//!
//! [Ruff]: https://github.com/astral-sh/ruff

pub use noqa::generate_noqa_edits;
#[cfg(feature = "clap")]
pub use registry::clap_completion::RuleParser;
#[cfg(feature = "clap")]
Expand Down

0 comments on commit 890cc32

Please sign in to comment.