-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use multi-fix semantics for inplace removal
- Loading branch information
1 parent
8829875
commit 1307b73
Showing
6 changed files
with
150 additions
and
116 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,46 @@ | ||
use rustpython_parser::ast::{Expr, ExprKind, Keyword, Location}; | ||
|
||
use ruff_diagnostics::Edit; | ||
use ruff_python_ast::helpers; | ||
use ruff_diagnostics::{Edit, Fix}; | ||
use ruff_python_ast::source_code::Locator; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::autofix::apply_fix; | ||
use crate::autofix::helpers::remove_argument; | ||
|
||
fn match_name(expr: &Expr) -> Option<&str> { | ||
if let ExprKind::Call { func, .. } = &expr.node { | ||
if let ExprKind::Attribute { value, .. } = &func.node { | ||
if let ExprKind::Name { id, .. } = &value.node { | ||
Some(id) | ||
} else { | ||
None | ||
return Some(id); | ||
} | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
None | ||
} | ||
|
||
/// Remove the `inplace` argument from a function call and replace it with an | ||
/// assignment. | ||
pub fn fix_inplace_argument( | ||
pub(super) fn convert_inplace_argument_to_assignment( | ||
locator: &Locator, | ||
expr: &Expr, | ||
violation_at: Location, | ||
violation_end: Location, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
) -> Option<Edit> { | ||
if let Ok(fix) = remove_argument( | ||
) -> Option<Fix> { | ||
// Add the assignment. | ||
let name = match_name(expr)?; | ||
let insert_assignment = Edit::insertion(format!("{name} = "), expr.location); | ||
|
||
// Remove the `inplace` argument. | ||
let remove_argument = remove_argument( | ||
locator, | ||
expr.location, | ||
violation_at, | ||
violation_end, | ||
args, | ||
keywords, | ||
false, | ||
) { | ||
// Reset the line index. | ||
let fix_me = Edit::deletion( | ||
helpers::to_relative(fix.location, expr.location), | ||
helpers::to_relative(fix.end_location, expr.location), | ||
); | ||
|
||
// Apply the deletion step. | ||
// TODO(charlie): Find a way to | ||
let contents = locator.slice(Range::new(expr.location, expr.end_location.unwrap())); | ||
let output = apply_fix(&fix_me, &Locator::new(contents)); | ||
|
||
// Obtain the name prefix. | ||
let name = match_name(expr)?; | ||
) | ||
.ok()?; | ||
|
||
// Apply the assignment. | ||
let new_contents = format!("{name} = {output}"); | ||
|
||
// Create the new fix. | ||
Some(Edit::replacement( | ||
new_contents, | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)) | ||
} else { | ||
None | ||
} | ||
Some(Fix::from_iter([insert_assignment, remove_argument])) | ||
} |
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.