Skip to content

Commit

Permalink
Slice source code instead of generating it for EM fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Oct 1, 2023
1 parent e91ffe3 commit b861379
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ def f_fix_indentation_check(foo):
# Report these, but don't fix them
if foo: raise RuntimeError("This is an example exception")
if foo: x = 1; raise RuntimeError("This is an example exception")


def f_triple_quoted_string():
raise RuntimeError(f"""This is an {"example"} exception""")
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use ruff_python_ast::{self as ast, Arguments, Constant, Expr, ExprContext, Stmt};
use ruff_text_size::{Ranged, TextRange};
use ruff_python_ast::{self as ast, Arguments, Constant, Expr, Stmt};
use ruff_source_file::Locator;
use ruff_text_size::Ranged;

use ruff_diagnostics::{Diagnostic, Edit, Fix, FixKind, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::whitespace;
use ruff_python_codegen::{Generator, Stylist};
use ruff_python_codegen::Stylist;

use crate::checkers::ast::Checker;
use crate::registry::{AsRule, Rule};
Expand Down Expand Up @@ -200,7 +201,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
first,
indentation,
checker.stylist(),
checker.generator(),
checker.locator(),
));
}
}
Expand All @@ -223,7 +224,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
first,
indentation,
checker.stylist(),
checker.generator(),
checker.locator(),
));
}
}
Expand All @@ -250,7 +251,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
first,
indentation,
checker.stylist(),
checker.generator(),
checker.locator(),
));
}
}
Expand Down Expand Up @@ -281,23 +282,13 @@ fn generate_fix(
exc_arg: &Expr,
indentation: &str,
stylist: &Stylist,
generator: Generator,
locator: &Locator,
) -> Fix {
let assignment = Stmt::Assign(ast::StmtAssign {
targets: vec![Expr::Name(ast::ExprName {
id: "msg".into(),
ctx: ExprContext::Store,
range: TextRange::default(),
})],
value: Box::new(exc_arg.clone()),
range: TextRange::default(),
});

Fix::suggested_edits(
Edit::insertion(
format!(
"{}{}{}",
generator.stmt(&assignment),
"msg = {}{}{}",
locator.slice(exc_arg.range()),
stylist.line_ending().as_str(),
indentation,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,20 @@ EM.py:60:35: EM101 Exception must not use a string literal, assign to variable f
|
= help: Assign to variable; remove string literal

EM.py:64:24: EM102 [*] Exception must not use an f-string literal, assign to variable first
|
63 | def f_triple_quoted_string():
64 | raise RuntimeError(f"""This is an {"example"} exception""")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM102
|
= help: Assign to variable; remove f-string literal

Suggested fix
61 61 |
62 62 |
63 63 | def f_triple_quoted_string():
64 |- raise RuntimeError(f"""This is an {"example"} exception""")
64 |+ msg = f"""This is an {"example"} exception"""
65 |+ raise RuntimeError(msg)


Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,20 @@ EM.py:60:35: EM101 Exception must not use a string literal, assign to variable f
|
= help: Assign to variable; remove string literal

EM.py:64:24: EM102 [*] Exception must not use an f-string literal, assign to variable first
|
63 | def f_triple_quoted_string():
64 | raise RuntimeError(f"""This is an {"example"} exception""")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM102
|
= help: Assign to variable; remove f-string literal

Suggested fix
61 61 |
62 62 |
63 63 | def f_triple_quoted_string():
64 |- raise RuntimeError(f"""This is an {"example"} exception""")
64 |+ msg = f"""This is an {"example"} exception"""
65 |+ raise RuntimeError(msg)


0 comments on commit b861379

Please sign in to comment.