Skip to content

Commit

Permalink
Fix join not using returned node (#189)
Browse files Browse the repository at this point in the history
* Fix join not using returned node

* ignore security issues in tests
  • Loading branch information
ikamensh authored Jul 28, 2023
1 parent c573f8b commit 651c822
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ max-branches = 18
max-statements = 67

[tool.ruff.per-file-ignores]
"test/*" = ["I"]
"test/*" = ["I", "S"]
"test/integration/*" = [
"F523",
"F821",
Expand Down
2 changes: 1 addition & 1 deletion src/flynt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from old "%-formatted" and .format(...) strings into Python 3.6+'s f-strings.
Learn more about f-strings at https://www.python.org/dev/peps/pep-0498/"""

__version__ = "1.0.0"
__version__ = "1.0.1"

from flynt.cli import main

Expand Down
10 changes: 7 additions & 3 deletions src/flynt/static_join/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def visit_Call(self, node: ast.Call):
def transform_join(tree: ast.AST, *args, **kwargs) -> Tuple[str, bool]:

jt = JoinTransformer()
jt.visit(tree)
new_code = fixup_transformed(tree)
return new_code, jt.counter > 0
new_tree = jt.visit(tree)
changed = jt.counter > 0
if changed:
new_code = fixup_transformed(new_tree)
else:
new_code = ""
return new_code, changed
9 changes: 6 additions & 3 deletions src/flynt/string_concat/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def transform_concat(tree: ast.AST, *args, **kwargs) -> Tuple[str, bool]:

ft = ConcatTransformer()
new = ft.visit(tree)
new_code = fixup_transformed(new)

return new_code, ft.counter > 0
changed = ft.counter > 0
if changed:
new_code = fixup_transformed(new)
else:
new_code = ""
return new_code, changed
12 changes: 12 additions & 0 deletions src/flynt/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ def ast_string_node(string: str) -> ast.Str:
return ast.Str(s=string)


def check_is_string_node(tree: ast.AST):
"""Raise an exception is tree doesn't represent a string"""
if isinstance(tree, ast.Module):
tree = tree.body[0]
if isinstance(tree, ast.Expr):
tree = tree.value
assert isinstance(tree, (ast.JoinedStr, ast.Str)), f"found {type(tree)}"


def fixup_transformed(tree: ast.AST, quote_type: Optional[str] = None) -> str:
"""Given a transformed string / fstring ast node, transform it to a string."""
# check_is_string_node(tree)
il = FstrInliner()
il.visit(tree)
new_code = ast_to_string(tree)
Expand All @@ -112,6 +123,7 @@ def fixup_transformed(tree: ast.AST, quote_type: Optional[str] = None) -> str:
new_code = set_quote_type(new_code, quote_type)
new_code = new_code.replace("\n", "\\n")
new_code = new_code.replace("\t", "\\t")
# ast.parse(new_code)
return new_code


Expand Down
9 changes: 9 additions & 0 deletions test/test_edits.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,12 @@ def test_literal_direct(state: State):
out, count = code_editor.fstringify_code_by_line(s_in, state)
assert count == 1
assert out == expected_out


def test_joins():

s_in = """';'.join(['a', 'b', 'c'])"""
expected_out = '"a;b;c"'
out, count = code_editor.fstringify_static_joins(s_in, State())
assert count > 0
assert out == expected_out

0 comments on commit 651c822

Please sign in to comment.