From 90a55e10a3287fcc21c112ed5751fc8df7df9b73 Mon Sep 17 00:00:00 2001 From: cguerre-giordano Date: Fri, 22 Nov 2024 17:39:27 +0100 Subject: [PATCH] feat: BACK-8055 Detect unused definitions --- src/erc7730/lint/lint.py | 7 ++--- src/erc7730/lint/lint_validate_definitions.py | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/erc7730/lint/lint_validate_definitions.py diff --git a/src/erc7730/lint/lint.py b/src/erc7730/lint/lint.py index 1fdd9bc..7d30a21 100644 --- a/src/erc7730/lint/lint.py +++ b/src/erc7730/lint/lint.py @@ -10,6 +10,7 @@ from erc7730.lint.lint_base import MultiLinter from erc7730.lint.lint_transaction_type_classifier import ClassifyTransactionTypeLinter from erc7730.lint.lint_validate_abi import ValidateABILinter +from erc7730.lint.lint_validate_definitions import DefinitionLinter from erc7730.lint.lint_validate_display_fields import ValidateDisplayFieldsLinter from erc7730.model.input.descriptor import InputERC7730Descriptor @@ -42,11 +43,7 @@ def lint_all(paths: list[Path], out: OutputAdder) -> None: :return: output errors """ linter = MultiLinter( - [ - ValidateABILinter(), - ValidateDisplayFieldsLinter(), - ClassifyTransactionTypeLinter(), - ] + [ValidateABILinter(), ValidateDisplayFieldsLinter(), ClassifyTransactionTypeLinter(), DefinitionLinter()] ) for path in paths: diff --git a/src/erc7730/lint/lint_validate_definitions.py b/src/erc7730/lint/lint_validate_definitions.py new file mode 100644 index 0000000..b4b4b7e --- /dev/null +++ b/src/erc7730/lint/lint_validate_definitions.py @@ -0,0 +1,27 @@ +from typing import final, override + +from erc7730.common.output import OutputAdder +from erc7730.lint import ERC7730Linter +from erc7730.model.resolved.context import ResolvedEIP712Context +from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor + + +@final +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(): + found = False + for _, format in descriptor.display.formats.items(): + if found is False: + for field in format.fields: + if field.path == name: + found = True + if found is False: + out.error( + title="Unused field definition", + message=f"Field {name} is not used in descriptor formats.", + )