From c303b28d23cb0fe21fb4792ecc9ec65a21f8a127 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Mon, 27 Jul 2020 17:53:34 -0500 Subject: [PATCH] Updates for issue #393: Refactoring rules: 1) insert blank line base rules refactored 2) refactored context_023 and context_025 --- vsg/rule_item.py | 2 + vsg/rules/context/rule_023.py | 2 + vsg/rules/context/rule_025.py | 2 + ...nk_line_above_line_containing_item_rule.py | 2 - ...nk_line_below_line_containing_item_rule.py | 39 +++++++++---------- vsg/tests/context/test_rule_023.py | 5 ++- vsg/tests/context/test_rule_025.py | 5 ++- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/vsg/rule_item.py b/vsg/rule_item.py index c9f93059..b50b4740 100644 --- a/vsg/rule_item.py +++ b/vsg/rule_item.py @@ -9,6 +9,8 @@ class Rule(rule.rule): def __init__(self, name, identifier): rule.rule.__init__(self, name, identifier) + self.regionBegin = None + self.regionEnd = None @abstractmethod def _get_regions(self, oFile): diff --git a/vsg/rules/context/rule_023.py b/vsg/rules/context/rule_023.py index 9b469b5a..c5568b46 100644 --- a/vsg/rules/context/rule_023.py +++ b/vsg/rules/context/rule_023.py @@ -11,3 +11,5 @@ class rule_023(insert_blank_line_below_line_containing_item_rule): def __init__(self): insert_blank_line_below_line_containing_item_rule.__init__(self, 'context', '023', parser.context_is_keyword) + self.regionBegin = parser.context_keyword + self.regionEnd = parser.context_semicolon diff --git a/vsg/rules/context/rule_025.py b/vsg/rules/context/rule_025.py index dce9cb76..1f106356 100644 --- a/vsg/rules/context/rule_025.py +++ b/vsg/rules/context/rule_025.py @@ -11,3 +11,5 @@ class rule_025(insert_blank_line_below_line_containing_item_rule): def __init__(self): insert_blank_line_below_line_containing_item_rule.__init__(self, 'context', '025', parser.context_semicolon) + self.regionBegin = parser.context_keyword + self.regionEnd = parser.context_semicolon diff --git a/vsg/rules/insert_blank_line_above_line_containing_item_rule.py b/vsg/rules/insert_blank_line_above_line_containing_item_rule.py index 2d484e5c..fea22b75 100644 --- a/vsg/rules/insert_blank_line_above_line_containing_item_rule.py +++ b/vsg/rules/insert_blank_line_above_line_containing_item_rule.py @@ -28,8 +28,6 @@ class insert_blank_line_above_line_containing_item_rule(rule_item.Rule): self.trigger = trigger self.allowComment = allowComment self.configuration.append('allowComment') - self.regionBegin = None - self.regionEnd = None def _get_regions(self, oFile): return oFile.get_region_bounded_by_items(self.regionBegin, self.regionEnd) diff --git a/vsg/rules/insert_blank_line_below_line_containing_item_rule.py b/vsg/rules/insert_blank_line_below_line_containing_item_rule.py index 2072ce29..ab3a4f07 100644 --- a/vsg/rules/insert_blank_line_below_line_containing_item_rule.py +++ b/vsg/rules/insert_blank_line_below_line_containing_item_rule.py @@ -1,10 +1,10 @@ from vsg import fix -from vsg import rule +from vsg import rule_item from vsg import utils -class insert_blank_line_below_line_containing_item_rule(rule.rule): +class insert_blank_line_below_line_containing_item_rule(rule_item.Rule): ''' Checks for blank lines below a line and will insert a blank line if one does not exist. @@ -22,25 +22,24 @@ class insert_blank_line_below_line_containing_item_rule(rule.rule): ''' def __init__(self, name, identifier, trigger): - rule.rule.__init__(self, name=name, identifier=identifier) - self.solution = 'Insert blank line below.' + rule_item.Rule.__init__(self, name=name, identifier=identifier) self.phase = 3 self.trigger = trigger - def analyze(self, oFile): - self._print_debug_message('Analyzing rule: ' + self.name + '_' + self.identifier) - lContexts = oFile.get_context_declarations() - for dContext in lContexts: - for iLine, oLine in enumerate(dContext['lines']): - try: - oNextLine = oFile.lines[dContext['metadata']['iStartLineNumber'] + iLine + 1] - except IndexError: - break - lObjects = oLine.get_objects() - for oObject in lObjects: - if isinstance(oObject, self.trigger) and not oNextLine.is_blank(): - self.add_violation(utils.create_violation_dict(dContext['metadata']['iStartLineNumber'] + iLine)) + def _get_regions(self, oFile): + return oFile.get_region_bounded_by_items(self.regionBegin, self.regionEnd) + + def _analyze_region(self, oFile, iLine, oLine, dRegion): + try: + oNextLine = oFile.lines[dRegion['metadata']['iStartLineNumber'] + iLine + 1] + except IndexError: + return + lObjects = oLine.get_objects() + for oObject in lObjects: + if isinstance(oObject, self.trigger) and not oNextLine.is_blank(): + dViolation = utils.create_violation_dict(dRegion['metadata']['iStartLineNumber'] + iLine) + dViolation['solution'] = 'Insert blank line below.' + self.add_violation(dViolation) - def _fix_violations(self, oFile): - for dViolation in self.violations[::-1]: - fix.insert_blank_line_below(self, oFile, utils.get_violation_line_number(dViolation)) + def _fix_violation(self, oFile, dViolation): + fix.insert_blank_line_below(self, oFile, utils.get_violation_line_number(dViolation)) diff --git a/vsg/tests/context/test_rule_023.py b/vsg/tests/context/test_rule_023.py index bf1cbd75..5e80e704 100644 --- a/vsg/tests/context/test_rule_023.py +++ b/vsg/tests/context/test_rule_023.py @@ -25,9 +25,10 @@ class test_context_rule(unittest.TestCase): self.assertEqual(oRule.name, 'context') self.assertEqual(oRule.identifier, '023') - dExpected = utils.add_violation_list([7]) + lExpected = [7] + oRule.analyze(self.oFile) - self.assertEqual(oRule.violations, dExpected) + self.assertEqual(lExpected, utils.extract_violation_lines(oRule.violations)) self.assertEqual('Insert blank line below.', oRule._get_solution(7)) diff --git a/vsg/tests/context/test_rule_025.py b/vsg/tests/context/test_rule_025.py index 5337984c..21550b7e 100644 --- a/vsg/tests/context/test_rule_025.py +++ b/vsg/tests/context/test_rule_025.py @@ -25,9 +25,10 @@ class test_context_rule(unittest.TestCase): self.assertEqual(oRule.name, 'context') self.assertEqual(oRule.identifier, '025') - dExpected = utils.add_violation_list([8]) + lExpected = [8] + oRule.analyze(self.oFile) - self.assertEqual(oRule.violations, dExpected) + self.assertEqual(lExpected, utils.extract_violation_lines(oRule.violations)) self.assertEqual('Insert blank line below.', oRule._get_solution(8))