Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Jun 3, 2023
1 parent fcfd6ad commit c05e1b7
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions crates/ruff/src/rules/flynt/rules/static_join_to_fstring.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_text_size::TextRange;
use rustpython_parser::ast::{self, Expr, Ranged};
use rustpython_parser::ast::{self, Constant, Expr, Ranged};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -33,6 +33,7 @@ fn is_static_length(elts: &[Expr]) -> bool {
fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
let mut fstring_elems = Vec::with_capacity(joinees.len() * 2);
let mut first = true;
let mut only_strings = true;

for expr in joinees {
if matches!(expr, Expr::JoinedStr(_)) {
Expand All @@ -44,11 +45,29 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
fstring_elems.push(helpers::to_constant_string(joiner));
}
fstring_elems.push(helpers::to_fstring_elem(expr)?);

if !matches!(
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
})
) {
only_strings = false;
}
}

let node = ast::ExprJoinedStr {
values: fstring_elems,
range: TextRange::default(),
let node = if only_strings {
ast::Expr::Constant(ast::ExprConstant {
value: fstring_elems.iter().collect::<String>().into(),
range: TextRange::default(),
kind: None,
})
} else {
ast::ExprJoinedStr {
values: fstring_elems,
range: TextRange::default(),
}
};
Some(node.into())
}
Expand All @@ -58,7 +77,7 @@ pub(crate) fn static_join_to_fstring(checker: &mut Checker, expr: &Expr, joiner:
args,
keywords,
..
})= expr else {
}) = expr else {
return;
};

Expand Down

0 comments on commit c05e1b7

Please sign in to comment.