This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace const-on-right with a more general rule
This makes `const-on-right` catch more cases anywhere in a source file; not limited to function parameters. It's a better and (much) simpler solution that fixes issues found in #40 #41 A few downsides: * It has worse fix-it context output (can be mitigated) * Incorrectly reports the following as a violation: ``` int const a = 1; ``` This is because the pattern essentially just looks for `const`s with blank on its left.
- Loading branch information
Showing
5 changed files
with
68 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# coding=utf-8 | ||
|
||
import re | ||
|
||
from comply.rules import Rule, RuleViolation | ||
|
||
from comply.printing import Colors | ||
|
||
|
||
class ConstOnRight(Rule): | ||
def __init__(self): | ||
Rule.__init__(self, name='const-on-right', | ||
description='Prefer const qualifiers on the right', | ||
suggestion='Place const qualifier to the right of the type declaration.') | ||
|
||
# match both struct/enum and standard type declarations | ||
type_pattern = r'((?:struct|enum)\s+?.+?\b|.+?\b)' | ||
|
||
pattern = re.compile(r'(?:[\n;,(]|(?:static|typedef))\s*\b(const)\b\s+?' + type_pattern) | ||
|
||
def augment(self, violation: RuleViolation): | ||
line_number, line = violation.lines[0] | ||
|
||
from_index, to_index = violation.meta['range'] if 'range' in violation.meta else (0, 0) | ||
insertion_index = violation.meta['insertion'] if 'insertion' in violation.meta else 0 | ||
|
||
augmented_line = line | ||
|
||
augmented_line = (augmented_line[:insertion_index] + | ||
Colors.good + ' const' + Colors.clear + | ||
augmented_line[insertion_index:]) | ||
|
||
augmented_line = (augmented_line[:from_index] + | ||
Colors.bad + augmented_line[from_index:to_index] + Colors.clear + | ||
augmented_line[to_index:]) | ||
|
||
violation.lines[0] = (line_number, augmented_line) | ||
|
||
def collect(self, text: str, filename: str, extension: str): | ||
offenders = [] | ||
|
||
lines = text.splitlines() | ||
|
||
for match in self.pattern.finditer(text): | ||
line_number, column = RuleViolation.at(match.start(1), text) | ||
|
||
line = lines[line_number - 1] | ||
|
||
offending_index = column - 1 | ||
offending_range = (offending_index, offending_index + len(match.group(1))) | ||
|
||
type_line_number, type_column = RuleViolation.at(match.start(2), text) | ||
|
||
insertion_index = type_column - 1 + len(match.group(2)) | ||
|
||
offender = self.violate(at=(line_number, column), | ||
lines=[(line_number, line)], | ||
meta={'range': offending_range, | ||
'insertion': insertion_index}) | ||
|
||
offenders.append(offender) | ||
|
||
return offenders |