Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parens around single import from /export statements #41

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ build-backend = "setuptools.build_meta"

[tool.poetry]
name = "mamushi"
version = "0.0.4-a2"
version = "0.0.4-a3"
description = "Vyper Formatter"
authors = ["benny <[email protected]>"]

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.0.4-a2
current_version = 0.0.4-a3
commit = False
tag = True
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion src/mamushi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.4-a2"
__version__ = "0.0.4-a3"
27 changes: 27 additions & 0 deletions src/mamushi/formatting/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
wrap_in_parentheses,
is_atom_with_invisible_parens,
ensure_visible,
is_lpar_token,
is_rpar_token,
)
from mamushi.formatting.strings import (
normalize_string_quotes,
Expand Down Expand Up @@ -238,6 +240,7 @@ def visit_call(self, node: Node) -> Iterator[Line]:

def visit_import(self, node: Node) -> Iterator[Line]:
"""Handles different import syntax"""
normalize_invisible_parens(node, parens_after={"import"})
for child in node.children:
if child.type in IMPORTS_TYPE and child.prev_sibling is None:
yield from self.line()
Expand All @@ -262,6 +265,8 @@ def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
normalize_invisible_parens(node, parens_after={"assert", ","})
elif node.type == tokens.RETURN_STMT:
normalize_invisible_parens(node, parens_after={"return"})
elif node.type == tokens.EXPORT:
normalize_invisible_parens(node, parens_after={"export", ":"})

is_body_like = node.parent and (
# TODO: handle this more cleanly
Expand Down Expand Up @@ -446,12 +451,34 @@ def normalize_invisible_parens(
):
wrap_in_parentheses(child, first_child, visible=False)

elif node.type in {tokens.IMPORT, tokens.EXPORT}:
_normalize_import_from(node, child, index)
break

elif not (isinstance(child, Leaf)):
wrap_in_parentheses(node, child, visible=False)

check_lpar = isinstance(child, Leaf) and (child.value in parens_after)


def _normalize_import_from(parent: Node, child: LN, index: int) -> None:
# "import from" nodes store parentheses directly as part of
# the statement
if (
is_lpar_token(child)
and isinstance(child, Leaf)
and isinstance(parent.children[-1], Leaf)
and is_rpar_token(parent.children[-1])
):
# make parentheses invisible
child.value = ""
parent.children[-1].value = ""
elif child.type != tokens.STAR:
# insert invisible parentheses
parent.insert_child(index, Leaf(tokens.LPAR, ""))
parent.append_child(Leaf(tokens.RPAR, ""))


def maybe_make_parens_invisible_in_atom(
node: LN,
parent: LN,
Expand Down
8 changes: 8 additions & 0 deletions src/mamushi/formatting/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def ensure_visible(leaf: Leaf) -> None:
leaf.value = ")"


def is_lpar_token(nl: LN) -> bool:
return nl.type == tokens.LPAR


def is_rpar_token(nl: LN) -> bool:
return nl.type == tokens.RPAR


def is_one_sequence_between(
opening: Leaf,
closing: Leaf,
Expand Down
9 changes: 9 additions & 0 deletions tests/data/imports/import_from_parens.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from a.c import (a)

from a.b import (
d
)
# output
from a.c import a

from a.b import d
5 changes: 5 additions & 0 deletions tests/data/modules/exports_parens.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports: (
abc.xyz
)
# output
exports: abc.xyz
Loading