-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[pycodestyle
] Implement blank-line-at-end-of-file
(W391
)
#10243
Merged
charliermarsh
merged 14 commits into
astral-sh:main
from
augustelalande:too_many_newlines
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1d3b602
implement too_many_newlines_at_end_of_file
augustelalande 065f643
add rule description
augustelalande fed92fd
add test cases with different lines endings
augustelalande 82adae1
run tests
augustelalande 5ba8358
linting
augustelalande 67753eb
address clippy issue
augustelalande ef73979
seems to be a KNOWN_PARSE_ERRORS
augustelalande 7ff02c6
Change W391 to be token based
augustelalande 60d4ce5
linting
augustelalande 6e2dc19
fix bug with windows style newline
augustelalande ef3c813
fix bug not detecting single newline
augustelalande 815087a
fix bug with files ending in comments
augustelalande d13c6b9
Merge branch 'main' into too_many_newlines
charliermarsh 2d4e2fe
Tweak message; remove locator
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
crates/ruff_linter/resources/test/fixtures/pycodestyle/W391_0.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Unix style | ||
def foo() -> None: | ||
pass | ||
|
||
|
||
def bar() -> None: | ||
pass | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
foo() | ||
bar() | ||
|
13 changes: 13 additions & 0 deletions
13
crates/ruff_linter/resources/test/fixtures/pycodestyle/W391_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Unix style | ||
def foo() -> None: | ||
pass | ||
|
||
|
||
def bar() -> None: | ||
pass | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
foo() | ||
bar() |
17 changes: 17 additions & 0 deletions
17
crates/ruff_linter/resources/test/fixtures/pycodestyle/W391_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Windows style | ||
def foo() -> None: | ||
pass | ||
|
||
|
||
def bar() -> None: | ||
pass | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
foo() | ||
bar() | ||
|
||
|
||
|
||
|
13 changes: 13 additions & 0 deletions
13
crates/ruff_linter/resources/test/fixtures/pycodestyle/W391_3.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Windows style | ||
def foo() -> None: | ||
pass | ||
|
||
|
||
def bar() -> None: | ||
pass | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
foo() | ||
bar() |
5 changes: 5 additions & 0 deletions
5
crates/ruff_linter/resources/test/fixtures/pycodestyle/W391_4.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This is fine | ||
def foo(): | ||
pass | ||
|
||
# Some comment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
crates/ruff_linter/src/rules/pycodestyle/rules/too_many_newlines_at_end_of_file.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_parser::lexer::LexResult; | ||
use ruff_python_parser::Tok; | ||
use ruff_text_size::{TextRange, TextSize}; | ||
|
||
/// ## What it does | ||
/// Checks for files with multiple trailing blank lines. | ||
/// | ||
/// ## Why is this bad? | ||
/// Trailing blank lines in a file are superfluous. | ||
/// | ||
/// However, the last line of the file should end with a newline. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// spam(1)\n\n\n | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// spam(1)\n | ||
/// ``` | ||
#[violation] | ||
pub struct TooManyNewlinesAtEndOfFile { | ||
num_trailing_newlines: u32, | ||
} | ||
|
||
impl AlwaysFixableViolation for TooManyNewlinesAtEndOfFile { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let TooManyNewlinesAtEndOfFile { | ||
num_trailing_newlines, | ||
} = self; | ||
|
||
// We expect a single trailing newline; so two trailing newlines is one too many, three | ||
// trailing newlines is two too many, etc. | ||
if *num_trailing_newlines > 2 { | ||
format!("Too many newlines at end of file") | ||
} else { | ||
format!("Extra newline at end of file") | ||
} | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
let TooManyNewlinesAtEndOfFile { | ||
num_trailing_newlines, | ||
} = self; | ||
if *num_trailing_newlines > 2 { | ||
"Remove trailing newlines".to_string() | ||
} else { | ||
"Remove trailing newline".to_string() | ||
} | ||
} | ||
} | ||
|
||
/// W391 | ||
pub(crate) fn too_many_newlines_at_end_of_file( | ||
diagnostics: &mut Vec<Diagnostic>, | ||
lxr: &[LexResult], | ||
) { | ||
let mut num_trailing_newlines = 0u32; | ||
let mut start: Option<TextSize> = None; | ||
let mut end: Option<TextSize> = None; | ||
|
||
// Count the number of trailing newlines. | ||
for (tok, range) in lxr.iter().rev().flatten() { | ||
match tok { | ||
Tok::NonLogicalNewline | Tok::Newline => { | ||
if num_trailing_newlines == 0 { | ||
end = Some(range.end()); | ||
} | ||
start = Some(range.end()); | ||
num_trailing_newlines += 1; | ||
} | ||
Tok::Dedent => continue, | ||
_ => { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if num_trailing_newlines == 0 || num_trailing_newlines == 1 { | ||
return; | ||
} | ||
|
||
let range = match (start, end) { | ||
(Some(start), Some(end)) => TextRange::new(start, end), | ||
_ => return, | ||
}; | ||
let mut diagnostic = Diagnostic::new( | ||
TooManyNewlinesAtEndOfFile { | ||
num_trailing_newlines, | ||
}, | ||
range, | ||
); | ||
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(range))); | ||
diagnostics.push(diagnostic); | ||
} |
17 changes: 17 additions & 0 deletions
17
...ycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__W391_W391_0.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
W391_0.py:14:1: W391 [*] Extra newline at end of file | ||
| | ||
12 | foo() | ||
13 | bar() | ||
14 | | ||
| ^ W391 | ||
| | ||
= help: Remove trailing newline | ||
|
||
ℹ Safe fix | ||
11 11 | if __name__ == '__main__': | ||
12 12 | foo() | ||
13 13 | bar() | ||
14 |- |
4 changes: 4 additions & 0 deletions
4
...ycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__W391_W391_1.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
|
22 changes: 22 additions & 0 deletions
22
...ycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__W391_W391_2.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
W391_2.py:14:1: W391 [*] Too many newlines at end of file | ||
| | ||
12 | foo() | ||
13 | bar() | ||
14 | / | ||
15 | | | ||
16 | | | ||
17 | | | ||
| | ||
= help: Remove trailing newlines | ||
|
||
ℹ Safe fix | ||
11 11 | if __name__ == '__main__': | ||
12 12 | foo() | ||
13 13 | bar() | ||
14 |- | ||
15 |- | ||
16 |- | ||
17 |- |
4 changes: 4 additions & 0 deletions
4
...ycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__W391_W391_3.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
|
4 changes: 4 additions & 0 deletions
4
...ycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__W391_W391_4.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the
Locator
. I don't think we need to guard on empty files here -- that's just for the "no trailing newline" check, since empty files would be a false positive.