Skip to content

robocop format fails to run if there are inline comment with Set Variable If (ReplaceWithVAR.replace_set_variable_if=True) #1495

@hans-min

Description

@hans-min

Minimal reproducible example to replicate this bug:
robocop_bug.resource

*** Keywords ***
Test Bug
    [Documentation]    Bug with ReplaceWithVAR 
    ...    Set replace_set_variable_if=False and you won't get this error.
    ...    Or move the comment  "# this is a comment" out of the Set Variable If line
    VAR    ${no_of_expected_records} =    1
    ${gar} =  Set Variable If    ${no_of_expected_records} > 1    1     # this is a comment
    ...    0

pyproject.toml:

[tool.robocop.format]
configure = ["ReplaceWithVAR.enabled=True"]
Run `robocop format` and you will get a very long Traceback error log with the last few line:
│ ╭──────────────────────────────────────────────────────────────────────────────────── locals ─────────────────────────────────────────────────────────────────────────────────────╮ │
│ │       comments = [Token(COMMENT, '# this is a comment', 5, 72)]                                                                                                                 │ │
│ │ converted_node = <robot.parsing.model.blocks.If object at 0x0000020435504190>                                                                                                   │ │
│ │         indent = Token(SEPARATOR, '    ', 5, 0)                                                                                                                                 │ │
│ │        kw_name = 'setvariableif'                                                                                                                                                │ │
│ │           node = KeywordCall(tokens=[Token(SEPARATOR, '    ', 5, 0), Token(ASSIGN, '${gar} =', 5, 4), Token(SEPARATOR, '    ', 5, 12), Token(KEYWORD, 'Set Variable If', 5,     │ │
│ │                  14), Token(SEPARATOR, '    ', 5, 29), Token(ARGUMENT, '${no_of_expected_records} > 1', 5, 33), Token(SEPARATOR, '    ', 5, 62), Token(ARGUMENT, '1', 5, 66),   │ │
│ │                  Token(SEPARATOR, '    ', 5, 67), Token(COMMENT, '# this is a comment', 5, 72), Token(EOL, '\n', 5, 91), Token(SEPARATOR, '    ', 6, 0), Token(CONTINUATION,    │ │
│ │                  '...', 6, 4), Token(SEPARATOR, '    ', 6, 7), Token(ARGUMENT, '0', 6, 11), Token(EOL, '\n', 6, 12)])                                                           │ │
│ │           self = <robocop.formatter.formatters.ReplaceWithVAR.ReplaceWithVAR object at 0x00000204352909E0>                                                                      │ │
│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                                                                                                     │
│ E:\test\.venv\Lib\site-packages\robocop\formatter\formatters\ReplaceWithVAR.py:179 in restore_comments                                                               │
│                                                                                                                                                                                     │
│   176 │   │   │   return node                                                                                                                                                       │
│   177 │   │   if len(comments) == 1:                                                                                                                                                │
│   178 │   │   │   # insert comment between last data token and EOL                                                                                                                  │
│ ❱ 179 │   │   │   node.tokens = [*node.tokens[:-1], Token(Token.SEPARATOR, "  "), comments[0],                                                                                      │
│   180 │   │   │   return node                                                                                                                                                       │
│   181 │   │   comment_nodes = [Comment.from_params(comment=comment.value, indent=indent) for c                                                                                      │
│   182 │   │   return *comment_nodes, node                                                                                                                                           │
│                                                                                                                                                                                     │
│ ╭─────────────────────────────────────────────── locals ───────────────────────────────────────────────╮                                                                            │
│ │ comments = [Token(COMMENT, '# this is a comment', 5, 72)]                                            │                                                                            │
│ │   indent = '    '                                                                                    │                                                                            │
│ │     node = <robot.parsing.model.blocks.If object at 0x0000020435504190>                              │                                                                            │
│ │     self = <robocop.formatter.formatters.ReplaceWithVAR.ReplaceWithVAR object at 0x00000204352909E0> │                                                                            │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯                                                                            │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
AttributeError: 'If' object has no attribute 'tokens'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions