Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite source mapping class #695

Closed
montyly opened this issue Nov 20, 2020 · 1 comment
Closed

Rewrite source mapping class #695

montyly opened this issue Nov 20, 2020 · 1 comment
Assignees

Comments

@montyly
Copy link
Member

montyly commented Nov 20, 2020

  • Rewrite compute_line and _convert_source_mapping to be efficient:

    @staticmethod
    def _compute_line(source_code, start, length):
    """
    Compute line(s) numbers and starting/ending columns
    from a start/end offset. All numbers start from 1.
    Not done in an efficient way
    """
    source_code = source_code.encode("utf-8")
    total_length = len(source_code)
    source_code = source_code.splitlines(True)
    counter = 0
    i = 0
    lines = []
    starting_column = None
    ending_column = None
    while counter < total_length:
    # Determine the length of the line, and advance the line number
    line_content = source_code[i]
    line_length = len(line_content)
    i = i + 1
    # Determine our column numbers.
    if starting_column is None and counter + line_length > start:
    starting_column = (start - counter) + 1
    if (
    starting_column is not None
    and ending_column is None
    and counter + line_length > start + length
    ):
    ending_column = ((start + length) - counter) + 1
    # Advance the current position counter, and determine line numbers.
    counter += line_length
    if counter > start:
    lines.append(i)
    # If our advanced position for the next line is out of range, stop.
    if counter > start + length:
    break
    return lines, starting_column, ending_column

  • Use fields instead of dictionary:

    return {
    "start": s,
    "length": l,
    "filename_used": filename_used,
    "filename_relative": filename_relative,
    "filename_absolute": filename_absolute,
    "filename_short": filename_short,
    "is_dependency": is_dependency,
    "lines": lines,
    "starting_column": starting_column,
    "ending_column": ending_column,
    }

  • Remove source_mapping_str and use __str__:

    @property
    def source_mapping_str(self) -> str:
    lines = self._get_lines_str()
    return f'{self.source_mapping.get("filename_short", "")}{lines}'

@montyly
Copy link
Member Author

montyly commented Mar 4, 2021

Consider #743 (comment) during the refactor

montyly added a commit that referenced this issue Jun 23, 2021
- core.source_mapping.SourceMapping is now a class (fix #695)
- Add slither.offset_to_objects to retrieve the object on a given
offset (WIP)
- Add slither.utils.source_mapping APIs to allow retrieving
definition/implementations/references informations from an offset (WIP)

Additionally this PR create a strict order for the detectors tests
output to ease the comparison in case in small changes in the json
output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants