From b906965dccb2e3ff8b507c9b099cc7937aad3377 Mon Sep 17 00:00:00 2001 From: Josselin Date: Tue, 11 Dec 2018 14:22:37 -0500 Subject: [PATCH] Update naming convention to new json format --- .../naming_convention/naming_convention.py | 116 ++++++++++-------- .../naming_convention.naming-convention.json | 2 +- 2 files changed, 64 insertions(+), 54 deletions(-) diff --git a/slither/detectors/naming_convention/naming_convention.py b/slither/detectors/naming_convention/naming_convention.py index 37602c86b..6acf34098 100644 --- a/slither/detectors/naming_convention/naming_convention.py +++ b/slither/detectors/naming_convention/naming_convention.py @@ -52,11 +52,12 @@ class NamingConvention(AbstractDetector): contract.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'contract', - 'convention':'CapWords', - 'name':{'name': contract.name, - 'source_mapping': contract.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'contract' + json['convention'] = 'CapWords' + json['name'] = {'name': contract.name, + 'source_mapping': contract.source_mapping} + results.append(json) for struct in contract.structures: if struct.contract != contract: @@ -67,13 +68,12 @@ class NamingConvention(AbstractDetector): info = info.format(struct.contract.name, struct.name, struct.source_mapping_str) all_info += info - - results.append({'check': self.ARGUMENT, - 'type': 'structure', - 'convention':'CapWords', - 'name':{'name': struct.name, - 'source_mapping': struct.source_mapping}}) - + json = self.generate_json_result(info) + json['type'] = 'structure' + json['convention'] = 'CapWords' + json['name'] = {'name': struct.name, + 'source_mapping': struct.source_mapping} + results.append(json) for event in contract.events: if event.contract != contract: continue @@ -83,12 +83,12 @@ class NamingConvention(AbstractDetector): info = info.format(event.contract.name, event.name, event.source_mapping_str) all_info += info - - results.append({'check': self.ARGUMENT, - 'type': 'event', - 'convention':'CapWords', - 'name':{'name': event.name, - 'source_mapping': event.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'event' + json['convention'] = 'CapWords' + json['name'] = {'name': event.name, + 'source_mapping': event.source_mapping} + results.append(json) for func in contract.functions: if func.contract != contract: @@ -99,11 +99,12 @@ class NamingConvention(AbstractDetector): info = info.format(func.contract.name, func.name, func.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'function', - 'convention':'mixedCase', - 'name':{'name': func.name, - 'source_mapping': func.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'function' + json['convention'] = 'mixedCase' + json['name'] = {'name': func.name, + 'source_mapping': func.source_mapping} + results.append(json) for argument in func.parameters: if argument in func.variables_read_or_written: @@ -118,11 +119,12 @@ class NamingConvention(AbstractDetector): argument.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'parameter', - 'convention':'mixedCase', - 'name':{'name': argument.name, - 'source_mapping': argument.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'parameter' + json['convention'] = 'mixedCase' + json['name'] = {'name': argument.name, + 'source_mapping': argument.source_mapping} + results.append(json) for var in contract.state_variables: if var.contract != contract: @@ -134,11 +136,12 @@ class NamingConvention(AbstractDetector): info = info.format(var.contract.name, var.name, var.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'variable', - 'convention':'l_O_I_should_not_be_used', - 'name':{'name': var.name, - 'source_mapping': var.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'variable' + json['convention'] = 'l_O_I_should_not_be_used' + json['name'] = {'name': var.name, + 'source_mapping': var.source_mapping} + results.append(json) if var.is_constant is True: # For ERC20 compatibility @@ -150,11 +153,13 @@ class NamingConvention(AbstractDetector): info = info.format(var.contract.name, var.name, var.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'variable_constant', - 'convention':'UPPER_CASE_WITH_UNDERSCORES', - 'name':{'name': var.name, - 'source_mapping': var.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'variable_constant' + json['convention'] = 'UPPER_CASE_WITH_UNDERSCORES' + json['name'] = {'name': var.name, + 'source_mapping': var.source_mapping} + results.append(json) + else: if var.visibility == 'private': correct_naming = self.is_mixed_case_with_underscore(var.name) @@ -165,11 +170,12 @@ class NamingConvention(AbstractDetector): info = info.format(var.contract.name, var.name, var.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'variable', - 'convention':'mixedCase', - 'name':{'name': var.name, - 'source_mapping': var.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'variable' + json['convention'] = 'mixedCase' + json['name'] = {'name': var.name, + 'source_mapping': var.source_mapping} + results.append(json) for enum in contract.enums: if enum.contract != contract: @@ -180,11 +186,13 @@ class NamingConvention(AbstractDetector): info = info.format(enum.contract.name, enum.name, enum.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'enum', - 'convention':'CapWords', - 'name':{'name': enum.name, - 'source_mapping': enum.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'enum' + json['convention'] = 'CapWords' + json['name'] = {'name': enum.name, + 'source_mapping': enum.source_mapping} + results.append(json) + for modifier in contract.modifiers: if modifier.contract != contract: @@ -197,11 +205,13 @@ class NamingConvention(AbstractDetector): modifier.source_mapping_str) all_info += info - results.append({'check': self.ARGUMENT, - 'type': 'modifier', - 'convention':'mixedCase', - 'name':{'name': modifier.name, - 'source_mapping': modifier.source_mapping}}) + json = self.generate_json_result(info) + json['type'] = 'modifier' + json['convention'] = 'mixedCase' + json['name'] = {'name': modifier.name, + 'source_mapping': modifier.source_mapping} + results.append(json) + if all_info != '': self.log(all_info) diff --git a/tests/expected_json/naming_convention.naming-convention.json b/tests/expected_json/naming_convention.naming-convention.json index c688066ff..03666aa8f 100644 --- a/tests/expected_json/naming_convention.naming-convention.json +++ b/tests/expected_json/naming_convention.naming-convention.json @@ -1 +1 @@ -[{"check": "naming-convention", "type": "contract", "convention": "CapWords", "name": {"name": "naming", "source_mapping": {"start": 26, "length": 598, "filename": "tests/naming_convention.sol", "lines": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48]}}}, {"check": "naming-convention", "type": "structure", "convention": "CapWords", "name": {"name": "test", "source_mapping": {"start": 227, "length": 20, "filename": "tests/naming_convention.sol", "lines": [14, 15, 16]}}}, {"check": "naming-convention", "type": "event", "convention": "CapWords", "name": {"name": "event_", "source_mapping": {"start": 303, "length": 19, "filename": "tests/naming_convention.sol", "lines": [23]}}}, {"check": "naming-convention", "type": "function", "convention": "mixedCase", "name": {"name": "GetOne", "source_mapping": {"start": 405, "length": 71, "filename": "tests/naming_convention.sol", "lines": [30, 31, 32, 33]}}}, {"check": "naming-convention", "type": "parameter", "convention": "mixedCase", "name": {"name": "Number2", "source_mapping": {"start": 512, "length": 12, "filename": "tests/naming_convention.sol", "lines": [35]}}}, {"check": "naming-convention", "type": "variable_constant", "convention": "UPPER_CASE_WITH_UNDERSCORES", "name": {"name": "MY_other_CONSTANT", "source_mapping": {"start": 141, "length": 35, "filename": "tests/naming_convention.sol", "lines": [9]}}}, {"check": "naming-convention", "type": "variable", "convention": "mixedCase", "name": {"name": "Var_One", "source_mapping": {"start": 183, "length": 16, "filename": "tests/naming_convention.sol", "lines": [11]}}}, {"check": "naming-convention", "type": "enum", "convention": "CapWords", "name": {"name": "numbers", "source_mapping": {"start": 77, "length": 23, "filename": "tests/naming_convention.sol", "lines": [6]}}}, {"check": "naming-convention", "type": "modifier", "convention": "mixedCase", "name": {"name": "CantDo", "source_mapping": {"start": 545, "length": 36, "filename": "tests/naming_convention.sol", "lines": [41, 42, 43]}}}, {"check": "naming-convention", "type": "parameter", "convention": "mixedCase", "name": {"name": "_used", "source_mapping": {"start": 748, "length": 10, "filename": "tests/naming_convention.sol", "lines": [59]}}}, {"check": "naming-convention", "type": "variable", "convention": "mixedCase", "name": {"name": "_myPublicVar", "source_mapping": {"start": 695, "length": 17, "filename": "tests/naming_convention.sol", "lines": [56]}}}, {"check": "naming-convention", "type": "variable", "convention": "l_O_I_should_not_be_used", "name": {"name": "l", "source_mapping": {"start": 847, "length": 10, "filename": "tests/naming_convention.sol", "lines": [67]}}}] \ No newline at end of file +[{"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Contract 'naming' (tests/naming_convention.sol#3-48) is not in CapWords\n", "type": "contract", "convention": "CapWords", "name": {"name": "naming", "source_mapping": {"start": 26, "length": 598, "filename": "tests/naming_convention.sol", "lines": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Struct 'naming.test' (tests/naming_convention.sol#14-16) is not in CapWords\n", "type": "structure", "convention": "CapWords", "name": {"name": "test", "source_mapping": {"start": 227, "length": 20, "filename": "tests/naming_convention.sol", "lines": [14, 15, 16]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Event 'naming.event_' (tests/naming_convention.sol#23) is not in CapWords\n", "type": "event", "convention": "CapWords", "name": {"name": "event_", "source_mapping": {"start": 303, "length": 19, "filename": "tests/naming_convention.sol", "lines": [23]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Function 'naming.GetOne' (tests/naming_convention.sol#30-33) is not in mixedCase\n", "type": "function", "convention": "mixedCase", "name": {"name": "GetOne", "source_mapping": {"start": 405, "length": 71, "filename": "tests/naming_convention.sol", "lines": [30, 31, 32, 33]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Parameter 'Number2' of naming.setInt (tests/naming_convention.sol#35) is not in mixedCase\n", "type": "parameter", "convention": "mixedCase", "name": {"name": "Number2", "source_mapping": {"start": 512, "length": 12, "filename": "tests/naming_convention.sol", "lines": [35]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Constant 'naming.MY_other_CONSTANT' (tests/naming_convention.sol#9) is not in UPPER_CASE_WITH_UNDERSCORES\n", "type": "variable_constant", "convention": "UPPER_CASE_WITH_UNDERSCORES", "name": {"name": "MY_other_CONSTANT", "source_mapping": {"start": 141, "length": 35, "filename": "tests/naming_convention.sol", "lines": [9]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Variable 'naming.Var_One' (tests/naming_convention.sol#11) is not in mixedCase\n", "type": "variable", "convention": "mixedCase", "name": {"name": "Var_One", "source_mapping": {"start": 183, "length": 16, "filename": "tests/naming_convention.sol", "lines": [11]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Enum 'naming.numbers' (tests/naming_convention.sol#6) is not in CapWords\n", "type": "enum", "convention": "CapWords", "name": {"name": "numbers", "source_mapping": {"start": 77, "length": 23, "filename": "tests/naming_convention.sol", "lines": [6]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Modifier 'naming.CantDo' (tests/naming_convention.sol#41-43) is not in mixedCase\n", "type": "modifier", "convention": "mixedCase", "name": {"name": "CantDo", "source_mapping": {"start": 545, "length": 36, "filename": "tests/naming_convention.sol", "lines": [41, 42, 43]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Parameter '_used' of T.test (tests/naming_convention.sol#59) is not in mixedCase\n", "type": "parameter", "convention": "mixedCase", "name": {"name": "_used", "source_mapping": {"start": 748, "length": 10, "filename": "tests/naming_convention.sol", "lines": [59]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Variable 'T._myPublicVar' (tests/naming_convention.sol#56) is not in mixedCase\n", "type": "variable", "convention": "mixedCase", "name": {"name": "_myPublicVar", "source_mapping": {"start": 695, "length": 17, "filename": "tests/naming_convention.sol", "lines": [56]}}}, {"check": "naming-convention", "impact": "Informational", "confidence": "High", "description": "Variable 'T.l' (tests/naming_convention.sol#67) used l, O, I, which should not be used\n", "type": "variable", "convention": "l_O_I_should_not_be_used", "name": {"name": "l", "source_mapping": {"start": 847, "length": 10, "filename": "tests/naming_convention.sol", "lines": [67]}}}] \ No newline at end of file