Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a chplcheck rule for line length #25938

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tools/chplcheck/src/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading