Skip to content

Commit

Permalink
Rename Fix to Edit
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 24, 2023
1 parent c721eed commit b526d51
Show file tree
Hide file tree
Showing 109 changed files with 394 additions and 404 deletions.
24 changes: 12 additions & 12 deletions crates/ruff/src/autofix/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libcst_native::{
use rustpython_parser::ast::{ExcepthandlerKind, Expr, Keyword, Location, Stmt, StmtKind};
use rustpython_parser::{lexer, Mode, Tok};

use ruff_diagnostics::Fix;
use ruff_diagnostics::Edit;
use ruff_python_ast::helpers;
use ruff_python_ast::helpers::to_absolute;
use ruff_python_ast::newlines::NewlineWithTrailingNewline;
Expand Down Expand Up @@ -178,38 +178,38 @@ pub fn delete_stmt(
locator: &Locator,
indexer: &Indexer,
stylist: &Stylist,
) -> Result<Fix> {
) -> Result<Edit> {
if parent
.map(|parent| is_lone_child(stmt, parent, deleted))
.map_or(Ok(None), |v| v.map(Some))?
.unwrap_or_default()
{
// If removing this node would lead to an invalid syntax tree, replace
// it with a `pass`.
Ok(Fix::replacement(
Ok(Edit::replacement(
"pass".to_string(),
stmt.location,
stmt.end_location.unwrap(),
))
} else {
Ok(if let Some(semicolon) = trailing_semicolon(stmt, locator) {
let next = next_stmt_break(semicolon, locator);
Fix::deletion(stmt.location, next)
Edit::deletion(stmt.location, next)
} else if helpers::match_leading_content(stmt, locator) {
Fix::deletion(stmt.location, stmt.end_location.unwrap())
Edit::deletion(stmt.location, stmt.end_location.unwrap())
} 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(
Edit::replacement(
stylist.line_ending().to_string(),
stmt.location,
stmt.end_location.unwrap(),
)
} else {
Fix::deletion(stmt.location, stmt.end_location.unwrap())
Edit::deletion(stmt.location, stmt.end_location.unwrap())
}
} else {
Fix::deletion(
Edit::deletion(
Location::new(stmt.location.row(), 0),
Location::new(stmt.end_location.unwrap().row() + 1, 0),
)
Expand All @@ -226,7 +226,7 @@ pub fn remove_unused_imports<'a>(
locator: &Locator,
indexer: &Indexer,
stylist: &Stylist,
) -> Result<Fix> {
) -> Result<Edit> {
let module_text = locator.slice(stmt);
let mut tree = match_module(module_text)?;

Expand Down Expand Up @@ -333,7 +333,7 @@ pub fn remove_unused_imports<'a>(
};
tree.codegen(&mut state);

Ok(Fix::replacement(
Ok(Edit::replacement(
state.to_string(),
stmt.location,
stmt.end_location.unwrap(),
Expand All @@ -355,7 +355,7 @@ pub fn remove_argument(
args: &[Expr],
keywords: &[Keyword],
remove_parentheses: bool,
) -> Result<Fix> {
) -> Result<Edit> {
// TODO(sbrugman): Preserve trailing comments.
let contents = locator.skip(stmt_at);

Expand Down Expand Up @@ -437,7 +437,7 @@ pub fn remove_argument(
}

match (fix_start, fix_end) {
(Some(start), Some(end)) => Ok(Fix::deletion(start, end)),
(Some(start), Some(end)) => Ok(Edit::deletion(start, end)),
_ => {
bail!("No fix could be constructed")
}
Expand Down
26 changes: 13 additions & 13 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use itertools::Itertools;
use rustc_hash::FxHashMap;
use rustpython_parser::ast::Location;

use ruff_diagnostics::{Diagnostic, Fix};
use ruff_diagnostics::{Diagnostic, Edit};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;

Expand All @@ -29,7 +29,7 @@ fn apply_fixes<'a>(
) -> (String, FixTable) {
let mut output = String::with_capacity(locator.len());
let mut last_pos: Option<Location> = None;
let mut applied: BTreeSet<&Fix> = BTreeSet::default();
let mut applied: BTreeSet<&Edit> = BTreeSet::default();
let mut fixed = FxHashMap::default();

for (rule, fix) in diagnostics
Expand Down Expand Up @@ -75,7 +75,7 @@ fn apply_fixes<'a>(
}

/// Apply a single fix.
pub(crate) fn apply_fix(fix: &Fix, locator: &Locator) -> String {
pub(crate) fn apply_fix(fix: &Edit, locator: &Locator) -> String {
let mut output = String::with_capacity(locator.len());

// Add all contents from `last_pos` to `fix.location`.
Expand All @@ -93,7 +93,7 @@ pub(crate) fn apply_fix(fix: &Fix, locator: &Locator) -> String {
}

/// Compare two fixes.
fn cmp_fix(rule1: Rule, rule2: Rule, fix1: &Fix, fix2: &Fix) -> std::cmp::Ordering {
fn cmp_fix(rule1: Rule, rule2: Rule, fix1: &Edit, fix2: &Edit) -> std::cmp::Ordering {
fix1.location
.cmp(&fix2.location)
.then_with(|| match (&rule1, &rule2) {
Expand All @@ -109,13 +109,13 @@ mod tests {
use rustpython_parser::ast::Location;

use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Fix;
use ruff_diagnostics::Edit;
use ruff_python_ast::source_code::Locator;

use crate::autofix::{apply_fix, apply_fixes};
use crate::rules::pycodestyle::rules::MissingNewlineAtEndOfFile;

fn create_diagnostics(fixes: impl IntoIterator<Item = Fix>) -> Vec<Diagnostic> {
fn create_diagnostics(fixes: impl IntoIterator<Item = Edit>) -> Vec<Diagnostic> {
fixes
.into_iter()
.map(|fix| Diagnostic {
Expand Down Expand Up @@ -147,7 +147,7 @@ class A(object):
"#
.trim(),
);
let diagnostics = create_diagnostics([Fix {
let diagnostics = create_diagnostics([Edit {
content: "Bar".to_string(),
location: Location::new(1, 8),
end_location: Location::new(1, 14),
Expand All @@ -173,7 +173,7 @@ class A(object):
"#
.trim(),
);
let diagnostics = create_diagnostics([Fix {
let diagnostics = create_diagnostics([Edit {
content: String::new(),
location: Location::new(1, 7),
end_location: Location::new(1, 15),
Expand All @@ -200,12 +200,12 @@ class A(object, object, object):
.trim(),
);
let diagnostics = create_diagnostics([
Fix {
Edit {
content: String::new(),
location: Location::new(1, 8),
end_location: Location::new(1, 16),
},
Fix {
Edit {
content: String::new(),
location: Location::new(1, 22),
end_location: Location::new(1, 30),
Expand Down Expand Up @@ -234,12 +234,12 @@ class A(object):
.trim(),
);
let diagnostics = create_diagnostics([
Fix {
Edit {
content: String::new(),
location: Location::new(1, 7),
end_location: Location::new(1, 15),
},
Fix {
Edit {
content: "ignored".to_string(),
location: Location::new(1, 9),
end_location: Location::new(1, 11),
Expand Down Expand Up @@ -267,7 +267,7 @@ class A(object):
.trim(),
);
let contents = apply_fix(
&Fix {
&Edit {
content: String::new(),
location: Location::new(1, 7),
end_location: Location::new(1, 15),
Expand Down
16 changes: 8 additions & 8 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use nohash_hasher::IntMap;
use rustpython_parser::ast::Location;

use ruff_diagnostics::{Diagnostic, Fix};
use ruff_diagnostics::{Diagnostic, Edit};
use ruff_python_ast::newlines::StrExt;
use ruff_python_ast::types::Range;

Expand Down Expand Up @@ -223,7 +223,7 @@ pub fn check_noqa(
trailing_spaces,
));
} else {
diagnostic.amend(Fix::replacement(
diagnostic.amend(Edit::replacement(
format!("# noqa: {}", valid_codes.join(", ")),
Location::new(row + 1, start_char),
Location::new(row + 1, end_char),
Expand All @@ -242,39 +242,39 @@ pub fn check_noqa(
ignored_diagnostics
}

/// Generate a [`Fix`] to delete a `noqa` directive.
/// Generate a [`Edit`] to delete a `noqa` directive.
fn delete_noqa(
row: usize,
line: &str,
leading_spaces: usize,
start_byte: usize,
end_byte: usize,
trailing_spaces: usize,
) -> Fix {
) -> Edit {
if start_byte - leading_spaces == 0 && end_byte == line.len() {
// Ex) `# noqa`
Fix::deletion(Location::new(row + 1, 0), Location::new(row + 2, 0))
Edit::deletion(Location::new(row + 1, 0), Location::new(row + 2, 0))
} else if end_byte == line.len() {
// Ex) `x = 1 # noqa`
let start_char = line[..start_byte].chars().count();
let end_char = start_char + line[start_byte..end_byte].chars().count();
Fix::deletion(
Edit::deletion(
Location::new(row + 1, start_char - leading_spaces),
Location::new(row + 1, end_char + trailing_spaces),
)
} else if line[end_byte..].trim_start().starts_with('#') {
// Ex) `x = 1 # noqa # type: ignore`
let start_char = line[..start_byte].chars().count();
let end_char = start_char + line[start_byte..end_byte].chars().count();
Fix::deletion(
Edit::deletion(
Location::new(row + 1, start_char),
Location::new(row + 1, end_char + trailing_spaces),
)
} else {
// Ex) `x = 1 # noqa here`
let start_char = line[..start_byte].chars().count();
let end_char = start_char + line[start_byte..end_byte].chars().count();
Fix::deletion(
Edit::deletion(
Location::new(row + 1, start_char + 1 + 1),
Location::new(row + 1, end_char + trailing_spaces),
)
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::Ordering;
pub use rustpython_parser::ast::Location;
use serde::{Deserialize, Serialize};

use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix};
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Edit};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;

Expand All @@ -12,7 +12,7 @@ pub struct Message {
pub kind: DiagnosticKind,
pub location: Location,
pub end_location: Location,
pub fix: Option<Fix>,
pub fix: Option<Edit>,
pub filename: String,
pub source: Option<Source>,
pub noqa_row: usize,
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/eradicate/rules.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustpython_parser::ast::Location;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn commented_out_code(
if is_standalone_comment(line) && comment_contains_code(line, &settings.task_tags[..]) {
let mut diagnostic = Diagnostic::new(CommentedOutCode, Range::new(start, end));
if autofix.into() && settings.rules.should_fix(Rule::CommentedOutCode) {
diagnostic.amend(Fix::deletion(location, end_location));
diagnostic.amend(Edit::deletion(location, end_location));
}
Some(diagnostic)
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_annotations/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use anyhow::{bail, Result};
use rustpython_parser::ast::Stmt;
use rustpython_parser::{lexer, Mode, Tok};

use ruff_diagnostics::Fix;
use ruff_diagnostics::Edit;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;

/// ANN204
pub fn add_return_annotation(locator: &Locator, stmt: &Stmt, annotation: &str) -> Result<Fix> {
pub fn add_return_annotation(locator: &Locator, stmt: &Stmt, annotation: &str) -> Result<Edit> {
let range = Range::from(stmt);
let contents = locator.slice(range);

Expand All @@ -18,7 +18,7 @@ pub fn add_return_annotation(locator: &Locator, stmt: &Stmt, annotation: &str) -
for (start, tok, ..) in lexer::lex_located(contents, Mode::Module, range.location).flatten() {
if seen_lpar && seen_rpar {
if matches!(tok, Tok::Colon) {
return Ok(Fix::insertion(format!(" -> {annotation}"), start));
return Ok(Edit::insertion(format!(" -> {annotation}"), start));
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_stmt;
use ruff_python_ast::types::Range;
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option

let mut diagnostic = Diagnostic::new(AssertFalse, Range::from(test));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
diagnostic.amend(Edit::replacement(
unparse_stmt(&assertion_error(msg), checker.stylist),
stmt.location,
stmt.end_location.unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustpython_parser::ast::{
};

use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Fix};
use ruff_diagnostics::{Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers;
use ruff_python_ast::helpers::unparse_expr;
Expand Down Expand Up @@ -98,7 +98,7 @@ fn duplicate_handler_exceptions<'a>(
Range::from(expr),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
diagnostic.amend(Edit::replacement(
if unique_elts.len() == 1 {
unparse_expr(unique_elts[0], checker.stylist)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustpython_parser::ast::{Constant, Expr, ExprContext, ExprKind, Location};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_expr;
use ruff_python_ast::types::Range;
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar
let mut diagnostic = Diagnostic::new(GetAttrWithConstant, Range::from(expr));

if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
diagnostic.amend(Edit::replacement(
unparse_expr(&attribute(obj, value), checker.stylist),
expr.location,
expr.end_location.unwrap(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, ExprKind};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_expr;
use ruff_python_ast::types::Range;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E
Range::from(type_),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
diagnostic.amend(Edit::replacement(
unparse_expr(elt, checker.stylist),
type_.location,
type_.end_location.unwrap(),
Expand Down
Loading

0 comments on commit b526d51

Please sign in to comment.