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

Add autofix for W292 [NoNewLineAtEndOfFile] #1354

Merged
merged 10 commits into from Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -540,7 +540,7 @@ For more, see [pycodestyle](https://pypi.org/project/pycodestyle/2.9.1/) on PyPI
| E743 | AmbiguousFunctionName | Ambiguous function name: `...` | |
| E902 | IOError | IOError: `...` | |
| E999 | SyntaxError | SyntaxError: `...` | |
| W292 | NoNewLineAtEndOfFile | No newline at end of file | |
| W292 | NoNewLineAtEndOfFile | No newline at end of file | 🛠 |
| W605 | InvalidEscapeSequence | Invalid escape sequence: '\c' | |

### mccabe (C90)
Expand Down
2 changes: 1 addition & 1 deletion resources/test/fixtures/pycodestyle/W292_2.py
@@ -1,2 +1,2 @@
def fn() -> None:
pass
print("Newline present (no W292)")
Empty file.
1 change: 1 addition & 0 deletions resources/test/fixtures/pycodestyle/W292_4.py
@@ -0,0 +1 @@

6 changes: 5 additions & 1 deletion src/checkers/lines.rs
Expand Up @@ -55,7 +55,11 @@ pub fn check_lines(
}

if enforce_no_newline_at_end_of_file {
if let Some(check) = no_newline_at_end_of_file(contents) {
if let Some(check) = no_newline_at_end_of_file(
contents,
matches!(autofix, flags::Autofix::Enabled)
&& settings.fixable.contains(&CheckCode::W292),
) {
checks.push(check);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/checks.rs
Expand Up @@ -2983,6 +2983,7 @@ impl CheckKind {
| CheckKind::NoBlankLineBeforeClass(..)
| CheckKind::NoBlankLineBeforeFunction(..)
| CheckKind::NoBlankLinesBetweenHeaderAndContent(..)
| CheckKind::NoNewLineAtEndOfFile
| CheckKind::NoOverIndentation
| CheckKind::NoSurroundingWhitespace
| CheckKind::NoUnderIndentation
Expand Down
17 changes: 12 additions & 5 deletions src/pycodestyle/checks.rs
Expand Up @@ -5,6 +5,7 @@ use rustpython_ast::{Located, Location, Stmt, StmtKind};
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};

use crate::ast::types::Range;
use crate::autofix::Fix;
use crate::checks::{Check, CheckKind};
use crate::source_code_locator::SourceCodeLocator;

Expand Down Expand Up @@ -134,18 +135,24 @@ pub fn ambiguous_function_name(name: &str, location: Range) -> Option<Check> {
}

/// W292
pub fn no_newline_at_end_of_file(contents: &str) -> Option<Check> {
pub fn no_newline_at_end_of_file(contents: &str, autofix: bool) -> Option<Check> {
if !contents.ends_with('\n') {
// Note: if `lines.last()` is `None`, then `contents` is empty (and so we don't
// want to raise W292 anyway).
if let Some(line) = contents.lines().last() {
return Some(Check::new(
// Both locations are at the end of the file (and thus the same).
let location = Location::new(contents.lines().count(), line.len());
let mut check = Check::new(
CheckKind::NoNewLineAtEndOfFile,
Range {
location: Location::new(contents.lines().count(), line.len() + 1),
end_location: Location::new(contents.lines().count(), line.len() + 1),
location,
end_location: location,
},
));
);
if autofix {
check.amend(Fix::insertion("\n".to_string(), location));
}
return Some(check);
}
}
None
Expand Down
2 changes: 2 additions & 0 deletions src/pycodestyle/mod.rs
Expand Up @@ -31,6 +31,8 @@ mod tests {
#[test_case(CheckCode::W292, Path::new("W292_0.py"))]
#[test_case(CheckCode::W292, Path::new("W292_1.py"))]
#[test_case(CheckCode::W292, Path::new("W292_2.py"))]
#[test_case(CheckCode::W292, Path::new("W292_3.py"))]
#[test_case(CheckCode::W292, Path::new("W292_4.py"))]
#[test_case(CheckCode::W605, Path::new("W605_0.py"))]
#[test_case(CheckCode::W605, Path::new("W605_1.py"))]
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
Expand Down
Expand Up @@ -5,9 +5,16 @@ expression: checks
- kind: NoNewLineAtEndOfFile
location:
row: 2
column: 9
column: 8
end_location:
row: 2
column: 9
fix: ~
column: 8
fix:
content: "\n"
location:
row: 2
column: 8
end_location:
row: 2
column: 8

@@ -0,0 +1,6 @@
---
source: src/pycodestyle/mod.rs
expression: checks
---
[]

@@ -0,0 +1,20 @@
---
source: src/pycodestyle/mod.rs
expression: checks
---
- kind: NoNewLineAtEndOfFile
location:
row: 1
column: 1
end_location:
row: 1
column: 1
fix:
content: "\n"
location:
row: 1
column: 1
end_location:
row: 1
column: 1