Skip to content

Commit

Permalink
Move unparse utility methods onto Generator (#4497)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored May 18, 2023
1 parent d3b1834 commit e9c6f16
Show file tree
Hide file tree
Showing 40 changed files with 137 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustpython_parser::ast::{self, Expr, Operator, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::{any_over_expr, unparse_expr};
use ruff_python_ast::helpers::any_over_expr;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -62,14 +62,14 @@ fn unparse_string_format_expression(checker: &mut Checker, expr: &Expr) -> Optio
}) => {
let Some(parent) = checker.ctx.expr_parent() else {
if any_over_expr(expr, &has_string_literal) {
return Some(unparse_expr(expr, checker.generator()));
return Some(checker.generator().expr(expr));
}
return None;
};
// Only evaluate the full BinOp, not the nested components.
let Expr::BinOp(_ )= parent else {
if any_over_expr(expr, &has_string_literal) {
return Some(unparse_expr(expr, checker.generator()));
return Some(checker.generator().expr(expr));
}
return None;
};
Expand All @@ -81,12 +81,12 @@ fn unparse_string_format_expression(checker: &mut Checker, expr: &Expr) -> Optio
};
// "select * from table where val = {}".format(...)
if attr == "format" && string_literal(value).is_some() {
return Some(unparse_expr(expr, checker.generator()));
return Some(checker.generator().expr(expr));
};
None
}
// f"select * from table where val = {val}"
Expr::JoinedStr(_) => Some(unparse_expr(expr, checker.generator())),
Expr::JoinedStr(_) => Some(checker.generator().expr(expr)),
_ => None,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::{unparse_constant, SimpleCallArgs};
use ruff_python_ast::helpers::SimpleCallArgs;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) fn request_without_timeout(
Expr::Constant(ast::ExprConstant {
value: value @ Constant::None,
..
}) => Some(unparse_constant(value, checker.generator())),
}) => Some(checker.generator().constant(value)),
_ => None,
} {
checker.diagnostics.push(Diagnostic::new(
Expand Down
3 changes: 1 addition & 2 deletions crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged, Stmt};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_stmt;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
Expand Down Expand Up @@ -56,7 +55,7 @@ pub(crate) fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg:
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_stmt(&assertion_error(msg), checker.generator()),
checker.generator().stmt(&assertion_error(msg)),
stmt.range(),
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path;
use ruff_python_ast::call_path::CallPath;
use ruff_python_ast::helpers::unparse_expr;

use crate::checkers::ast::Checker;
use crate::registry::{AsRule, Rule};
Expand Down Expand Up @@ -97,9 +96,9 @@ fn duplicate_handler_exceptions<'a>(
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
if unique_elts.len() == 1 {
unparse_expr(unique_elts[0], checker.generator())
checker.generator().expr(unique_elts[0])
} else {
unparse_expr(&type_pattern(unique_elts), checker.generator())
checker.generator().expr(&type_pattern(unique_elts))
},
expr.range(),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_expr;

use ruff_python_stdlib::identifiers::{is_identifier, is_mangled_private};

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub(crate) fn getattr_with_constant(
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&attribute(obj, value), checker.generator()),
checker.generator().expr(&attribute(obj, value)),
expr.range(),
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rustpython_parser::ast::{self, Excepthandler, Expr, Ranged};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_expr;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
Expand Down Expand Up @@ -45,14 +44,14 @@ pub(crate) fn redundant_tuple_in_exception_handler(
};
let mut diagnostic = Diagnostic::new(
RedundantTupleInExceptionHandler {
name: unparse_expr(elt, checker.generator()),
name: checker.generator().expr(elt),
},
type_.range(),
);
if checker.patch(diagnostic.kind.rule()) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(elt, checker.generator()),
checker.generator().expr(elt),
type_.range(),
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged, Stmt};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_stmt;

use ruff_python_ast::source_code::Generator;
use ruff_python_stdlib::identifiers::{is_identifier, is_mangled_private};

Expand Down Expand Up @@ -39,7 +39,7 @@ fn assignment(obj: &Expr, name: &str, value: &Expr, generator: Generator) -> Str
type_comment: None,
range: TextRange::default(),
});
unparse_stmt(&stmt, generator)
generator.stmt(&stmt)
}

/// B010
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff/src/rules/flake8_errmsg/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged, Stmt};

use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_stmt;

use ruff_python_ast::source_code::{Generator, Stylist};
use ruff_python_ast::whitespace;

Expand Down Expand Up @@ -201,7 +201,7 @@ fn generate_fix(
type_comment: None,
range: TextRange::default(),
});
let assignment = unparse_stmt(&node1, generator);
let assignment = generator.stmt(&node1);
#[allow(deprecated)]
Fix::unspecified_edits(
Edit::insertion(
Expand All @@ -225,7 +225,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
if let Expr::Call(ast::ExprCall { args, .. }) = exc {
if let Some(first) = args.first() {
match first {
// Check for string literals
// Check for string literals.
Expr::Constant(ast::ExprConstant {
value: Constant::Str(string),
..
Expand Down Expand Up @@ -257,7 +257,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
}
}
}
// Check for f-strings
// Check for f-strings.
Expr::JoinedStr(_) => {
if checker.settings.rules.enabled(Rule::FStringInException) {
let indentation = whitespace::indentation(checker.locator, stmt).and_then(
Expand All @@ -284,7 +284,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
checker.diagnostics.push(diagnostic);
}
}
// Check for .format() calls
// Check for .format() calls.
Expr::Call(ast::ExprCall { func, .. }) => {
if checker.settings.rules.enabled(Rule::DotFormatInException) {
if let Expr::Attribute(ast::ExprAttribute { value, attr, .. }) =
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/rules/flake8_pie/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::helpers::{trailing_comment_start_offset, unparse_expr};
use ruff_python_ast::helpers::trailing_comment_start_offset;
use ruff_python_ast::types::RefEquality;
use ruff_python_stdlib::identifiers::is_identifier;

Expand Down Expand Up @@ -443,7 +443,7 @@ pub(crate) fn non_unique_enums<'a, 'b>(
if !seen_targets.insert(ComparableExpr::from(value)) {
let diagnostic = Diagnostic::new(
NonUniqueEnums {
value: unparse_expr(value, checker.generator()),
value: checker.generator().expr(value),
},
stmt.range(),
);
Expand Down Expand Up @@ -612,7 +612,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) {
let bool_op = node;
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&bool_op, checker.generator()),
checker.generator().expr(&bool_op),
expr.range(),
)));
}
Expand Down
10 changes: 4 additions & 6 deletions crates/ruff/src/rules/flake8_pyi/rules/duplicate_union_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rustpython_parser::ast::{self, Expr, Operator, Ranged};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::helpers::unparse_expr;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
Expand Down Expand Up @@ -61,7 +60,7 @@ fn traverse_union<'a>(
if !seen_nodes.insert(expr.into()) {
let mut diagnostic = Diagnostic::new(
DuplicateUnionMember {
duplicate_name: unparse_expr(expr, checker.generator()),
duplicate_name: checker.generator().expr(expr),
},
expr.range(),
);
Expand All @@ -80,10 +79,9 @@ fn traverse_union<'a>(
// Replace the parent with its non-duplicate child.
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(
if expr == left.as_ref() { right } else { left },
checker.generator(),
),
checker
.generator()
.expr(if expr == left.as_ref() { right } else { left }),
parent.range(),
)));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::borrow::Cow;

use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::{has_comments_in, unparse_stmt, Truthiness};
use ruff_python_ast::helpers::{has_comments_in, Truthiness};
use ruff_python_ast::source_code::{Locator, Stylist};
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{visitor, whitespace};
Expand Down Expand Up @@ -198,7 +198,7 @@ pub(crate) fn unittest_assertion(
if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) {
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_stmt(&stmt, checker.generator()),
checker.generator().stmt(&stmt),
expr.range(),
)));
}
Expand Down
13 changes: 6 additions & 7 deletions crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rustpython_parser::{lexer, Mode, Tok};

use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::unparse_expr;

use crate::checkers::ast::Checker;
use crate::registry::{AsRule, Rule};
Expand Down Expand Up @@ -78,7 +77,7 @@ fn elts_to_csv(elts: &[Expr], checker: &Checker) -> Option<String> {
kind: None,
range: TextRange::default(),
});
Some(unparse_expr(&node, checker.generator()))
Some(checker.generator().expr(&node))
}

/// Returns the range of the `name` argument of `@pytest.mark.parametrize`.
Expand Down Expand Up @@ -164,7 +163,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
});
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
format!("({})", unparse_expr(&node, checker.generator())),
format!("({})", checker.generator().expr(&node)),
name_range,
)));
}
Expand Down Expand Up @@ -195,7 +194,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
});
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&node, checker.generator()),
checker.generator().expr(&node),
name_range,
)));
}
Expand Down Expand Up @@ -228,7 +227,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
});
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&node, checker.generator()),
checker.generator().expr(&node),
expr.range(),
)));
}
Expand Down Expand Up @@ -278,7 +277,7 @@ fn check_names(checker: &mut Checker, decorator: &Expr, expr: &Expr) {
});
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
format!("({})", unparse_expr(&node, checker.generator())),
format!("({})", checker.generator().expr(&node)),
expr.range(),
)));
}
Expand Down Expand Up @@ -373,7 +372,7 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) {
let node = value.clone();
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&node, checker.generator()),
checker.generator().expr(&node),
expr.range(),
)));
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff/src/rules/flake8_simplify/rules/ast_bool_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustpython_parser::ast::{self, Boolop, Cmpop, Expr, ExprContext, Ranged, Una
use ruff_diagnostics::{AlwaysAutofixableViolation, AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::helpers::{contains_effect, has_comments, unparse_expr, Truthiness};
use ruff_python_ast::helpers::{contains_effect, has_comments, Truthiness};

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
Expand Down Expand Up @@ -368,7 +368,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
// multiple duplicates, the fixes will conflict.
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&bool_op, checker.generator()),
checker.generator().expr(&bool_op),
expr.range(),
)));
}
Expand Down Expand Up @@ -455,7 +455,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
let in_expr = node2.into();
let mut diagnostic = Diagnostic::new(
CompareWithTuple {
replacement: unparse_expr(&in_expr, checker.generator()),
replacement: checker.generator().expr(&in_expr),
},
expr.range(),
);
Expand All @@ -479,7 +479,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
};
#[allow(deprecated)]
diagnostic.set_fix(Fix::unspecified(Edit::range_replacement(
unparse_expr(&in_expr, checker.generator()),
checker.generator().expr(&in_expr),
expr.range(),
)));
}
Expand Down Expand Up @@ -613,7 +613,7 @@ pub(crate) fn get_short_circuit_edit(
}
}
} else {
unparse_expr(expr, checker.generator())
checker.generator().expr(expr)
};
Edit::range_replacement(content, range)
}
Expand Down
Loading

0 comments on commit e9c6f16

Please sign in to comment.