Skip to content

Commit

Permalink
feat: exclusions for display fields (#48)
Browse files Browse the repository at this point in the history
* feat: exclusions for display fields

* Update src/erc7730/lint/lint_validate_display_fields.py
  • Loading branch information
fsamier authored Oct 4, 2024
1 parent 5a653b8 commit 325dbdc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
43 changes: 36 additions & 7 deletions src/erc7730/common/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@


class Output(BaseModel):
"""An output notice/warning/error."""
"""An output info/debug/warning/error."""

class Level(IntEnum):
"""ERC7730Linter output level."""

DEBUG = auto()
INFO = auto()
WARNING = auto()
ERROR = auto()
Expand All @@ -25,12 +26,18 @@ class Level(IntEnum):


class OutputAdder(ABC):
"""An output notice/warning/error sink."""
"""An output debug/info/warning/error sink."""

has_infos = False
has_warnings = False
has_errors = False

@abstractmethod
def debug(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
) -> None:
raise NotImplementedError()

@abstractmethod
def info(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
Expand All @@ -56,6 +63,12 @@ class ListOutputAdder(OutputAdder, BaseModel):

outputs: list[Output] = []

@override
def debug(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
) -> None:
self.outputs.append(Output(file=file, line=line, title=title, message=message, level=Output.Level.DEBUG))

@override
def info(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
Expand All @@ -82,6 +95,12 @@ def error(
class ConsoleOutputAdder(OutputAdder):
"""An output adder that prints to the console."""

@override
def debug(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
) -> None:
self._log(Output.Level.DEBUG, message, file, line, title)

@override
def info(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
Expand Down Expand Up @@ -113,23 +132,25 @@ def _log(
title: str | None = None,
) -> None:
match level:
case Output.Level.DEBUG:
style = "bold"
case Output.Level.INFO:
color = "blue"
style = "blue"
case Output.Level.WARNING:
color = "yellow"
style = "yellow"
case Output.Level.ERROR:
color = "red"
style = "red"
case _:
assert_never(level)

log = f"[{color}]{level.name}"
log = f"[{style}]{level.name}"
if file is not None:
log += f": {file.name}"
if line is not None:
log += f" line {line}"
if title is not None:
log += f": {title}"
log += f"[/{color}]: {message}"
log += f"[/{style}]: {message}"

print(log)

Expand All @@ -138,6 +159,12 @@ def _log(
class GithubAnnotationsAdder(OutputAdder):
"""An output adder that formats errors to be parsed as Github annotations."""

@override
def debug(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
) -> None:
pass

@override
def info(
self, message: str, file: FilePath | None = None, line: int | None = None, title: str | None = None
Expand Down Expand Up @@ -169,6 +196,8 @@ def _log(
title: str | None = None,
) -> None:
match level:
case Output.Level.DEBUG:
raise RuntimeError("GithubAnnotationsAdder does not support debug messages")
case Output.Level.INFO:
lvl = "notice"
case Output.Level.WARNING:
Expand Down
34 changes: 25 additions & 9 deletions src/erc7730/lint/lint_validate_display_fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import final, override

from erc7730.common.abi import compute_paths, function_to_selector, reduce_signature, signature_to_selector
Expand All @@ -7,6 +8,8 @@
from erc7730.model.resolved.context import EIP712JsonSchema, ResolvedContractContext, ResolvedEIP712Context
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor

AUTHORIZED_MISSING_DISPLAY_FIELDS_REGEX = {r"(.+\.)?nonce"}


@final
class ValidateDisplayFieldsLinter(ERC7730Linter):
Expand Down Expand Up @@ -43,10 +46,16 @@ def _validate_eip712_paths(cls, descriptor: ResolvedERC7730Descriptor, out: Outp
format_paths = compute_format_paths(descriptor.display.formats[schema.primaryType]).data_paths

for path in eip712_paths - format_paths:
out.warning(
title="Missing Display field",
message=f"Display field for path `{path}` is missing for message {schema.primaryType}.",
)
if any(re.fullmatch(regex, path) for regex in AUTHORIZED_MISSING_DISPLAY_FIELDS_REGEX):
out.debug(
title="Optional Display field missing",
message=f"Display field for path `{path}` is missing for message {schema.primaryType}.",
)
else:
out.warning(
title="Missing Display field",
message=f"Display field for path `{path}` is missing for message {schema.primaryType}.",
)
for path in format_paths - eip712_paths:
out.error(
title="Extra Display field",
Expand Down Expand Up @@ -99,11 +108,18 @@ def _validate_abi_paths(cls, descriptor: ResolvedERC7730Descriptor, out: OutputA
abi_paths = abi_paths_by_selector[keccak]

for path in abi_paths - format_paths:
out.warning(
title="Missing Display field",
message=f"Display field for path `{path}` is missing for selector {cls._display(selector,
keccak)}.",
)
if not any(re.fullmatch(regex, path) for regex in AUTHORIZED_MISSING_DISPLAY_FIELDS_REGEX):
out.debug(
title="Optional Display field missing",
message=f"Display field for path `{path}` is missing for selector {cls._display(selector,
keccak)}.",
)
else:
out.warning(
title="Missing Display field",
message=f"Display field for path `{path}` is missing for selector {cls._display(selector,
keccak)}.",
)
for path in format_paths - abi_paths:
out.error(
title="Invalid Display field",
Expand Down

0 comments on commit 325dbdc

Please sign in to comment.