Skip to content

Commit

Permalink
feat: BACK-8055 Detect unused definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaireGuerreGiordano committed Nov 25, 2024
1 parent 780971e commit c41d245
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/erc7730/lint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod

from erc7730.common.output import OutputAdder
from erc7730.model.input.descriptor import InputERC7730Descriptor
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor


Expand All @@ -13,5 +14,5 @@ class ERC7730Linter(ABC):
"""

@abstractmethod
def lint(self, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None:
raise NotImplementedError()
9 changes: 6 additions & 3 deletions src/erc7730/lint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def lint_file(path: Path, linter: ERC7730Linter, out: OutputAdder, show_as: Path

with BufferAdder(file_out, prolog=f"➡️ checking [bold]{label}[/bold]…", epilog="") as out, ExceptionsToOutput(out):
input_descriptor = InputERC7730Descriptor.load(path)
resolved_descriptor = ERC7730InputToResolved().convert(input_descriptor, out)
if resolved_descriptor is not None:
linter.lint(resolved_descriptor, out)
if isinstance(linter, DefinitionLinter):
linter.lint(input_descriptor, out)
else:
resolved_descriptor = ERC7730InputToResolved().convert(input_descriptor, out)
if resolved_descriptor is not None:
linter.lint(resolved_descriptor, out)
3 changes: 2 additions & 1 deletion src/erc7730/lint/lint_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from erc7730.common.output import OutputAdder
from erc7730.lint import ERC7730Linter
from erc7730.model.input.descriptor import InputERC7730Descriptor
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor


Expand All @@ -13,6 +14,6 @@ def __init__(self, linters: list[ERC7730Linter]):
self.lints = linters

@override
def lint(self, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None:
for linter in self.lints:
linter.lint(descriptor, out)
20 changes: 11 additions & 9 deletions src/erc7730/lint/lint_transaction_type_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from erc7730.lint.classifier import TxClass
from erc7730.lint.classifier.abi_classifier import ABIClassifier
from erc7730.lint.classifier.eip712_classifier import EIP712Classifier
from erc7730.model.input.descriptor import InputERC7730Descriptor
from erc7730.model.resolved.context import EIP712Schema, ResolvedContractContext, ResolvedEIP712Context
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor
from erc7730.model.resolved.display import ResolvedDisplay, ResolvedFormat
Expand All @@ -18,15 +19,16 @@ class ClassifyTransactionTypeLinter(ERC7730Linter):
"""

@override
def lint(self, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
if descriptor.context is None:
return None
if (tx_class := self._determine_tx_class(descriptor)) is None:
# could not determine transaction type
return None
if (display := descriptor.display) is None:
return None
DisplayFormatChecker(tx_class, display).check(out)
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None:
if isinstance(descriptor, ResolvedERC7730Descriptor):
if descriptor.context is None:
return None
if (tx_class := self._determine_tx_class(descriptor)) is None:
# could not determine transaction type
return None
if (display := descriptor.display) is None:
return None
DisplayFormatChecker(tx_class, display).check(out)

@classmethod
def _determine_tx_class(cls, descriptor: ResolvedERC7730Descriptor) -> TxClass | None:
Expand Down
3 changes: 2 additions & 1 deletion src/erc7730/lint/lint_validate_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from erc7730.common.abi import compute_signature, get_functions
from erc7730.common.output import OutputAdder
from erc7730.lint import ERC7730Linter
from erc7730.model.input.descriptor import InputERC7730Descriptor
from erc7730.model.resolved.context import ResolvedContractContext, ResolvedEIP712Context
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor

Expand All @@ -17,7 +18,7 @@ class ValidateABILinter(ERC7730Linter):
"""

@override
def lint(self, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None:
if isinstance(descriptor.context, ResolvedEIP712Context):
return self._validate_eip712_schemas(descriptor.context, out)
if isinstance(descriptor.context, ResolvedContractContext):
Expand Down
13 changes: 7 additions & 6 deletions src/erc7730/lint/lint_validate_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from erc7730.common.output import OutputAdder
from erc7730.lint import ERC7730Linter
from erc7730.model.resolved.context import ResolvedEIP712Context
from erc7730.model.input.descriptor import InputERC7730Descriptor
from erc7730.model.input.display import InputReference
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor


Expand All @@ -11,17 +12,17 @@ class DefinitionLinter(ERC7730Linter):
"""Check that parameters under definitions are used in formats section"""

@override
def lint(self, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
if isinstance(descriptor.context, ResolvedEIP712Context) and descriptor.display.definitions is not None:
for name, _ in descriptor.display.definitions.items():
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None:
if isinstance(descriptor, InputERC7730Descriptor) and descriptor.display.definitions is not None:
for _, definition in descriptor.display.definitions.items():
found = False
for _, format in descriptor.display.formats.items():
if found is False:
for field in format.fields:
if field.path == name:
if isinstance(field, InputReference) and field.ref == definition.label:
found = True
if found is False:
out.error(
title="Unused field definition",
message=f"Field {name} is not used in descriptor formats.",
message=f"Field {definition.label} is not used in descriptor formats.",
)
8 changes: 5 additions & 3 deletions src/erc7730/lint/lint_validate_display_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from erc7730.common.abi import function_to_selector
from erc7730.common.output import OutputAdder
from erc7730.lint import ERC7730Linter
from erc7730.model.input.descriptor import InputERC7730Descriptor
from erc7730.model.paths import DataPath, Field
from erc7730.model.paths.path_ops import data_path_ends_with, path_starts_with, to_absolute
from erc7730.model.paths.path_schemas import (
Expand All @@ -27,9 +28,10 @@ class ValidateDisplayFieldsLinter(ERC7730Linter):
"""

@override
def lint(self, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
self._validate_eip712_paths(descriptor, out)
self._validate_abi_paths(descriptor, out)
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None:
if isinstance(descriptor, ResolvedERC7730Descriptor):
self._validate_eip712_paths(descriptor, out)
self._validate_abi_paths(descriptor, out)

@classmethod
def _validate_eip712_paths(cls, descriptor: ResolvedERC7730Descriptor, out: OutputAdder) -> None:
Expand Down

0 comments on commit c41d245

Please sign in to comment.