bikerp
bikerp

Reputation: 131

SonarJS - how to avoid "Non-existent variables should not be referenced" issue

I have a JavaScript project that consists of several source files. These files are referenced in <script> tags in index.html page. There are functions and variables defined in those source files and used in other source files. The problem is that Sonar treats those files as independent and I got

Non-existent variables should not be referenced (javascript:S3827)

issue. Can anybody help how to avoid this?

Thanks

Pavel

Upvotes: 2

Views: 1934

Answers (3)

2bam
2bam

Reputation: 149

For anyone also landing here looking to avoid warnings from globals in vscode's SonarQube plugin locally, you need to add a comma separated string with all your globals in your user/workspace settings like so:

{
    "sonarlint.analyzerProperties": {
        "sonar.javascript.globals": "__ctx,clock,_DEBUG"
    }
}

The file can be easily accessed through vscode's command palette: (Ctrl+Shift+P), Preferences: Open Workspace Settings (JSON), Enter.

Upvotes: 0

Siddharth
Siddharth

Reputation: 177

If your variable is a global variable, you can choose to invoke the variable from the window object like window.variable. Sonarqube will no longer flag that variable as undefined.

Upvotes: 1

Elena Vilchik
Elena Vilchik

Reputation: 1090

This rule requires configuration. List names of variables and functions shared across files in "sonar.javascript.globals" project property.

Upvotes: 4

Related Questions