Javascript files not support //NOSONAR option for SonarQube Scanner

Dear all,
We are fighting with the option to ignore lines into Javascript which is no supported by SonarQube Scanner
My configuration is:

  • Azure DevOps CI (Build Pipeline)
  • Simple project with basic code files (JS, XML & SQL)
  • SonarQube Community Edition 8.9 (updated last monday)

The Javascript file is used by a specific dedicated engine (Dedicated software) and support some non standard keys like:

#include "XXXXX.YYY"

This option create mistake in Scanner and we are looking to ask the scanner to ignore these lines using //NOSONAR, as explained into your documentation ( Frequently Asked Questions | SonarQube Docs)

But as you can see below the SonarQube scanner does not support it and continue to present error from this line

How can I do to support this option ?

Thanks for your help

Fab

Hey there.

You’re running into a parsing issue, which means the entire file cannot be parsed (it’s not an issue that can be turned. off with //NOSONAR)

Where is this syntax coming from? Does it have a name? Is it currently a proposed new language feature for Javascript, or something very specific to your engine?

This is a dedicated Solution using it’s own JS parser Engine, and that “#include” key is part of supported option for.
We can’t ignore simply that, and that is why i was looking the //NOSONAR option
Fab

Hello Support team,

I finally worked with Azure DevOps to develop a PowerShell script task running just before the SonarQube Run Code Analysis.

This script is looking into all source code file to search the problematic word (#include) and replacing directly by comment before (//#include)

You can see the Yaml task here if anyone need to do something similar in another context:

steps:
- powershell: |
   Write-Host "------------------------------------------------------------"
   Write-Host "Get JS File content containing #include line", $(Agent.WorkFolder)
   Write-Host "------------------------------------------------------------"
   
   $AllJSFIleToManage = Get-ChildItem -Path $(Agent.WorkFolder)\*.js -Recurse -Force  |  Select-String -Pattern "#include " -AllMatches | Foreach {$_.Path}
   foreach ($MyFile in $AllJSFIleToManage)
   {
      Write-Host "JS File to change -", $MyFile
      
      (Get-Content $MyFile -Encoding UTF8) -replace '#include ', '//#include ' | Set-Content $MyFile 
      Write-Host "JS File changed -", $MyFile 
      Write-Host " -----------------------------------"
   }
   
  errorActionPreference: continue
  displayName: 'PowerShell Script remove specific #Include lines from JS files for Sonar'
  continueOnError: true

It’s working now like a charm and SonarQube is now supporting this source code which is an alternative Javascript.

Enjoy your day.
Fab

I published the solution in that Microsoft DevOps place:

Fab