Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _tree_invariant_check(input_code, formatted_code):
input_code_parse_tree = parser.parse(input_code)
formatted_code_parse_tree = parser.parse(formatted_code)
loosen_tree_transformer = LoosenTreeTransformer()
input_code_parse_tree = loosen_tree_transformer.transform(input_code_parse_tree)
formatted_code_parse_tree = loosen_tree_transformer.transform(
formatted_code_parse_tree
)
diff = "\n".join(
difflib.unified_diff(
str(input_code_parse_tree.pretty()).splitlines(),
str(formatted_code_parse_tree.pretty()).splitlines(),
)
)
assert input_code_parse_tree == formatted_code_parse_tree, diff
def test_parsing_failure(gdscript_nok_path):
with open(gdscript_nok_path, "r") as fh:
code = fh.read()
try:
parser.parse(code)
except: # pylint: disable=bare-except
return
raise Exception("shall fail")
def format_code(
gdscript_code: str,
max_line_length: int,
parse_tree: Optional[Tree] = None,
comment_parse_tree: Optional[Tree] = None,
) -> str:
parse_tree = (
parse_tree
if parse_tree is not None
else parser.parse(gdscript_code, gather_metadata=True)
)
comment_parse_tree = (
comment_parse_tree
if comment_parse_tree is not None
else parser.parse_comments(gdscript_code)
)
gdscript_code_lines = [
"",
*gdscript_code.splitlines(),
] # type: List[str]
formatted_lines = [] # type: FormattedLines
context = Context(
indent=0,
previously_processed_line_number=0,
max_line_length=max_line_length,
gdscript_code_lines=gdscript_code_lines,
def check_formatting_safety(
given_code: str,
formatted_code: str,
max_line_length: int,
given_code_parse_tree: Optional[Tree] = None,
given_code_comment_parse_tree: Optional[Tree] = None,
) -> None:
if given_code == formatted_code:
return
formatted_code_parse_tree = parser.parse(formatted_code, gather_metadata=True)
formatted_code_comment_parse_tree = parser.parse_comments(formatted_code)
check_comment_persistence(
given_code,
formatted_code,
given_code_comment_parse_tree=given_code_comment_parse_tree,
formatted_code_comment_parse_tree=formatted_code_comment_parse_tree,
)
check_tree_invariant(
given_code,
formatted_code,
given_code_parse_tree=given_code_parse_tree,
formatted_code_parse_tree=formatted_code_parse_tree,
)
check_formatting_stability(
formatted_code,
max_line_length,
def lint_code(
gdscript_code: str, config: MappingProxyType = DEFAULT_CONFIG
) -> List[Problem]:
parse_tree = parser.parse(gdscript_code, gather_metadata=True)
problems = design_checks.lint(parse_tree, config)
problems += format_checks.lint(gdscript_code, config)
problems += name_checks.lint(parse_tree, config)
problems += class_checks.lint(parse_tree, config)
problems += basic_checks.lint(parse_tree, config)
return problems
def check_tree_invariant(
given_code: str,
formatted_code: str,
given_code_parse_tree: Optional[Tree] = None,
formatted_code_parse_tree: Optional[Tree] = None,
) -> None:
given_code_parse_tree = (
given_code_parse_tree
if given_code_parse_tree is not None
else parser.parse(given_code)
)
formatted_code_parse_tree = (
formatted_code_parse_tree
if formatted_code_parse_tree is not None
else parser.parse(formatted_code)
)
loosen_tree_transformer = LoosenTreeTransformer()
given_code_parse_tree = loosen_tree_transformer.transform(given_code_parse_tree)
formatted_code_parse_tree = loosen_tree_transformer.transform(
formatted_code_parse_tree
)
if given_code_parse_tree != formatted_code_parse_tree:
raise TreeInvariantViolation
def check_tree_invariant(
given_code: str,
formatted_code: str,
given_code_parse_tree: Optional[Tree] = None,
formatted_code_parse_tree: Optional[Tree] = None,
) -> None:
given_code_parse_tree = (
given_code_parse_tree
if given_code_parse_tree is not None
else parser.parse(given_code)
)
formatted_code_parse_tree = (
formatted_code_parse_tree
if formatted_code_parse_tree is not None
else parser.parse(formatted_code)
)
loosen_tree_transformer = LoosenTreeTransformer()
given_code_parse_tree = loosen_tree_transformer.transform(given_code_parse_tree)
formatted_code_parse_tree = loosen_tree_transformer.transform(
formatted_code_parse_tree
)
if given_code_parse_tree != formatted_code_parse_tree:
raise TreeInvariantViolation
)
check_formatting_safety(
code,
formatted_code,
max_line_length=line_length,
given_code_parse_tree=code_parse_tree,
given_code_comment_parse_tree=comment_parse_tree,
)
print(formatted_code, end="")
elif arguments["--check"]:
formattable_files = set()
for file_path in files:
with open(file_path, "r") as fh:
code = fh.read()
try:
code_parse_tree = parser.parse(code, gather_metadata=True)
comment_parse_tree = parser.parse_comments(code)
formatted_code = format_code(
gdscript_code=code,
max_line_length=line_length,
parse_tree=code_parse_tree,
comment_parse_tree=comment_parse_tree,
)
except Exception as e:
print(
"exception during formatting of {}".format(file_path),
file=sys.stderr,
)
raise e
if code != formatted_code:
print("would reformat {}".format(file_path), file=sys.stderr)
try: