Skip to content

Commit

Permalink
Utilize create_matcherror more in other rules (#4408)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
cavcrosby and ssbarnea authored Dec 6, 2024
1 parent 81b413b commit 2726e13
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
17 changes: 7 additions & 10 deletions src/ansiblelint/rules/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.schemas.__main__ import JSON_SCHEMAS
Expand All @@ -16,6 +15,7 @@

if TYPE_CHECKING:
from ansiblelint.config import Options
from ansiblelint.errors import MatchError
from ansiblelint.utils import Task


Expand Down Expand Up @@ -120,11 +120,10 @@ def _get_field_matches(
if not has_jinja(plugin_value) and plugin_value not in values:
msg = f"'{key}' must be one of the currently available values: {', '.join(values)}"
results.append(
MatchError(
self.create_matcherror(
message=msg,
lineno=data.get("__line__", 1),
lintable=file,
rule=self,
filename=file,
details=ValidateSchemaRule.description,
tag=f"schema[{file.kind}]",
),
Expand All @@ -149,10 +148,9 @@ def matchtask(
msg = pre_checks["task"][key]["msg"]
tag = pre_checks["task"][key]["tag"]
results.append(
MatchError(
self.create_matcherror(
message=msg,
lintable=file,
rule=self,
filename=file,
details=ValidateSchemaRule.description,
tag=f"schema[{tag}]",
),
Expand All @@ -178,10 +176,9 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
return []

result.append(
MatchError(
self.create_matcherror(
message=error,
lintable=file,
rule=self,
filename=file,
details=ValidateSchemaRule.description,
tag=f"schema[{file.kind}]",
),
Expand Down
37 changes: 15 additions & 22 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
PLAYBOOK_ROLE_KEYWORDS,
RC,
)
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule, RulesCollection
from ansiblelint.runner import Runner
Expand All @@ -26,6 +25,7 @@
from ansiblelint.utils import parse_yaml_from_file

if TYPE_CHECKING:
from ansiblelint.errors import MatchError
from ansiblelint.utils import Task


Expand Down Expand Up @@ -121,11 +121,10 @@ def get_var_naming_matcherror(
) -> MatchError | None:
"""Return a MatchError if the variable name is not valid, otherwise None."""
if not isinstance(ident, str): # pragma: no cover
return MatchError(
return self.create_matcherror(
tag="var-naming[non-string]",
message="Variables names must be strings.",
rule=self,
lintable=file,
filename=file,
)

if ident in ANNOTATION_KEYS or ident in self.allowed_special_names:
Expand All @@ -134,35 +133,31 @@ def get_var_naming_matcherror(
try:
ident.encode("ascii")
except UnicodeEncodeError:
return MatchError(
return self.create_matcherror(
tag="var-naming[non-ascii]",
message=f"Variables names must be ASCII. ({ident})",
rule=self,
lintable=file,
filename=file,
)

if keyword.iskeyword(ident):
return MatchError(
return self.create_matcherror(
tag="var-naming[no-keyword]",
message=f"Variables names must not be Python keywords. ({ident})",
rule=self,
lintable=file,
filename=file,
)

if ident in self.reserved_names:
return MatchError(
return self.create_matcherror(
tag="var-naming[no-reserved]",
message=f"Variables names must not be Ansible reserved names. ({ident})",
rule=self,
lintable=file,
filename=file,
)

if ident in self.read_only_names:
return MatchError(
return self.create_matcherror(
tag="var-naming[read-only]",
message=f"This special variable is read-only. ({ident})",
rule=self,
lintable=file,
filename=file,
)

# We want to allow use of jinja2 templating for variable names
Expand All @@ -172,11 +167,10 @@ def get_var_naming_matcherror(
if not bool(self.re_pattern.match(ident)) and (
not prefix or not prefix.from_fqcn
):
return MatchError(
return self.create_matcherror(
tag="var-naming[pattern]",
message=f"Variables names should match {self.re_pattern_str} regex. ({ident})",
rule=self,
lintable=file,
filename=file,
)

if (
Expand All @@ -185,11 +179,10 @@ def get_var_naming_matcherror(
and not has_jinja(prefix.value)
and is_fqcn_or_name(prefix.value)
):
return MatchError(
return self.create_matcherror(
tag="var-naming[no-role-prefix]",
message=f"Variables names from within roles should use {prefix.value}_ as a prefix.",
rule=self,
lintable=file,
filename=file,
)
return None

Expand Down

0 comments on commit 2726e13

Please sign in to comment.