robocorp / robotframework-lsp

Robocorp extensions for VS Code: Robocorp Code and RFW LSP
https://robocorp.com/docs/developer-tools/visual-studio-code
Apache License 2.0
206 stars 94 forks source link

Warning when global variable is shadows by local variable #925

Open xylix opened 1 year ago

xylix commented 1 year ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

When there is some RF code like this:

*** Variables ***
${ACTION_TYPE}   "a value"

*** Keywords ***
Arguments vs Variables
    [ARGUMENTS]    ${action_type}  # the argument shadows a global variable

    Log To Console    ${action_type}  # logs the local variable, as expected
    Log To Console    ${ACTION_TYPE} # logs the local variable, which may be unexpected

   ${ACTION_TYPE}=  Set Variable    RANDOM  # changes the local variable
   Set Task Variable  ${action_type}  DIFFERENT RANDOM # changes the task level variable

Especially thanks to the fact that RobotFramework variables are case-insensitive and underscore-insensitive, it's pretty easy for a user to accidentally shadow a global variable with a local one.

Describe the solution you'd like I would like a warning in situations like this. I guess a workspace-level or comment toggle to disable the warning might also be useful?

Describe alternatives you've considered The other thing also demonstrated in the example, using "different" names that parse to same Robot Framework variable name, could also be an LSP check or a linting rule, but it might make more sense for that to be opt-in.

bhirsz commented 1 year ago

We have added a lot of new variable related rules to Robocop recently. They should be included in the next release. Some of them should be helpful for you.

We have old possible-variable-overwriting rule, and now new unused-argument, argument-overwritten-before-usage, variable-overwritten-before-usage, unused-variable and inconsistent-variable-name.

The last one, inconsistent-variable-name seems closest to what you want - see my tests in open PR for comparison https://github.com/MarketSquare/robotframework-robocop/pull/843/files#diff-2f0d7272720db7099a933a449e434fc5918db1794deb7e76a0ab024970ecff4b .

Also Robotidy recently got new transformer - RenameVariables - which is quite powerfull and can automatically rename all variables by their scope (removing any inconsistency): https://robotidy.readthedocs.io/en/stable/transformers/RenameVariables.html

It still under development but we're focusing a lot in this area recently. If there are some issues, missing features then feel free to open issues in our repositories :)