Remove manual newline from autofix helpers (#2184)

This commit is contained in:
Charlie Marsh 2023-01-25 19:53:26 -05:00 committed by GitHub
parent 353857e2a5
commit 4190f1045e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 47 additions and 12 deletions

View File

@ -12,7 +12,7 @@ use crate::ast::whitespace::LinesWithTrailingNewline;
use crate::cst::helpers::compose_module_path;
use crate::cst::matchers::match_module;
use crate::fix::Fix;
use crate::source_code::{Indexer, Locator};
use crate::source_code::{Indexer, Locator, Stylist};
/// Determine if a body contains only a single statement, taking into account
/// deleted.
@ -157,6 +157,7 @@ pub fn delete_stmt(
deleted: &[&Stmt],
locator: &Locator,
indexer: &Indexer,
stylist: &Stylist,
) -> Result<Fix> {
if parent
.map(|parent| is_lone_child(stmt, parent, deleted))
@ -179,7 +180,11 @@ pub fn delete_stmt(
} else if helpers::preceded_by_continuation(stmt, indexer) {
if is_end_of_file(stmt, locator) && stmt.location.column() == 0 {
// Special-case: a file can't end in a continuation.
Fix::replacement("\n".to_string(), stmt.location, stmt.end_location.unwrap())
Fix::replacement(
stylist.line_ending().to_string(),
stmt.location,
stmt.end_location.unwrap(),
)
} else {
Fix::deletion(stmt.location, stmt.end_location.unwrap())
}
@ -200,6 +205,7 @@ pub fn remove_unused_imports<'a>(
deleted: &[&Stmt],
locator: &Locator,
indexer: &Indexer,
stylist: &Stylist,
) -> Result<Fix> {
let module_text = locator.slice_source_code_range(&Range::from_located(stmt));
let mut tree = match_module(module_text)?;
@ -237,7 +243,7 @@ pub fn remove_unused_imports<'a>(
if !found_star {
bail!("Expected \'*\' for unused import");
}
return delete_stmt(stmt, parent, deleted, locator, indexer);
return delete_stmt(stmt, parent, deleted, locator, indexer, stylist);
} else {
bail!("Expected: ImportNames::Aliases | ImportNames::Star");
}
@ -298,7 +304,7 @@ pub fn remove_unused_imports<'a>(
}
if aliases.is_empty() {
delete_stmt(stmt, parent, deleted, locator, indexer)
delete_stmt(stmt, parent, deleted, locator, indexer, stylist)
} else {
let mut state = CodegenState::default();
tree.codegen(&mut state);

View File

@ -4611,6 +4611,7 @@ impl<'a> Checker<'a> {
&deleted,
self.locator,
self.indexer,
self.stylist,
) {
Ok(fix) => {
if fix.content.is_empty() || fix.content == "pass" {

View File

@ -35,7 +35,14 @@ pub fn no_unnecessary_pass(checker: &mut Checker, body: &[Stmt]) {
Range::from_located(pass_stmt),
);
if checker.patch(&Rule::NoUnnecessaryPass) {
match delete_stmt(pass_stmt, None, &[], checker.locator, checker.indexer) {
match delete_stmt(
pass_stmt,
None,
&[],
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
diagnostic.amend(fix);
}
@ -94,7 +101,14 @@ pub fn dupe_class_field_definitions<'a, 'b>(
.map(std::convert::Into::into)
.collect();
let locator = checker.locator;
match delete_stmt(stmt, Some(parent), &deleted, locator, checker.indexer) {
match delete_stmt(
stmt,
Some(parent),
&deleted,
locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
checker.deletions.insert(RefEquality(stmt));
diagnostic.amend(fix);

View File

@ -63,6 +63,7 @@ pub fn print_call(checker: &mut Checker, func: &Expr, keywords: &[Keyword]) {
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
if fix.content.is_empty() || fix.content == "pass" {

View File

@ -65,9 +65,14 @@ fn remove_unused_variable(
.iter()
.map(std::convert::Into::into)
.collect();
let locator = checker.locator;
let indexer = checker.indexer;
match delete_stmt(stmt, parent, &deleted, locator, indexer) {
match delete_stmt(
stmt,
parent,
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => Some((DeletionKind::Whole, fix)),
Err(err) => {
error!("Failed to delete unused variable: {}", err);
@ -107,9 +112,14 @@ fn remove_unused_variable(
.iter()
.map(std::convert::Into::into)
.collect();
let locator = checker.locator;
let indexer = checker.indexer;
match delete_stmt(stmt, parent, &deleted, locator, indexer) {
match delete_stmt(
stmt,
parent,
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => Some((DeletionKind::Whole, fix)),
Err(err) => {
error!("Failed to delete unused variable: {}", err);

View File

@ -98,6 +98,7 @@ pub fn unnecessary_builtin_import(
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
if fix.content.is_empty() || fix.content == "pass" {

View File

@ -78,6 +78,7 @@ pub fn unnecessary_future_import(checker: &mut Checker, stmt: &Stmt, names: &[Lo
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
if fix.content.is_empty() || fix.content == "pass" {

View File

@ -46,6 +46,7 @@ pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr,
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
if fix.content.is_empty() || fix.content == "pass" {