Improve comment patching + state variable constant patching

pull/238/head
Josselin 5 years ago
parent 9ad04d9128
commit b600cae512
  1. 4
      slither/tools/slither_format/formatters/constable_states.py
  2. 8
      slither/tools/slither_format/formatters/naming_convention.py

@ -1,10 +1,12 @@
import re import re
from ..exceptions import FormatError from ..exceptions import FormatError, FormatImpossible
from ..utils.patches import create_patch from ..utils.patches import create_patch
def format(slither, result): def format(slither, result):
elements = result['elements'] elements = result['elements']
for element in elements: for element in elements:
if not element.expression:
raise FormatImpossible(f'{element.name} is uninitialized and cannot become constant.')
_patch(slither, result, element['source_mapping']['filename_absolute'], _patch(slither, result, element['source_mapping']['filename_absolute'],
element['name'], element['name'],
"constant " + element['name'], "constant " + element['name'],

@ -338,17 +338,17 @@ def _explore_variables_declaration(slither, variables, result, target, convert,
# Patch comment only makes sense for local variable declaration in the parameter list # Patch comment only makes sense for local variable declaration in the parameter list
if patch_comment and isinstance(variable, LocalVariable): if patch_comment and isinstance(variable, LocalVariable):
if 'lines' in variable.source_mapping and variable.source_mapping['lines']: if 'lines' in variable.source_mapping and variable.source_mapping['lines']:
end_line = variable.source_mapping['lines'][0]
func = variable.function func = variable.function
end_line = func.source_mapping['lines'][0]
if variable in func.parameters: if variable in func.parameters:
idx = len(func.parameters) - func.parameters.index(variable) idx = len(func.parameters) - func.parameters.index(variable) + 1
first_line = end_line - idx - 2 first_line = end_line - idx - 2
potential_comments = slither.source_code[filename_source_code].encode('utf8') potential_comments = slither.source_code[filename_source_code].encode('utf8')
potential_comments = potential_comments.splitlines(keepends=True)[first_line:end_line-1] potential_comments = potential_comments.splitlines(keepends=True)[first_line:end_line-1]
idx_beginning = variable.source_mapping['start'] idx_beginning = func.source_mapping['start']
idx_beginning += - variable.source_mapping['starting_column'] + 1 idx_beginning += - func.source_mapping['starting_column'] + 1
idx_beginning += - sum([len(c) for c in potential_comments]) idx_beginning += - sum([len(c) for c in potential_comments])
old_comment = f'@param {old_str}'.encode('utf8') old_comment = f'@param {old_str}'.encode('utf8')

Loading…
Cancel
Save