Skip to content

[BUG] Robocop linter highlighting broken in case of custom rule with visit_File and node.source.suffix check #588

@fsmura

Description

@fsmura

Describe the bug
I'm using a robocop custom rule for checking variable names within resource files located under a specific path.
The custom rule visits the file, checks its path and in case it matches a condition, the variable section is visited and the variables are checked.
After activating this rule in the robocop.toml, the complete robocop highlighting in VS code doesn't work anymore.

Steps To Reproduce

Set up structure:
Image

test.resource:

*** Variables ***
${VARIABLE}     ${EMPTY}

robocop.toml:

[tool.robocop.lint]
configure = ["line-too-long.line_length=20"]
custom_rules = ["custom.py"]

custom.py:

from robocop.linter.rules import Rule, RuleSeverity, VisitorChecker


class VarNameCheck(Rule):
    """
    Checks that variable names in the Variables section of resource files start with the prefix LOCATOR_.
    """

    name = "var-naming"
    rule_id = "EX10"
    message = "Variable name '{variable}' must start with the prefix LOCATOR_"
    severity = RuleSeverity.ERROR


class VarNameCheck(VisitorChecker):
    var_name_rule: VarNameCheck

    def visit_File(self, node):
        if node.source.suffix == ".resource":
            for section in node.sections:
                if section.header.name == "Variables":
                    self.visit_VariableSection(section)

    def visit_VariableSection(self, node):
        for token in node.body:
            if token.type == "VARIABLE" and not token.name.startswith("${LOCATOR_"):
                self.report(
                    self.var_name_rule,
                    node=token,
                    variable=token.name,
                    col=token.data_tokens[0].col_offset + 1,
                    end_col=token.data_tokens[0].end_col_offset + 1,
                )

Actual behavior
No highlighting at all:
Image

Expected behavior
The highlighting for the robocop rules should be displayed:
Image

Interesting note
I you remove the line if node.source.suffix == ".resource": and reload the robotcode extension, the highlighting works fine!

Logs
Unfortunately no outputs for the error state.

Desktop (please complete the following information):

  • VS Code Version: 1.113.0
  • RobotCode Version: 2.4.0
  • Python Version: 3.12.10
  • RobotFramework Version: 7.4.2
  • Robocop Version: 8.2.4
  • OS: Windows 11 Enterprise (issue is OS independent, error occurred within a Ubuntu WSL, too)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions