From 9479bc6fb16c68e2c6d15bdf9562bea48b3312ff Mon Sep 17 00:00:00 2001 From: Jade Abraham Date: Thu, 12 Sep 2024 11:49:16 -0700 Subject: [PATCH] add a lint rule for line length Signed-off-by: Jade Abraham --- tools/chplcheck/src/rules.py | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tools/chplcheck/src/rules.py b/tools/chplcheck/src/rules.py index 1bca2af9d86b..d9f6fbff091a 100644 --- a/tools/chplcheck/src/rules.py +++ b/tools/chplcheck/src/rules.py @@ -838,3 +838,47 @@ def unwrap_intermediate_block(node: AstNode) -> Optional[AstNode]: prev_depth = depth prev = child prev_line = line + + @driver.advanced_rule + def LineLength(context: Context, root: AstNode): + """ + Warn for lines that exceed 80 characters. + """ + + # TODO: this rule currently hardcode 80 characters + # we should make it configurable in the future + # but currently it's not possible to pass any configuration to rules + MAX_LINE_LENGTH = 80 + + if isinstance(root, Comment): + return True + + warned = set() + lines = None + for node in postorder(root): + if isinstance(node, Comment): + continue + + if lines is None: + lines = chapel.get_file_lines(context, node) + + loc = node.location() + lines_for_node = range(loc.start()[0], loc.end()[0] + 1) + for line_number in lines_for_node: + line = lines[line_number - 1] + if len(line) > MAX_LINE_LENGTH and line_number not in warned: + warned.add(line_number) + # TODO: ideally we would warn on the whole line, but we + # have to warn on a node + + # find the outermost node that starts on this line + outermost = node + while True: + parent = outermost.parent() + if parent is None: + break + if parent.location().start()[0] == line_number: + outermost = parent + else: + break + yield AdvancedRuleResult(outermost, data=line_number)