Skip to content

Commit

Permalink
feat: add async requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
kuruk-mm committed Nov 22, 2023
1 parent 138e999 commit ba4428a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gdtoolkit/formatter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def main():
sys.stdout.reconfigure(encoding="utf-8")
arguments = docopt(
__doc__,
version="gdformat {}".format(
version="gdformat {} (kuruk-mm fork)".format(
pkg_resources.get_distribution("gdtoolkit").version
),
)
Expand Down
2 changes: 1 addition & 1 deletion gdtoolkit/gd2py/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def main():
sys.stdout.reconfigure(encoding="utf-8")
arguments = docopt(
__doc__,
version="gd2py {}".format(pkg_resources.get_distribution("gdtoolkit").version),
version="gd2py {} (kuruk-mm fork)".format(pkg_resources.get_distribution("gdtoolkit").version),
)
with open(arguments["<path>"], "r", encoding="utf-8") as handle:
print(convert_code(handle.read()))
2 changes: 1 addition & 1 deletion gdtoolkit/gdradon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def main():
sys.stdout.reconfigure(encoding="utf-8")
arguments = docopt(
__doc__,
version="gdradon {}".format(
version="gdradon {} (kuruk-mm fork)".format(
pkg_resources.get_distribution("gdtoolkit").version
),
)
Expand Down
3 changes: 3 additions & 0 deletions gdtoolkit/linter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"function-name": r"(_on_{}(_[a-z0-9]+)*|{})".format(
PASCAL_CASE, PRIVATE_SNAKE_CASE
),
"async-function-name": r"(_async_{}|async_{})".format(
SNAKE_CASE, SNAKE_CASE
),
"class-name": PASCAL_CASE,
"sub-class-name": r"_?{}".format(PASCAL_CASE),
"signal-name": SNAKE_CASE,
Expand Down
2 changes: 1 addition & 1 deletion gdtoolkit/linter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
def main():
arguments = docopt(
__doc__,
version="gdlint {}".format(pkg_resources.get_distribution("gdtoolkit").version),
version="gdlint {} (kuruk-mm fork)".format(pkg_resources.get_distribution("gdtoolkit").version),
)

if arguments["--verbose"]:
Expand Down
46 changes: 45 additions & 1 deletion gdtoolkit/linter/name_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

def lint(parse_tree: Tree, config: MappingProxyType) -> List[Problem]:
disable = config["disable"]

rule_name_tokens = _gather_rule_name_tokens(
parse_tree,
[
Expand All @@ -34,11 +35,29 @@ def lint(parse_tree: Tree, config: MappingProxyType) -> List[Problem]:
partial(
_generic_name_check,
config["function-name"],
rule_name_tokens["func_def"],
_get_functions(
parse_tree,
False,
["func_def"],
)["func_def"],
"function-name",
'Function name "{}" is not valid',
),
),
(
"async-function-name",
partial(
_generic_name_check,
config["async-function-name"],
_get_functions(
parse_tree,
True,
["func_def"],
)["func_def"],
"async-function-name",
'Async Function name "{}" is not valid',
),
),
(
"sub-class-name",
partial(
Expand Down Expand Up @@ -264,3 +283,28 @@ def _has_call_expr_name_in(tree: Tree, legal_names: List[str]) -> bool:
name = name_token.value
return name in legal_names
return False

def _has_await(parse_tree: Tree) -> bool:
for _ in parse_tree.find_data("await_expr"):
return True
return False

def _get_functions(
parse_tree: Tree, only_async_funcs: bool, rules
) -> Dict[str, List[str]]:
name_tokens_per_rule = {rule: [] for rule in rules} # type: Dict[str, List[str]]
for node in parse_tree.iter_subtrees():
if isinstance(node, Tree) and node.data in rules:
is_async = _has_await(node)

if only_async_funcs != is_async:
continue

rule_name = node.data
name_token = find_name_token_among_children(node)
if name_token is None:
name_token = find_name_token_among_children(node.children[0])
assert name_token is not None

name_tokens_per_rule[rule_name].append(name_token)
return name_tokens_per_rule
2 changes: 1 addition & 1 deletion gdtoolkit/parser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
def main():
arguments = docopt(
__doc__,
version="gdparse {}".format(
version="gdparse {} (kuruk-mm fork)".format(
pkg_resources.get_distribution("gdtoolkit").version
),
)
Expand Down
3 changes: 1 addition & 2 deletions tests/linter/test_basic_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .common import simple_ok_check, simple_nok_check


# fmt: off
@pytest.mark.parametrize('code', [
"""
Expand Down Expand Up @@ -31,7 +30,7 @@
'''docstr'''
""",
"""
func foo():
func async_foo():
await get_tree().create_timer(2.0).timeout
""",
"""
Expand Down
29 changes: 29 additions & 0 deletions tests/linter/test_name_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ def test_function_name_ok(code):
def test_function_name_nok(code):
simple_nok_check(code, 'function-name')

@pytest.mark.parametrize('code', [
"""
func async_some_button():
await 0
""",
"""
func _async_a_name():
await 0
func test_simple_func():
pass
""",
])
def test_async_function_name_ok(code):
simple_ok_check(code)

@pytest.mark.parametrize('code', [
"""
func _some_button():
await 0
""",
"""
func asynca_name():
await 0
""",
])
def test_function_name_nok(code):
simple_nok_check(code, 'async-function-name')


@pytest.mark.parametrize('code', [
"""
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ deps =
pytest > 5
hypothesis == 6.15.0
commands =
pytest -m "not generated" {posargs}
pytest -s -m "not generated" {posargs}

[testenv:lint]
deps =
Expand Down

0 comments on commit ba4428a

Please sign in to comment.