How to exclude JavaScript source files from sonarqube analysis? - javascript

Hello sonarqube experts,
I am running sonarqube 5.2 server and JavaScript plugin v.2.13. I would like to exclude some of the JavaScript code that is third party library (jQuery, for example) from my analysis. how can I do this?
I tried something like this:
sonar.module= myModule
myModule.sonar.sources=srcFolder
myModule.sonar.exclusions=**/notMyCode/*.js
I can see in the log the exclusion filter is being picked up:
Excluded sources: =**/notMyCode/*.js;
but then it appears to be ignored as all the files are analyzed anyway:
101 files indexed
0 files ignored because of inclusion/exclusion patterns
I tried all possible exclusion patterns and nothing seems to work, even when I put it this way:
sonar.exclusions=*.js
This should exclude all javascript files but the exclusion filter is being completely ignored.
just found out by trial and error that the exclusion patterns work if "global" qualifier is used.
for example the following works as expected:
sonar.global.exclusions=**/notMyCode/*.js;
while this one doesn't:
myModule.sonar.exclusions=**/notMyCode/*.js;
is this expected behavior?

This is invalid:
sonar.module= myModule
You probably meant to write this (plural "modules"):
sonar.modules = myModule
The use of sonar.modules is demonstrated in several examples in the documentation.
With this correction, the exclusions should work as expected,
both global and project-specific.
Also make sure that you're using the correct glob pattern,
as explained here.
Finally, in your last examples you used a terminating ; at the end of lines, for example myModule.sonar.exclusions=**/notMyCode/*.js, you should remove those.

In sonarqube go to project settings-->General settings-->Analysis scope-->source file exclusions.
example to exclude js files : **/*.js

Related

Ignore lines running debuggers like pdb.set_trace() or debugger in the repository?

Sometimes when I commit my code in python or javascript, I forgot to remove the debuggers, so I must do it manually.
I know something about creating a .gitattributes file at the root of the project with the following code:
*.py filter=import pdb; pdb.set_trace()
*.js filter=debugger
But when I commit the code, it saves the lines in the repository anyway.
How to remove or at least comment these lines in the repository, and put them back in the working directory?
Try commenting the unwanted lines out, in js this is done by putting the // operator before unwanted lines.
In python this is done by the # operator before the unwanted line
for instance
#filter=import pdb; pdb.set_trace()
And for js
//filter=debugger
Opinion: the filter option in a .gitattributes should not be used the way you're trying to use it. Even if you fix the syntax, this is a bad idea.
The actual problem is that the syntax for filter lines in .gitattributes is simply:
filter=<name>
You must then define this filter (one whose name you substitute in for the angle bracketed expression) in .git/config or your global .gitconfig. For instance, if you had a filter named hack-debug-lines-in-python, you might have:
*.py filter=hack-debug-lines-in-python
in the .gitattributes file, and then:
[filter "hack-debug-lines-in-python"]
clean = ...
smudge = ...
in your .git/config. You would need to fill in the three dots here with an actual program that can do the debug-line trickiness that you want.
You will need to write this filter program. Git does not include one.

How can I use KotlinJS without the stdlib?

Since in most cases we have this:
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
And the Kotlin standard library seems to large for me.
I want to minimize it by creating my own stdlib, which can be smaller, by declaring only external methods I need.
I tried to remove that method, it compiles, but the generated JS code has this:
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'streaking'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'streaking'.");
}
Which means that there must be something necessary in the stdlib, which I don't know.
As my requirements are described above, is there any workarounds?
Or how can I reduce the size of the generated code?
You should make use of Kotlin's kotlin-dce-js Plugin, which does exactly what you want: Minimize the code to what you really use and eliminate the "dead code".
See here:
https://kotlinlang.org/docs/reference/javascript-dce.html#javascript-dce
"There are several ways you get unused declarations: [...] - You are using a shared library which provides much more functions than you actually need. For example, standard library (kotlin.js) contains functions for manipulating lists, arrays, char sequences, adapters for DOM, etc, which together gives about 1.3 mb file. A simple "Hello, world" application only requires console routines, which is only few kilobytes for the entire file."
Here's an alternative. You can kick out the kotlin runtime and make use of this method declaration:
external fun js(code: String): dynamic
Like, if you're using console.log, just write
js("console").log("Hey man")
And remove the check you mentioned in the description. It'll work.

Understanding the Communication between Modules in jQuery Source Code Structure [duplicate]

Uncompressed jQuery file: http://code.jquery.com/jquery-2.0.3.js
jQuery Source code: https://github.com/jquery/jquery/blob/master/src/core.js
What are they doing to make it seem like the final output is not using Require.js under the hood? Require.js examples tells you to insert the entire library into your code to make it work standalone as a single file.
Almond.js, a smaller version of Require.js also tell you to insert itself into your code to have a standalone javascript file.
When minified, I don't care for extra bloat, it's only a few extra killobytes (for almond.js), but unminified is barely readable. I have to scroll all the way down, past almond.js code to see my application logic.
Question
How can I make my code to be similar to jQuery, in which the final output does not look like a Frankenweenie?
Short answer:
You have to create your own custom build procedure.
Long answer
jQuery's build procedure works only because jQuery defines its modules according to a pattern that allows a convert function to transform the source into a distributed file that does not use define. If anyone wants to replicate what jQuery does, there's no shortcut: 1) the modules have to be designed according to a pattern which will allow stripping out the define calls, and 2) you have to have a custom conversion function. That's what jQuery does. The entire logic that combines the jQuery modules into one file is in build/tasks/build.js.
This file defines a custom configuration that it passes to r.js. The important option are:
out which is set to "dist/jquery.js". This is the single
file produced by the optimization.
wrap.startFile which is set to "src/intro.js". This file
will be prepended to dist/jquery.js.
wrap.endFile which is set to "src/outro.js". This file will
be appended to dist/jquery.js.
onBuildWrite which is set to convert. This is a custom function.
The convert function is called every time r.js wants to output a module into the final output file. The output of that function is what r.js writes to the final file. It does the following:
If a module is from the var/ directory, the module will be
transformed as follows. Let's take the case of
src/var/toString.js:
define([
"./class2type"
], function( class2type ) {
return class2type.toString;
});
It will become:
var toString = class2type.toString;
Otherwise, the define(...) call is replace with the contents of the callback passed to define, the final return statement is stripped and any assignments to exports are stripped.
I've omitted details that do not specifically pertain to your question.
You can use a tool called AMDClean by gfranko https://www.npmjs.org/package/amdclean
It's much simpler than what jQuery is doing and you can set it up quickly.
All you need to do is to create a very abstract module (the one that you want to expose to global scope) and include all your sub modules in it.
Another alternative that I've recently been using is browserify. You can export/import your modules the NodeJS way and use them in any browser. You need to compile them before using it. It also has gulp and grunt plugins for setting up a workflow. For better explanations read the documentations on browserify.org.

How to specify wildcards in sonar-project.properties

I am trying to use SonarQube to scan the UI modules I have. The UI modules are lot in number. They have a common structure. Each module has its own JS files.
I need to specify the sonar.sources value to match all JS files in my project. Is it possible to do something like this?
sonar.sources=\*/*/script
sonar.language=js
I used these. But, I got an error saying something like "unable to resolve path". Can someone help?
Try to use wildcard :
* Match zero or more characters
** Match zero or more directories
? Match a single character
Like this:
sonar.sources=**/script
Update
As of 2019, the sonar.sources parameter doesn't support such glob patterns. The common practice is to set this value to a list of directories that contain source code. The scanner will find traverse the directory trees and run applicable analyzers (JavaScript analyzers will consume .js files, Python analyzers will consume .py files, and so on.)
Thanks all. I used sonar.sources=. in my properties file. This properties file is sitting next to my modules. So, now SonarQube takes into account all the folders next to this file, and scans for the specified file extensions. It works now.
sonar.sources does not currently support wildcard at all. This cannot be done.

Excluding files from jslint4java in eclipse

I'm using jslint4java in eclipse. Unfortunately I have a few huge dictionary files that never change but every time when the workspace is built, linting those files takes ages.
Is it possible to exclude specific files from jslint4java and how can this be configured?
In the version of the jslint4java Eclipse plug-in that I have (1.0.1.201207042009) there is an "Exclude files that match these patterns from JSLint:" field in the jslint4java preferences; I believe this is exactly what you are looking for.
It's below the "Make JSLint Laxer" list, on the right; if you have a long list of predefined global variables (as I do) you may have to scroll to see it.
When you enter an exclusion pattern as mentioned above you need to disable and re-enable jslint on the project for it to take effect. Cost me an hour...
Another note on excluding files using that pattern option. In my case I wanted to exclude every file that ended in .min.js. So, naturally I assumed the pattern would be *.min.js.
WRONG!
The pattern was just .min.js

Categories

Resources