I want to create a transformer that converts all quotes of f-strings from a single quote to triple quotes, but leaves nested f-strings intact.
For example, the next expression left intact.
f"""\
Hello {developer_name}
My name is {_get_machine(f"{self.prop_a}, {self.prop_b}")}
"""
But, the transformer result is:
f"""\
Hello {developer_name}
My name is {_get_machine(f"""{self.prop_a}, {self.prop_b}""")}
"""
I tried the following matchers but without success:
class _FormattedStringEscapingTransformer(m.MatcherDecoratableTransformer):
@m.call_if_not_inside(
m.FormattedString(
parts=m.OneOf(
m.FormattedStringExpression(expression=m.TypeOf(m.FormattedString))
)
)
)
@m.leave(m.FormattedString())
def escape_f_string(
self, original_node: cst.FormattedString, updated_node: cst.FormattedString
) -> cst.FormattedString:
return updated_node.with_changes(start='f"""', end='"""')
class _FormattedStringEscapingTransformer(m.MatcherDecoratableTransformer):
@m.call_if_not_inside(
m.FormattedString(
parts=m.OneOf(
m.FormattedStringExpression(
expression=m.Not(m.FormattedString(parts=m.DoNotCare()))
)
)
)
)
@m.leave(m.FormattedString())
def escape_f_string(
self, original_node: cst.FormattedString, updated_node: cst.FormattedString
) -> cst.FormattedString:
return updated_node.with_changes(start='f"""', end='"""')
None of them worked.
What is the correct matcher to exclude transformation if inner f-strings expressions?