diff --git a/robocop/checkers/misc.py b/robocop/checkers/misc.py index 95f5fdbb..10a18f75 100644 --- a/robocop/checkers/misc.py +++ b/robocop/checkers/misc.py @@ -1,6 +1,7 @@ """Miscellaneous checkers""" import ast +import re from dataclasses import dataclass from pathlib import Path from typing import Optional @@ -882,12 +883,20 @@ def comma_separated_list(value: str) -> list[str]: misreading your keyword arguments, explicitly state that the value is empty using the built-in ``${EMPTY}`` variable. - If your argument is falsly flagged by this rule, escape the ``=`` character in your argument - value by like so: ``\\=``. - Example of a rule violation:: My Amazing Keyword argument_name= + + Positional arguments that end with a ``=`` character can be falsly flagged by this rule. You + can fix this by making the situation more explicit: + + 1. Escape the ``=`` character:: + + Log value\\= + + 2. Make it a named argument instead:: + + Log message=value= """, ), } @@ -1997,6 +2006,7 @@ class UndefinedArgumentDefaultChecker(VisitorChecker): "undefined-argument-default", "undefined-argument-value", ) + valid_argument_name = re.compile(r"[a-zA-Z0-9-_ ]+") def visit_Arguments(self, node: Arguments): # noqa: N802 for token in node.get_tokens(Token.ARGUMENT): @@ -2034,10 +2044,19 @@ def visit_KeywordCall(self, node: KeywordCall): # noqa: N802 # `=` is escaped continue + if arg_name.endswith(" "): + # Space before `=` is not a named arg + continue + if default_val != "": # Has a value continue + is_plain_var_name = self.valid_argument_name.fullmatch(arg_name) + if is_plain_var_name is None: + # Argument name includes invalid chars + continue + # Falsly triggers if a positional argument ends with `=` # The language server has the same behavior self.report( diff --git a/tests/atest/rules/misc/undefined_argument_value/expected_output.txt b/tests/atest/rules/misc/undefined_argument_value/expected_output.txt index 30d3a5bb..a4a6a7db 100644 --- a/tests/atest/rules/misc/undefined_argument_value/expected_output.txt +++ b/tests/atest/rules/misc/undefined_argument_value/expected_output.txt @@ -2,5 +2,4 @@ test.robot:3:12 [E] 0933 Undefined argument value, use message=${EMPTY} instead test.robot:4:12 [E] 0933 Undefined argument value, use message=${EMPTY} instead test.robot:4:24 [E] 0933 Undefined argument value, use level=${EMPTY} instead test.robot:5:27 [E] 0933 Undefined argument value, use level=${EMPTY} instead -test.robot:6:12 [E] 0933 Undefined argument value, use A great log message=${EMPTY} instead -test.robot:7:12 [E] 0933 Undefined argument value, use A great log message =${EMPTY} instead \ No newline at end of file +test.robot:6:12 [E] 0933 Undefined argument value, use A great log message=${EMPTY} instead \ No newline at end of file diff --git a/tests/atest/rules/misc/undefined_argument_value/test.robot b/tests/atest/rules/misc/undefined_argument_value/test.robot index 10095e23..dbdebc06 100644 --- a/tests/atest/rules/misc/undefined_argument_value/test.robot +++ b/tests/atest/rules/misc/undefined_argument_value/test.robot @@ -15,6 +15,13 @@ With escaped equals sign With defined values Log Hello = world Log message=Hello world + Log message=Hello= Log message== Log = Log = amazing! + +Additional edge cases + # https://github.com/MarketSquare/robotframework-robocop/issues/1160 + Push Buttons C${expression}= + Get Text xpath=(//h4)[5] *= min + Get Text xpath=(//h4)[5] == min