coala-analyzer /
coala
| 1 | from coalib.bearlib.spacing.SpacingHelper import SpacingHelper |
||
| 2 | from coalib.results.Diff import Diff |
||
| 3 | from coalib.bears.LocalBear import LocalBear |
||
| 4 | from coalib.results.Result import Result |
||
| 5 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 5 |
||
|
0 ignored issues
–
show
|
|||
| 6 | |||
| 7 | class SpaceConsistencyBear(LocalBear): |
||
| 8 | |||
| 9 | def run(self, |
||
| 10 | filename, |
||
| 11 | file, |
||
| 12 | use_spaces: bool, |
||
| 13 | allow_trailing_whitespace: bool=False, |
||
| 14 | tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH, |
||
| 15 | enforce_newline_at_EOF: bool=True): |
||
| 16 | ''' |
||
| 17 | Checks the space consistency for each line. |
||
| 18 | |||
| 19 | :param use_spaces: True if spaces are to be used instead |
||
| 20 | of tabs. |
||
| 21 | :param allow_trailing_whitespace: Whether to allow trailing whitespace |
||
| 22 | or not. |
||
| 23 | :param tab_width: Number of spaces representing one |
||
| 24 | tab. |
||
| 25 | :param enforce_newline_at_EOF: Whether to enforce a newline at the |
||
| 26 | End Of File. |
||
| 27 | ''' |
||
| 28 | spacing_helper = SpacingHelper(tab_width) |
||
| 29 | result_texts = [] |
||
| 30 | |||
| 31 | for line_number, line in enumerate(file, start=1): |
||
| 32 | replacement = line |
||
| 33 | |||
| 34 | if enforce_newline_at_EOF: |
||
| 35 | # Since every line contains at the end at least one \n, only |
||
| 36 | # the last line could potentially not have one. So we don't |
||
| 37 | # need to check whether the current line_number is the last |
||
| 38 | # one. |
||
| 39 | if replacement[-1] != "\n": |
||
| 40 | replacement += "\n" |
||
| 41 | result_texts.append("No newline at EOF.") |
||
| 42 | |||
| 43 | if not allow_trailing_whitespace: |
||
| 44 | replacement = replacement.rstrip(" \t\n") + "\n" |
||
| 45 | if replacement != line.rstrip("\n") + "\n": |
||
| 46 | result_texts.append("Trailing whitespaces.") |
||
| 47 | |||
| 48 | if use_spaces: |
||
| 49 | pre_replacement = replacement |
||
| 50 | replacement = spacing_helper.replace_tabs_with_spaces( |
||
| 51 | replacement) |
||
| 52 | if replacement != pre_replacement: |
||
| 53 | result_texts.append("Tabs used instead of spaces.") |
||
| 54 | else: |
||
| 55 | pre_replacement = replacement |
||
| 56 | replacement = spacing_helper.replace_spaces_with_tabs( |
||
| 57 | replacement) |
||
| 58 | if replacement != pre_replacement: |
||
| 59 | result_texts.append("Spaces used instead of tabs.") |
||
| 60 | |||
| 61 | if len(result_texts) > 0: |
||
| 62 | diff = Diff(file) |
||
| 63 | diff.change_line(line_number, line, replacement) |
||
| 64 | inconsistencies = "".join("\n- " + string |
||
| 65 | for string in result_texts) |
||
| 66 | yield Result.from_values( |
||
| 67 | self, |
||
| 68 | "Line contains following spacing inconsistencies:" |
||
| 69 | + inconsistencies, |
||
| 70 | diffs={filename: diff}, |
||
| 71 | file=filename, |
||
| 72 | line=line_number) |
||
| 73 | result_texts = [] |
||
| 74 |
This check looks for lines that are too long. You can specify the maximum line length.