From 8469c13b708808bceac61f6ffd3e6fc1464a7280 Mon Sep 17 00:00:00 2001 From: Nfsaavedra Date: Sat, 1 Oct 2022 16:09:05 +0100 Subject: [PATCH] Fix get code in Ansible and Puppet. Fix long resource and improper alignment for Puppet --- glitch/analysis/design.py | 18 ++++++++++++++---- glitch/parsers/cmof.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/glitch/analysis/design.py b/glitch/analysis/design.py index 94e3876..7e01b42 100644 --- a/glitch/analysis/design.py +++ b/glitch/analysis/design.py @@ -26,14 +26,23 @@ def check(self, element, file: str): return [] class PuppetImproperAlignmentSmell(SmellChecker): + cached_file = "" + lines = [] + def check(self, element, file: str) -> list[Error]: + if DesignVisitor.PuppetImproperAlignmentSmell.cached_file != file: + with open(file, "r") as f: + DesignVisitor.PuppetImproperAlignmentSmell.lines = f.readlines() + DesignVisitor.PuppetImproperAlignmentSmell.cached_file = file + lines = DesignVisitor.PuppetImproperAlignmentSmell.lines + longest = 0 longest_ident = 0 longest_split = "" for a in element.attributes: if len(a.name) > longest and '=>' in a.code: longest = len(a.name) - split = a.code.split('=>')[0] + split = lines[a.line - 1].split('=>')[0] longest_ident = len(split) longest_split = split if longest_split == "": return [] @@ -42,7 +51,7 @@ def check(self, element, file: str) -> list[Error]: element, file, repr(element))] for a in element.attributes: - first_line = a.code.split("\n")[0] + first_line = lines[a.line - 1] cur_arrow_column = len(first_line.split('=>')[0]) if cur_arrow_column != longest_ident: return [Error('implementation_improper_alignment', @@ -298,8 +307,9 @@ def check_atomicunit(self, au: AtomicUnit, file: str) -> list[Error]: if au.type in DesignVisitor.__EXEC: lines = 0 - for line in au.code.split('\n'): - if line.strip() != "": lines += 1 + for attr in au.attributes: + for line in attr.code.split('\n'): + if line.strip() != "": lines += 1 if lines > 7: errors.append(Error("design_long_resource", au, file, repr(au))) diff --git a/glitch/parsers/cmof.py b/glitch/parsers/cmof.py index d2af863..c7b1a3c 100644 --- a/glitch/parsers/cmof.py +++ b/glitch/parsers/cmof.py @@ -75,6 +75,26 @@ def yaml_comments(d): return set(comments) + @staticmethod + def __get_element_code(start_token, end_token, code): + if isinstance(end_token, list) and len(end_token) > 0: + end_token = end_token[-1] + elif isinstance(end_token, list) or isinstance(end_token, str): + end_token = start_token + + if start_token.start_mark.line == end_token.end_mark.line: + res = code[start_token.start_mark.line][start_token.start_mark.column : end_token.end_mark.column] + else: + res = code[start_token.start_mark.line] + + for line in range(start_token.start_mark.line + 1, end_token.end_mark.line): + res += code[line] + + if start_token.start_mark.line != end_token.end_mark.line: + res += code[end_token.end_mark.line][:end_token.end_mark.column] + + return res + @staticmethod def __parse_vars(unit_block, cur_name, token, code): def create_variable(name, value): @@ -82,6 +102,7 @@ def create_variable(name, value): if (value in ["null", "~"]): value = "" v = Variable(name, value, has_variable) v.line = token.start_mark.line + 1 + v.code = AnsibleParser.__get_element_code(token, value, code) v.code = ''.join(code[token.start_mark.line : token.end_mark.line + 1]) unit_block.add_variable(v) @@ -111,7 +132,7 @@ def create_attribute(token, name, value): if (value in ["null", "~"]): value = "" a = Attribute(name, value, has_variable) a.line = token.start_mark.line + 1 - a.code = ''.join(code[token.start_mark.line : token.end_mark.line + 1]) + a.code = AnsibleParser.__get_element_code(token, val, code) attributes.append(a) attributes = [] @@ -966,7 +987,18 @@ def __process_unitblock_component(ce, unit_block: UnitBlock): @staticmethod def __process_codeelement(codeelement, path, code): def get_code(ce): - return ''.join(code[ce.line - 1 : ce.end_line]) + if ce.line == ce.end_line: + res = code[ce.line - 1][ce.col - 1 : ce.end_col - 1] + else: + res = code[ce.line - 1] + + for line in range(ce.line, ce.end_line - 1): + res += code[line] + + if ce.line != ce.end_line: + res += code[ce.end_line - 1][:ce.end_col - 1] + + return res if (isinstance(codeelement, puppetmodel.Value)): if isinstance(codeelement, puppetmodel.Hash):