Refactoring case rules into individual files to support maintainability.
This commit is contained in:
parent
b5d0803a7e
commit
8364cbd3f7
|
|
@ -1,252 +0,0 @@
|
|||
|
||||
from vsg import rule
|
||||
import re
|
||||
|
||||
|
||||
class case_rule(rule.rule):
|
||||
|
||||
def __init__(self):
|
||||
rule.rule.__init__(self)
|
||||
self.name = 'case'
|
||||
|
||||
|
||||
class rule_001(case_rule):
|
||||
'''Case rule 001 checks for the proper indentation at the beginning of the line.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '001'
|
||||
self.solution = 'Ensure proper indentation.'
|
||||
self.phase = 4
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseKeyword or oLine.isCaseWhenKeyword or oLine.isEndCaseKeyword:
|
||||
self._check_indent(oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._fix_indent(oFile.lines[iLineNumber])
|
||||
|
||||
|
||||
class rule_002(case_rule):
|
||||
'''Case rule 002 checks for a single space after the "case" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '002'
|
||||
self.solution = 'Ensure a single space exists after the "case" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseKeyword:
|
||||
if not re.match('^\s*case\s\S', oLine.lineLower):
|
||||
self.add_violation(iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'case')
|
||||
|
||||
|
||||
class rule_003(case_rule):
|
||||
'''Case rule 003 checks for a single space before the "is" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '003'
|
||||
self.solution = 'Ensure a single space exists before the "is" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseIsKeyword:
|
||||
self._is_single_space_before('is', oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_before_word(oFile.lines[iLineNumber], 'is')
|
||||
|
||||
|
||||
class rule_004(case_rule):
|
||||
'''Case rule 004 checks for a single space after the "when" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '004'
|
||||
self.solution = 'Ensure a single space exists after the "when" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenKeyword:
|
||||
self._is_single_space_after('when', oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'when')
|
||||
|
||||
|
||||
class rule_005(case_rule):
|
||||
'''Case rule 005 checks for a single space before the "=>" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '005'
|
||||
self.solution = 'Ensure a single space exists before the "=>" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenEnd:
|
||||
self._is_single_space_before('=>', oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_before_word(oFile.lines[iLineNumber], '=>')
|
||||
|
||||
|
||||
class rule_006(case_rule):
|
||||
'''Case rule 006 checks for a single space between the "end" and "case" keywords.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '006'
|
||||
self.solution = 'Ensure a single space exists between the "end" and "case" keywords.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isEndCaseKeyword:
|
||||
if not re.match('^\s*end\scase', oLine.lineLower):
|
||||
self.add_violation(iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'end')
|
||||
|
||||
|
||||
class rule_007(case_rule):
|
||||
'''Case rule 007 ensures a blank line exists before the "case" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '007'
|
||||
self.solution = 'Ensure a blank line exists before the "case" keyword.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseKeyword:
|
||||
self._is_blank_line_before(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_above(oFile, iLineNumber)
|
||||
|
||||
|
||||
class rule_008(case_rule):
|
||||
'''Case rule 008 ensures a blank line exists below the "case" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '008'
|
||||
self.solution = 'Ensure a blank line exists below the "case" keyword.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseIsKeyword:
|
||||
self._is_blank_line_after(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_below(oFile, iLineNumber)
|
||||
|
||||
|
||||
class rule_009(case_rule):
|
||||
'''Case rule 009 ensures a blank line exists above the "end case" keywords.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '009'
|
||||
self.solution = 'Ensure a blank line exists above the "end case" keywords.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isEndCaseKeyword:
|
||||
self._is_blank_line_before(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_above(oFile, iLineNumber)
|
||||
|
||||
|
||||
class rule_010(case_rule):
|
||||
'''Case rule 010 ensures a blank line exists below the "end case" keywords.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '010'
|
||||
self.solution = 'Ensure a blank line exists below the "end case" keywords.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isEndCaseKeyword:
|
||||
self._is_blank_line_after(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_below(oFile, iLineNumber)
|
||||
|
||||
|
||||
class rule_011(case_rule):
|
||||
'''Case rule 011 ensures the alignment of multiline "when" statements.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '011'
|
||||
self.solution = 'Align with space after the "when" keyword.'
|
||||
self.phase = 5
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenKeyword and oLine.isCaseWhenEnd:
|
||||
continue
|
||||
if oLine.isCaseWhenKeyword:
|
||||
iAlignmentColumn = (oLine.indentLevel * self.indentSize) + len('when ')
|
||||
continue
|
||||
if oLine.insideCaseWhen:
|
||||
self._check_multiline_alignment(iAlignmentColumn, oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.dFix['violations']:
|
||||
self._fix_multiline_alignment(oFile, iLineNumber)
|
||||
|
||||
|
||||
class rule_012(case_rule):
|
||||
'''Case rule 012 ensures code does not exist after the => operator.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '012'
|
||||
self.solution = 'Move code after the => operator to it\'s own line.'
|
||||
self.phase = 1
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenEnd:
|
||||
if re.match('^.*=>\s*\w', oLine.line):
|
||||
self.add_violation(iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
oFile.lines.insert(iLineNumber + 1, oFile.lines[iLineNumber])
|
||||
oLine = oFile.lines[iLineNumber]
|
||||
iIndex = oLine.line.find('=>') + len('=>') + 1
|
||||
oLine.update_line(oLine.line[:iIndex])
|
||||
oLine = oFile.lines[iLineNumber + 1]
|
||||
oLine.update_line(oLine.line[iIndex:])
|
||||
oLine.isCaseWhenEnd = False
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
from vsg import rule
|
||||
|
||||
class case_rule(rule.rule):
|
||||
|
||||
def __init__(self):
|
||||
rule.rule.__init__(self)
|
||||
self.name = 'case'
|
||||
|
||||
|
||||
from rule_001 import rule_001
|
||||
from rule_002 import rule_002
|
||||
from rule_003 import rule_003
|
||||
from rule_004 import rule_004
|
||||
from rule_005 import rule_005
|
||||
from rule_006 import rule_006
|
||||
from rule_007 import rule_007
|
||||
from rule_008 import rule_008
|
||||
from rule_009 import rule_009
|
||||
from rule_010 import rule_010
|
||||
from rule_011 import rule_011
|
||||
from rule_012 import rule_012
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_001(case_rule):
|
||||
'''Case rule 001 checks for the proper indentation at the beginning of the line.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '001'
|
||||
self.solution = 'Ensure proper indentation.'
|
||||
self.phase = 4
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseKeyword or oLine.isCaseWhenKeyword or oLine.isEndCaseKeyword:
|
||||
self._check_indent(oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._fix_indent(oFile.lines[iLineNumber])
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
import re
|
||||
|
||||
class rule_002(case_rule):
|
||||
'''Case rule 002 checks for a single space after the "case" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '002'
|
||||
self.solution = 'Ensure a single space exists after the "case" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseKeyword:
|
||||
if not re.match('^\s*case\s\S', oLine.lineLower):
|
||||
self.add_violation(iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'case')
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_003(case_rule):
|
||||
'''Case rule 003 checks for a single space before the "is" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '003'
|
||||
self.solution = 'Ensure a single space exists before the "is" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseIsKeyword:
|
||||
self._is_single_space_before('is', oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_before_word(oFile.lines[iLineNumber], 'is')
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_004(case_rule):
|
||||
'''Case rule 004 checks for a single space after the "when" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '004'
|
||||
self.solution = 'Ensure a single space exists after the "when" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenKeyword:
|
||||
self._is_single_space_after('when', oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'when')
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_005(case_rule):
|
||||
'''Case rule 005 checks for a single space before the "=>" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '005'
|
||||
self.solution = 'Ensure a single space exists before the "=>" keyword.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenEnd:
|
||||
self._is_single_space_before('=>', oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_before_word(oFile.lines[iLineNumber], '=>')
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
import re
|
||||
|
||||
class rule_006(case_rule):
|
||||
'''Case rule 006 checks for a single space between the "end" and "case" keywords.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '006'
|
||||
self.solution = 'Ensure a single space exists between the "end" and "case" keywords.'
|
||||
self.phase = 2
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isEndCaseKeyword:
|
||||
if not re.match('^\s*end\scase', oLine.lineLower):
|
||||
self.add_violation(iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations:
|
||||
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'end')
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_007(case_rule):
|
||||
'''Case rule 007 ensures a blank line exists before the "case" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '007'
|
||||
self.solution = 'Ensure a blank line exists before the "case" keyword.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseKeyword:
|
||||
self._is_blank_line_before(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_above(oFile, iLineNumber)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_008(case_rule):
|
||||
'''Case rule 008 ensures a blank line exists below the "case" keyword.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '008'
|
||||
self.solution = 'Ensure a blank line exists below the "case" keyword.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseIsKeyword:
|
||||
self._is_blank_line_after(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_below(oFile, iLineNumber)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_009(case_rule):
|
||||
'''Case rule 009 ensures a blank line exists above the "end case" keywords.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '009'
|
||||
self.solution = 'Ensure a blank line exists above the "end case" keywords.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isEndCaseKeyword:
|
||||
self._is_blank_line_before(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_above(oFile, iLineNumber)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_010(case_rule):
|
||||
'''Case rule 010 ensures a blank line exists below the "end case" keywords.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '010'
|
||||
self.solution = 'Ensure a blank line exists below the "end case" keywords.'
|
||||
self.phase = 3
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isEndCaseKeyword:
|
||||
self._is_blank_line_after(oFile, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
self._insert_blank_line_below(oFile, iLineNumber)
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
|
||||
|
||||
class rule_011(case_rule):
|
||||
'''Case rule 011 ensures the alignment of multiline "when" statements.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '011'
|
||||
self.solution = 'Align with space after the "when" keyword.'
|
||||
self.phase = 5
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenKeyword and oLine.isCaseWhenEnd:
|
||||
continue
|
||||
if oLine.isCaseWhenKeyword:
|
||||
iAlignmentColumn = (oLine.indentLevel * self.indentSize) + len('when ')
|
||||
continue
|
||||
if oLine.insideCaseWhen:
|
||||
self._check_multiline_alignment(iAlignmentColumn, oLine, iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.dFix['violations']:
|
||||
self._fix_multiline_alignment(oFile, iLineNumber)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
from vsg.rules.case import case_rule
|
||||
import re
|
||||
|
||||
class rule_012(case_rule):
|
||||
'''Case rule 012 ensures code does not exist after the => operator.'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self)
|
||||
self.identifier = '012'
|
||||
self.solution = 'Move code after the => operator to it\'s own line.'
|
||||
self.phase = 1
|
||||
|
||||
def analyze(self, oFile):
|
||||
for iLineNumber, oLine in enumerate(oFile.lines):
|
||||
if oLine.isCaseWhenEnd:
|
||||
if re.match('^.*=>\s*\w', oLine.line):
|
||||
self.add_violation(iLineNumber)
|
||||
|
||||
def _fix_violations(self, oFile):
|
||||
for iLineNumber in self.violations[::-1]:
|
||||
oFile.lines.insert(iLineNumber + 1, oFile.lines[iLineNumber])
|
||||
oLine = oFile.lines[iLineNumber]
|
||||
iIndex = oLine.line.find('=>') + len('=>') + 1
|
||||
oLine.update_line(oLine.line[:iIndex])
|
||||
oLine = oFile.lines[iLineNumber + 1]
|
||||
oLine.update_line(oLine.line[iIndex:])
|
||||
oLine.isCaseWhenEnd = False
|
||||
Loading…
Reference in New Issue