Skip to content

Commit

Permalink
allow multiline expressions in f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
zsol committed Oct 1, 2023
1 parent e1da64b commit 9d3dd49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions native/libcst/src/tokenizer/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ impl<'t> TokState<'t> {
self.text_pos.next();
self.at_bol = true;
if self.split_fstring
&& !self.fstring_stack.iter().all(|node| node.allow_multiline())
&& self.fstring_stack.last().map(|node| node.allow_multiline())
== Some(false)
{
Err(TokError::UnterminatedString)
} else if self.blank_line || !self.paren_stack.is_empty() {
Expand Down Expand Up @@ -895,7 +896,8 @@ impl<'t> TokState<'t> {
is_in_format_spec: bool,
is_raw_string: bool,
) -> Result<Option<TokType>, TokError<'t>> {
let allow_multiline = self.fstring_stack.iter().all(|node| node.allow_multiline());
let allow_multiline =
self.fstring_stack.last().map(|node| node.allow_multiline()) == Some(true);
let mut in_named_unicode: bool = false;
let mut ok_result = Ok(None); // value to return if we reach the end and don't error out
'outer: loop {
Expand Down
2 changes: 1 addition & 1 deletion native/libcst/src/tokenizer/core/string_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl FStringNode {
}

pub fn allow_multiline(&self) -> bool {
self.quote_size == StringQuoteSize::Triple
self.quote_size == StringQuoteSize::Triple || self.is_in_expr()
}

pub fn is_in_expr(&self) -> bool {
Expand Down
12 changes: 11 additions & 1 deletion native/libcst/tests/fixtures/super_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@

print(f"{self.ERASE_CURRENT_LINE}{self._human_seconds(elapsed_time)} {percent:.{self.pretty_precision}f}% complete, {self.estimate_completion(elapsed_time, finished, left)} estimated for {left} files to go...")

f"{"\n".join()}"

f"___{
x
}___"

f"___{(
x
)}___"

f'\{{\}}'
f"regexp_like(path, '.*\{file_type}$')"
f"\lfoo"

f"{_:{_:}{a}}"
f"{_:{_:}{a}}"

0 comments on commit 9d3dd49

Please sign in to comment.