Add stylist settings to all LibCST invocations (#2225)
This commit is contained in:
@@ -306,7 +306,11 @@ pub fn remove_unused_imports<'a>(
|
||||
if aliases.is_empty() {
|
||||
delete_stmt(stmt, parent, deleted, locator, indexer, stylist)
|
||||
} else {
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
|
||||
@@ -9,7 +9,7 @@ use libcst_native::{
|
||||
use crate::ast::types::Range;
|
||||
use crate::cst::matchers::{match_expr, match_module};
|
||||
use crate::fix::Fix;
|
||||
use crate::source_code::Locator;
|
||||
use crate::source_code::{Locator, Stylist};
|
||||
|
||||
fn match_call<'a, 'b>(expr: &'a mut Expr<'b>) -> Result<&'a mut Call<'b>> {
|
||||
if let Expression::Call(call) = &mut expr.value {
|
||||
@@ -30,6 +30,7 @@ fn match_arg<'a, 'b>(call: &'a Call<'b>) -> Result<&'a Arg<'b>> {
|
||||
/// (C400) Convert `list(x for x in y)` to `[x for x in y]`.
|
||||
pub fn fix_unnecessary_generator_list(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call(GeneratorExp)))) -> Expr(ListComp)))
|
||||
@@ -58,7 +59,11 @@ pub fn fix_unnecessary_generator_list(
|
||||
rpar: generator_exp.rpar.clone(),
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -71,6 +76,7 @@ pub fn fix_unnecessary_generator_list(
|
||||
/// (C401) Convert `set(x for x in y)` to `{x for x in y}`.
|
||||
pub fn fix_unnecessary_generator_set(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call(GeneratorExp)))) -> Expr(SetComp)))
|
||||
@@ -99,7 +105,11 @@ pub fn fix_unnecessary_generator_set(
|
||||
rpar: generator_exp.rpar.clone(),
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -113,6 +123,7 @@ pub fn fix_unnecessary_generator_set(
|
||||
/// range(3)}`.
|
||||
pub fn fix_unnecessary_generator_dict(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
@@ -157,7 +168,11 @@ pub fn fix_unnecessary_generator_dict(
|
||||
whitespace_after_colon: ParenthesizableWhitespace::SimpleWhitespace(SimpleWhitespace(" ")),
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -170,6 +185,7 @@ pub fn fix_unnecessary_generator_dict(
|
||||
/// (C403) Convert `set([x for x in y])` to `{x for x in y}`.
|
||||
pub fn fix_unnecessary_list_comprehension_set(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call(ListComp)))) ->
|
||||
@@ -197,7 +213,11 @@ pub fn fix_unnecessary_list_comprehension_set(
|
||||
rpar: list_comp.rpar.clone(),
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -211,6 +231,7 @@ pub fn fix_unnecessary_list_comprehension_set(
|
||||
/// range(3)}`.
|
||||
pub fn fix_unnecessary_list_comprehension_dict(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
@@ -248,7 +269,11 @@ pub fn fix_unnecessary_list_comprehension_dict(
|
||||
rpar: list_comp.rpar.clone(),
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -302,7 +327,11 @@ fn drop_trailing_comma<'a>(
|
||||
}
|
||||
|
||||
/// (C405) Convert `set((1, 2))` to `{1, 2}`.
|
||||
pub fn fix_unnecessary_literal_set(locator: &Locator, expr: &rustpython_ast::Expr) -> Result<Fix> {
|
||||
pub fn fix_unnecessary_literal_set(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call(List|Tuple)))) -> Expr(Set)))
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
let mut tree = match_module(module_text)?;
|
||||
@@ -334,7 +363,11 @@ pub fn fix_unnecessary_literal_set(locator: &Locator, expr: &rustpython_ast::Exp
|
||||
}));
|
||||
}
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -345,7 +378,11 @@ pub fn fix_unnecessary_literal_set(locator: &Locator, expr: &rustpython_ast::Exp
|
||||
}
|
||||
|
||||
/// (C406) Convert `dict([(1, 2)])` to `{1: 2}`.
|
||||
pub fn fix_unnecessary_literal_dict(locator: &Locator, expr: &rustpython_ast::Expr) -> Result<Fix> {
|
||||
pub fn fix_unnecessary_literal_dict(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call(List|Tuple)))) -> Expr(Dict)))
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
let mut tree = match_module(module_text)?;
|
||||
@@ -399,7 +436,11 @@ pub fn fix_unnecessary_literal_dict(locator: &Locator, expr: &rustpython_ast::Ex
|
||||
rpar: vec![],
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -412,6 +453,7 @@ pub fn fix_unnecessary_literal_dict(locator: &Locator, expr: &rustpython_ast::Ex
|
||||
/// (C408)
|
||||
pub fn fix_unnecessary_collection_call(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call("list" | "tuple" | "dict")))) -> Expr(List|Tuple|Dict)
|
||||
@@ -508,7 +550,11 @@ pub fn fix_unnecessary_collection_call(
|
||||
}
|
||||
};
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -521,6 +567,7 @@ pub fn fix_unnecessary_collection_call(
|
||||
/// (C409) Convert `tuple([1, 2])` to `tuple(1, 2)`
|
||||
pub fn fix_unnecessary_literal_within_tuple_call(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
@@ -562,7 +609,11 @@ pub fn fix_unnecessary_literal_within_tuple_call(
|
||||
}],
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -575,6 +626,7 @@ pub fn fix_unnecessary_literal_within_tuple_call(
|
||||
/// (C410) Convert `list([1, 2])` to `[1, 2]`
|
||||
pub fn fix_unnecessary_literal_within_list_call(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
@@ -618,7 +670,11 @@ pub fn fix_unnecessary_literal_within_list_call(
|
||||
rpar: vec![],
|
||||
}));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -629,7 +685,11 @@ pub fn fix_unnecessary_literal_within_list_call(
|
||||
}
|
||||
|
||||
/// (C411) Convert `list([i * i for i in x])` to `[i * i for i in x]`.
|
||||
pub fn fix_unnecessary_list_call(locator: &Locator, expr: &rustpython_ast::Expr) -> Result<Fix> {
|
||||
pub fn fix_unnecessary_list_call(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
// Expr(Call(List|Tuple)))) -> Expr(List|Tuple)))
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
let mut tree = match_module(module_text)?;
|
||||
@@ -639,7 +699,11 @@ pub fn fix_unnecessary_list_call(locator: &Locator, expr: &rustpython_ast::Expr)
|
||||
|
||||
body.value = arg.value.clone();
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -654,6 +718,7 @@ pub fn fix_unnecessary_list_call(locator: &Locator, expr: &rustpython_ast::Expr)
|
||||
/// reverse=True)`.
|
||||
pub fn fix_unnecessary_call_around_sorted(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
@@ -723,7 +788,11 @@ pub fn fix_unnecessary_call_around_sorted(
|
||||
}
|
||||
}
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -736,6 +805,7 @@ pub fn fix_unnecessary_call_around_sorted(
|
||||
/// (C416) Convert `[i for i in x]` to `list(x)`.
|
||||
pub fn fix_unnecessary_comprehension(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
expr: &rustpython_ast::Expr,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
@@ -792,7 +862,11 @@ pub fn fix_unnecessary_comprehension(
|
||||
}
|
||||
}
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
|
||||
@@ -66,7 +66,7 @@ pub fn unnecessary_generator_list(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryGeneratorList) {
|
||||
match fixes::fix_unnecessary_generator_list(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -97,7 +97,7 @@ pub fn unnecessary_generator_set(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryGeneratorSet) {
|
||||
match fixes::fix_unnecessary_generator_set(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -127,7 +127,11 @@ pub fn unnecessary_generator_dict(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryGeneratorDict) {
|
||||
match fixes::fix_unnecessary_generator_dict(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_generator_dict(
|
||||
checker.locator,
|
||||
checker.stylist,
|
||||
expr,
|
||||
) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -161,7 +165,11 @@ pub fn unnecessary_list_comprehension_set(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryListComprehensionSet) {
|
||||
match fixes::fix_unnecessary_list_comprehension_set(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_list_comprehension_set(
|
||||
checker.locator,
|
||||
checker.stylist,
|
||||
expr,
|
||||
) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -200,7 +208,8 @@ pub fn unnecessary_list_comprehension_dict(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryListComprehensionDict) {
|
||||
match fixes::fix_unnecessary_list_comprehension_dict(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr)
|
||||
{
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -234,7 +243,7 @@ pub fn unnecessary_literal_set(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryLiteralSet) {
|
||||
match fixes::fix_unnecessary_literal_set(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -275,7 +284,7 @@ pub fn unnecessary_literal_dict(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryLiteralDict) {
|
||||
match fixes::fix_unnecessary_literal_dict(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -316,7 +325,7 @@ pub fn unnecessary_collection_call(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryCollectionCall) {
|
||||
match fixes::fix_unnecessary_collection_call(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -349,7 +358,11 @@ pub fn unnecessary_literal_within_tuple_call(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryLiteralWithinTupleCall) {
|
||||
match fixes::fix_unnecessary_literal_within_tuple_call(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_literal_within_tuple_call(
|
||||
checker.locator,
|
||||
checker.stylist,
|
||||
expr,
|
||||
) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -382,7 +395,11 @@ pub fn unnecessary_literal_within_list_call(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryLiteralWithinListCall) {
|
||||
match fixes::fix_unnecessary_literal_within_list_call(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_literal_within_list_call(
|
||||
checker.locator,
|
||||
checker.stylist,
|
||||
expr,
|
||||
) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -406,7 +423,7 @@ pub fn unnecessary_list_call(checker: &mut Checker, expr: &Expr, func: &Expr, ar
|
||||
let mut diagnostic =
|
||||
Diagnostic::new(violations::UnnecessaryListCall, Range::from_located(expr));
|
||||
if checker.patch(&Rule::UnnecessaryListCall) {
|
||||
match fixes::fix_unnecessary_list_call(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -449,7 +466,7 @@ pub fn unnecessary_call_around_sorted(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryCallAroundSorted) {
|
||||
match fixes::fix_unnecessary_call_around_sorted(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -612,7 +629,7 @@ pub fn unnecessary_comprehension(
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if checker.patch(&Rule::UnnecessaryComprehension) {
|
||||
match fixes::fix_unnecessary_comprehension(checker.locator, expr) {
|
||||
match fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ pub(crate) fn fix_multiple_with_statements(
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..Default::default()
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ use crate::ast::types::Range;
|
||||
use crate::cst::matchers::{match_expr, match_module};
|
||||
use crate::fix::Fix;
|
||||
use crate::python::string::strip_quotes_and_prefixes;
|
||||
use crate::source_code::Locator;
|
||||
use crate::source_code::{Locator, Stylist};
|
||||
|
||||
/// Generate a [`Fix`] to remove unused keys from format dict.
|
||||
pub fn remove_unused_format_arguments_from_dict(
|
||||
unused_arguments: &[&str],
|
||||
stmt: &Expr,
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(stmt));
|
||||
let mut tree = match_module(module_text)?;
|
||||
@@ -46,7 +47,11 @@ pub fn remove_unused_format_arguments_from_dict(
|
||||
|
||||
body.value = Expression::Dict(Box::new(new_dict));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
@@ -61,6 +66,7 @@ pub fn remove_unused_keyword_arguments_from_format_call(
|
||||
unused_arguments: &[&str],
|
||||
location: Range,
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
) -> Result<Fix> {
|
||||
let module_text = locator.slice_source_code_range(&location);
|
||||
let mut tree = match_module(module_text)?;
|
||||
@@ -90,7 +96,11 @@ pub fn remove_unused_keyword_arguments_from_format_call(
|
||||
|
||||
body.value = Expression::Call(Box::new(new_call));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Ok(Fix::replacement(
|
||||
|
||||
@@ -120,7 +120,12 @@ pub(crate) fn percent_format_extra_named_arguments(
|
||||
location,
|
||||
);
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
match remove_unused_format_arguments_from_dict(&missing, right, checker.locator) {
|
||||
match remove_unused_format_arguments_from_dict(
|
||||
&missing,
|
||||
right,
|
||||
checker.locator,
|
||||
checker.stylist,
|
||||
) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
@@ -278,8 +283,12 @@ pub(crate) fn string_dot_format_extra_named_arguments(
|
||||
location,
|
||||
);
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
match remove_unused_keyword_arguments_from_format_call(&missing, location, checker.locator)
|
||||
{
|
||||
match remove_unused_keyword_arguments_from_format_call(
|
||||
&missing,
|
||||
location,
|
||||
checker.locator,
|
||||
checker.stylist,
|
||||
) {
|
||||
Ok(fix) => {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use rustpython_parser::lexer::Tok;
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::fix::Fix;
|
||||
use crate::source_code::Locator;
|
||||
use crate::source_code::{Locator, Stylist};
|
||||
|
||||
/// Generate a fix to remove a base from a `ClassDef` statement.
|
||||
pub fn remove_class_def_base(
|
||||
@@ -105,7 +105,7 @@ pub fn remove_class_def_base(
|
||||
}
|
||||
|
||||
/// Generate a fix to remove arguments from a `super` call.
|
||||
pub fn remove_super_arguments(locator: &Locator, expr: &Expr) -> Option<Fix> {
|
||||
pub fn remove_super_arguments(locator: &Locator, stylist: &Stylist, expr: &Expr) -> Option<Fix> {
|
||||
let range = Range::from_located(expr);
|
||||
let contents = locator.slice_source_code_range(&range);
|
||||
|
||||
@@ -125,7 +125,11 @@ pub fn remove_super_arguments(locator: &Locator, expr: &Expr) -> Option<Fix> {
|
||||
body.whitespace_before_args = ParenthesizableWhitespace::default();
|
||||
body.whitespace_after_func = ParenthesizableWhitespace::default();
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
Some(Fix::replacement(
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::cst::matchers::{match_call, match_expression};
|
||||
use crate::fix::Fix;
|
||||
use crate::registry::Diagnostic;
|
||||
use crate::rules::pyflakes::format::FormatSummary;
|
||||
use crate::source_code::{Locator, Stylist};
|
||||
use crate::violations;
|
||||
|
||||
// An opening curly brace, followed by any integer, followed by any text,
|
||||
@@ -54,7 +55,13 @@ fn generate_arguments<'a>(
|
||||
}
|
||||
|
||||
/// Returns the corrected function call.
|
||||
fn generate_call(module_text: &str, correct_order: &[usize]) -> Result<String> {
|
||||
fn generate_call(
|
||||
expr: &Expr,
|
||||
correct_order: &[usize],
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
) -> Result<String> {
|
||||
let module_text = locator.slice_source_code_range(&Range::from_located(expr));
|
||||
let mut expression = match_expression(module_text)?;
|
||||
let mut call = match_call(&mut expression)?;
|
||||
|
||||
@@ -66,13 +73,21 @@ fn generate_call(module_text: &str, correct_order: &[usize]) -> Result<String> {
|
||||
panic!("Expected: Expression::Attribute")
|
||||
};
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
item.codegen(&mut state);
|
||||
let cleaned = remove_specifiers(&state.to_string());
|
||||
|
||||
call.func = Box::new(match_expression(&cleaned)?);
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
expression.codegen(&mut state);
|
||||
if module_text == state.to_string() {
|
||||
// Ex) `'{' '0}'.format(1)`
|
||||
@@ -101,12 +116,9 @@ pub(crate) fn format_literals(checker: &mut Checker, summary: &FormatSummary, ex
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
// Currently, the only issue we know of is in LibCST:
|
||||
// https://github.com/Instagram/LibCST/issues/846
|
||||
if let Ok(contents) = generate_call(
|
||||
checker
|
||||
.locator
|
||||
.slice_source_code_range(&Range::from_located(expr)),
|
||||
&summary.indexes,
|
||||
) {
|
||||
if let Ok(contents) =
|
||||
generate_call(expr, &summary.indexes, checker.locator, checker.stylist)
|
||||
{
|
||||
diagnostic.amend(Fix::replacement(
|
||||
contents,
|
||||
expr.location,
|
||||
|
||||
@@ -128,7 +128,11 @@ fn format_import(
|
||||
} else {
|
||||
import.names = clean_aliases;
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
let mut content = state.to_string();
|
||||
@@ -183,7 +187,11 @@ fn format_import_from(
|
||||
rpar: vec![],
|
||||
})));
|
||||
|
||||
let mut state = CodegenState::default();
|
||||
let mut state = CodegenState {
|
||||
default_newline: stylist.line_ending(),
|
||||
default_indent: stylist.indentation(),
|
||||
..CodegenState::default()
|
||||
};
|
||||
tree.codegen(&mut state);
|
||||
|
||||
let mut content = state.to_string();
|
||||
|
||||
@@ -21,7 +21,7 @@ pub fn super_call_with_parameters(checker: &mut Checker, expr: &Expr, func: &Exp
|
||||
return;
|
||||
};
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
if let Some(fix) = fixes::remove_super_arguments(checker.locator, expr) {
|
||||
if let Some(fix) = fixes::remove_super_arguments(checker.locator, checker.stylist, expr) {
|
||||
diagnostic.amend(fix);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user