Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Commit

Permalink
Replace const-on-right with a more general rule
Browse files Browse the repository at this point in the history
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
jhauberg committed Apr 10, 2018
1 parent 547b21e commit 8492082
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 113 deletions.
4 changes: 2 additions & 2 deletions comply/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def make_rules(names: list, exceptions: list, is_strict: bool) -> list:
functions.FunctionTooLong(),
functions.TooManyFunctions(),
functions.NoRedundantName(),
functions.ConstOnRight(),
functions.NoAttachedStars(),
functions.NoRedundantSize(),
misc.IdentifierTooLong(),
Expand All @@ -118,7 +117,8 @@ def make_rules(names: list, exceptions: list, is_strict: bool) -> list:
misc.LineTooLong(),
misc.FileTooLong(),
misc.PreferStandardInt(),
misc.ScopeTooDeep()
misc.ScopeTooDeep(),
misc.ConstOnRight()
]

if len(names) > 0:
Expand Down
2 changes: 0 additions & 2 deletions comply/rules/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from comply.rules.functions.function_too_long import FunctionTooLong
from comply.rules.functions.too_many_functions import TooManyFunctions
from comply.rules.functions.no_redundant_name import NoRedundantName
from comply.rules.functions.const_on_right import ConstOnRight
from comply.rules.functions.no_attached_stars import NoAttachedStars
from comply.rules.functions.no_redundant_size import NoRedundantSize

Expand All @@ -17,7 +16,6 @@
"FunctionTooLong",
"TooManyFunctions",
"NoRedundantName",
"ConstOnRight",
"NoAttachedStars",
"NoRedundantSize"
]
108 changes: 0 additions & 108 deletions comply/rules/functions/const_on_right.py

This file was deleted.

4 changes: 3 additions & 1 deletion comply/rules/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from comply.rules.misc.no_todo import NoTodo
from comply.rules.misc.identifier_too_long import IdentifierTooLong
from comply.rules.misc.scope_too_deep import ScopeTooDeep
from comply.rules.misc.const_on_right import ConstOnRight

__all__ = [
"LineTooLong",
Expand All @@ -19,5 +20,6 @@
"PreferStandardInt",
"NoTodo",
"IdentifierTooLong",
"ScopeTooDeep"
"ScopeTooDeep",
"ConstOnRight"
]
63 changes: 63 additions & 0 deletions comply/rules/misc/const_on_right.py
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

0 comments on commit 8492082

Please sign in to comment.