Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _gather_rule_name_tokens(
parse_tree: Tree, rules, predicate=lambda _: True
) -> 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:
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])
predicate_outcome = predicate(node.children[0])
else:
predicate_outcome = predicate(node)
assert name_token is not None
if predicate_outcome:
name_tokens_per_rule[rule_name].append(name_token)
return name_tokens_per_rule
def _class_var_stmt_visibility(class_var_stmt) -> str:
some_var_stmt = class_var_stmt.children[0]
name_token = find_name_token_among_children(some_var_stmt)
return "pub" if is_function_public(name_token.value) else "prv"
def _has_load_or_preload_call_expr(tree: Tree) -> bool:
for child in tree.children:
if isinstance(child, Tree) and child.data == "expr":
expr = child
if (
len(expr.children) == 1
and isinstance(expr.children[0], Tree)
and expr.children[0].data == "standalone_call"
):
standalone_call = expr.children[0]
name_token = find_name_token_among_children(standalone_call)
assert name_token is not None
name = name_token.value
return name in ["load", "preload"]
return False
def _gather_rule_name_tokens(
parse_tree: Tree, rules, predicate=lambda _: True
) -> 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:
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])
predicate_outcome = predicate(node.children[0])
else:
predicate_outcome = predicate(node)
assert name_token is not None
if predicate_outcome:
name_tokens_per_rule[rule_name].append(name_token)
return name_tokens_per_rule
def _unused_argument_check(parse_tree: Tree) -> List[Problem]:
problems = []
for func_def in parse_tree.find_data("func_def"):
if (
isinstance(func_def.children[1], Tree)
and func_def.children[1].data == "func_args"
):
argument_definitions = {} # type: Dict[str, int]
argument_tokens = {}
func_args = func_def.children[1]
for func_arg in func_args.children:
arg_name_token = find_name_token_among_children(func_arg)
arg_name = arg_name_token.value
argument_definitions[arg_name] = (
argument_definitions.get(arg_name, 0) + 1
)
argument_tokens[arg_name] = arg_name_token
name_occurances = {} # type: Dict[str, int]
for xnode in func_def.iter_subtrees():
for node in xnode.children:
if isinstance(node, Token) and node.type == "NAME":
name = node.value
name_occurances[name] = name_occurances.get(name, 0) + 1
for argument in argument_definitions:
if argument_definitions[argument] == name_occurances[
argument
] and not argument.startswith("_"):
problems.append(
def __init__(self, func_def: Tree):
name_token = find_name_token_among_children(func_def)
self.name = name_token.value
def _load_data_from_class_def(self, class_def: Tree) -> None:
name_token = find_name_token_among_children(class_def)
self.name = name_token.value
self._load_data_from_node_children(class_def)