mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00
Move flake8-executable
rules out of physical lines checker (#6039)
## Summary These only need the token stream, and we always prefer token-based to physical line-based rules. There are a few other changes snuck in here: - Renaming the rule files to match the diagnostic names (likely an error). - The "leading whitespace before shebang" rule now works regardless of where the comment occurs (i.e., if the shebang is on the second line, and the first line is blank, we flag and remove that leading whitespace).
This commit is contained in:
parent
7f3797185c
commit
776d598738
22 changed files with 235 additions and 224 deletions
|
@ -1,15 +1,10 @@
|
|||
use ruff_python_trivia::{is_python_whitespace, Cursor};
|
||||
use ruff_text_size::{TextLen, TextSize};
|
||||
use std::ops::Deref;
|
||||
|
||||
use ruff_python_trivia::Cursor;
|
||||
|
||||
/// A shebang directive (e.g., `#!/usr/bin/env python3`).
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(crate) struct ShebangDirective<'a> {
|
||||
/// The offset of the directive contents (e.g., `/usr/bin/env python3`) from the start of the
|
||||
/// line.
|
||||
pub(crate) offset: TextSize,
|
||||
/// The contents of the directive (e.g., `"/usr/bin/env python3"`).
|
||||
pub(crate) contents: &'a str,
|
||||
}
|
||||
pub(crate) struct ShebangDirective<'a>(&'a str);
|
||||
|
||||
impl<'a> ShebangDirective<'a> {
|
||||
/// Parse a shebang directive from a line, or return `None` if the line does not contain a
|
||||
|
@ -17,9 +12,6 @@ impl<'a> ShebangDirective<'a> {
|
|||
pub(crate) fn try_extract(line: &'a str) -> Option<Self> {
|
||||
let mut cursor = Cursor::new(line);
|
||||
|
||||
// Trim whitespace.
|
||||
cursor.eat_while(is_python_whitespace);
|
||||
|
||||
// Trim the `#!` prefix.
|
||||
if !cursor.eat_char('#') {
|
||||
return None;
|
||||
|
@ -28,10 +20,15 @@ impl<'a> ShebangDirective<'a> {
|
|||
return None;
|
||||
}
|
||||
|
||||
Some(Self {
|
||||
offset: line.text_len() - cursor.text_len(),
|
||||
contents: cursor.chars().as_str(),
|
||||
})
|
||||
Some(Self(cursor.chars().as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ShebangDirective<'_> {
|
||||
type Target = str;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,6 +56,12 @@ mod tests {
|
|||
assert_debug_snapshot!(ShebangDirective::try_extract(source));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shebang_match_trailing_comment() {
|
||||
let source = "#!/usr/bin/env python # trailing comment";
|
||||
assert_debug_snapshot!(ShebangDirective::try_extract(source));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shebang_leading_space() {
|
||||
let source = " #!/usr/bin/env python";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue