12

I would like to disable a block of code(say a function) in a file for analysis by SonarQube... Any suggestions on how to do that..

I found something for java here --> Turning Sonar off for certain code

3 Answers 3

6

As far as I know Sonar respects // NOSONAR tag for Java projects but it looks like that is also has been implemented for JS: https://jira.sonarsource.com/browse/SONARJS-294

Sign up to request clarification or add additional context in comments.

1 Comment

Apologies for such delay. // NOSONAR works for a single line, is there something for a block of code? like a function, or if-else-if block.
4

To ignore blocks:

 // BEGIN-NOSCAN const YourFunction () => { // Your code } // END-NOSCAN 

To ignore single lines:

const YourCode = 'Example'; // NOSONAR 

You can read more about it here in the SonarQube docs Narrowing the Focus. Thanks to Nicolas's answer above.

1 Comment

As far as I've understood, //NOSONAR is actually required to be placed at the end of the line, not on top
1

Add below configuration in your sonar properties file

sonar.issue.ignore.block=e1 sonar.issue.ignore.block.e1.beginBlockRegexp=NOSONAR_BEGIN sonar.issue.ignore.block.e1.endBlockRegexp=NOSONAR_END 

and use annotations like this to ignore a specific code block

//NOSONAR_BEGIN your code //NOSONAR_END 

this is compatible with the latest sonarQube versions i.e. 9.9+.

For more reference: https://docs.communityhealthtoolkit.org/contribute/code/static-analysis/#ignoring-all-rules-for-a-block-of-code

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.