From 0d228d00a87b098e83af697d4aa49fba889629bf Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Sun, 31 Jan 2021 13:43:35 -0600 Subject: [PATCH] Updates for issue #450: Adding support for open_paren_new_line option. 1) Added tests 2) Added code --- vsg/rules/constant/rule_012.py | 26 ++++++++++++++++++++++++++ vsg/tests/constant/test_rule_012a.py | 26 ++++++++++++++++++++++++++ vsg/vhdlFile/utils.py | 13 +++++++++++++ 3 files changed, 65 insertions(+) diff --git a/vsg/rules/constant/rule_012.py b/vsg/rules/constant/rule_012.py index 00293334..3472b4e3 100644 --- a/vsg/rules/constant/rule_012.py +++ b/vsg/rules/constant/rule_012.py @@ -71,6 +71,7 @@ class rule_012(rule.Rule): _check_first_paren_new_line(self, oToi) _check_last_paren_new_line(self, oToi) + _check_open_paren_new_line(self, oToi) # for oNewToi in lToi: @@ -245,6 +246,31 @@ def _check_last_paren_new_line(self, oToi): break + +def _check_open_paren_new_line(self, oToi): + + if self.open_paren_new_line == 'Ignore': + return + + iLine, lTokens = utils.get_toi_parameters(oToi) + + for iToken, oToken in enumerate(lTokens): + iLine = utils.increment_line_number(iLine, oToken) + if isinstance(oToken, token.constant_declaration.assignment_operator): + bSearch = True + if isinstance(oToken, parser.open_parenthesis) and bSearch: + if utils.is_token_at_end_of_line(iToken, lTokens): + if not self.open_paren_new_line: + iEnd = utils.find_carriage_return(lTokens, iToken) + sSolution = 'Remove carriage return after open parenthesis.' + oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iEnd), sSolution) + self.add_violation(oViolation) + else: + if self.open_paren_new_line: + sSolution = 'Add carriage return after open parenthesis.' + oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iToken), sSolution) + self.add_violation(oViolation) + def _is_open_paren_after_assignment(oToi): diff --git a/vsg/tests/constant/test_rule_012a.py b/vsg/tests/constant/test_rule_012a.py index fa0fa10e..bf4fa22d 100644 --- a/vsg/tests/constant/test_rule_012a.py +++ b/vsg/tests/constant/test_rule_012a.py @@ -25,6 +25,7 @@ class test_constant_rule(unittest.TestCase): oRule = constant.rule_012() oRule.first_paren_new_line = True oRule.last_paren_new_line = 'Ignore' + oRule.open_paren_new_line = 'Ignore' self.assertTrue(oRule) self.assertEqual(oRule.name, 'constant') self.assertEqual(oRule.identifier, '012') @@ -38,6 +39,7 @@ class test_constant_rule(unittest.TestCase): oRule = constant.rule_012() oRule.first_paren_new_line = False oRule.last_paren_new_line = 'Ignore' + oRule.open_paren_new_line = 'Ignore' lExpected = [14, 17, 21, 27, 41, 57] @@ -48,6 +50,7 @@ class test_constant_rule(unittest.TestCase): oRule = constant.rule_012() oRule.first_paren_new_line = 'Ignore' oRule.last_paren_new_line = True + oRule.open_paren_new_line = 'Ignore' lExpected = [11, 14] @@ -58,12 +61,35 @@ class test_constant_rule(unittest.TestCase): oRule = constant.rule_012() oRule.first_paren_new_line = 'Ignore' oRule.last_paren_new_line = False + oRule.open_paren_new_line = 'Ignore' lExpected = [18, 24, 38, 54, 72] oRule.analyze(self.oFile) self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + def test_rule_012_open_paren_new_line_true(self): + oRule = constant.rule_012() + oRule.first_paren_new_line = 'Ignore' + oRule.last_paren_new_line = 'Ignore' + oRule.open_paren_new_line = True + + lExpected = [11, 11, 14, 14, 14, 17, 17, 17, 22, 23, 28, 33] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + + def test_rule_012_open_paren_new_line_false(self): + oRule = constant.rule_012() + oRule.first_paren_new_line = 'Ignore' + oRule.last_paren_new_line = 'Ignore' + oRule.open_paren_new_line = False + + lExpected = [10, 21, 27, 41, 42, 48, 57, 58, 65] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + # def test_fix_rule_012(self): # oRule = constant.rule_012() # diff --git a/vsg/vhdlFile/utils.py b/vsg/vhdlFile/utils.py index 6296b637..a8d1823b 100644 --- a/vsg/vhdlFile/utils.py +++ b/vsg/vhdlFile/utils.py @@ -638,3 +638,16 @@ def read_vhdlfile(sFileName): return lLines, None except OSError as e: return [], e + + +def is_token_at_end_of_line(iToken, lTokens): + iMyToken = iToken + 1 + if are_next_consecutive_token_types([parser.carriage_return], iMyToken, lTokens): + return True + if are_next_consecutive_token_types([parser.whitespace, parser.carriage_return], iMyToken, lTokens): + return True + if are_next_consecutive_token_types([parser.comment, parser.carriage_return], iMyToken, lTokens): + return True + if are_next_consecutive_token_types([parser.whitespace, parser.comment, parser.carriage_return], iMyToken, lTokens): + return True + return False