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.
Related
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
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.
Using Google Closure Compiler to minify a bunch of javascripts. Now I'd like to also add source maps to those to debug out in the wild.
Thing is, I want to keep the original (and preferrably also the map files) on a completely different place, like another server. I've been looking for a solution to this, and found out about the sourceRoot parameter. But it seems as it's not supported?
Also found this --source_map_location_mapping parameter, but no documentation whatsoever. Seems as it wants a pipe-delimited argument (filesystem-path|webserver-path). Tried a couple of different approaches to this, like local filename|remote url but without prevail. That just gives me No such file or directory and java.lang.ArrayIndexOutOfBoundsException.
Has anyone succeeded to place the minified/mapped source files on a remote machine?
Or does anyone know of any documentation for --source_map_location_mapping?
Luckily Google Closure Compiler's source code is available publicly
https://gist.github.com/lydonchandra/b97b38e3ff56ba8e0ba5
REM --source_map_location_mapping is case SENSITIVE !
REM need extra escaped double quote --source_map_location_mapping="\"C:/tools/closure/^|httpsa://bla/\"" as per http://stackoverflow.com/a/29542669
java -jar compiler.jar --compilation_level=SIMPLE_OPTIMIZATIONS --create_source_map=C:\tools\closure\latest\maplayer.js.map --output_wrapper "%output%//# sourceMappingURL=maplayer.js.map" --js=C:\tools\closure\mapslayer.js --js_output_file=maplayer.min.js --source_map_location_mapping="\"C:/tools/closure/^|httpsa://bla/\""
The flag should be formatted like so:
--source_map_location_mapping=foo/|http://bar
The flag should be repeated if you need multiple locations:
--source_map_location_mapping=foo/|http://bar --source_map_location_mapping=xxx/|http://yyy
But what I expect that you are running into is that the "|" might be interpreted by your command shell. For example:
echo --source_map_location_mapping=foo/|http://bar
-bash: http://bar: No such file or directory
(The choice to use "|" was unfortunate). Make sure it is escaped appropriately. like:
--source_map_location_mapping="foo/|http://bar"
I submitted a pull request to report an error for badly formatted flag values:
https://github.com/google/closure-compiler/pull/620
which will at least you know that your flag value is incorrect (so you won't see the out of bounds exception).
John is correct functionality-wise, but I think I can clear it up a bit (as this was super confusing for me to get working).
I suspect many people have the same issue as I:
source map urls are generated relative to your current directory
they don't necessarily match up to relative urls on your website/server
Even if they did match up directly, the strangely-defined pseudo-spec found here means that Chrome/Firefox are going to try to load your paths relative to your sourcemap. i.e. the browser loads /assets/sourcemaps/main.map, sees assets/js/main.js, and loads /assets/sourcemap/assets/js/main.js (yay). (Or it might be relative to the original js file actually, I just happened to have them in the same directory).
Let's use the above example. Say we have assets/js/main.js in our sourcemap, and want to make sure that loads mywebsite.com/assets/js/main.js. To do this, you'd pass the option:
--source_map_location_mapping="assets|/assets"
Like John mentioned, quotes are important, and repeat the arg multiple times for multiple options. The prefixed / will let Firefox/Chrome know you want it relative to your website root. (If you're doing this in something like grunt-closure-tools you'll need to escape more:
config:{
source_map_location_mapping:"\"assets|/assets\"",
}
This way, we can essentially map any given sourcemap path to any given website path. It's not really a perfect replacement for some sort of closure source root, but it does let you map each section of your sources individually to their own roots, so it's not that bad a compromise, and does give some additional flexibility (i.e. you could specify some cdn paths for some of your assets but not for other).
An additional thing you might find helpful, you can automatically add the sourceMappingURL via an output_wrapper. (Though, if you want the ability to debug in production, you should probably prefer some ability to make the server return X-Sourcemap: blah.js.map headers instead, inaccessible by the public)
--output_wrapper="(function(){%output%}).call(this); //# sourceMappingURL=/assets/js/my_main_file.js.map"
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
I have two 'define' in two separate js files.
def1.js and def2.js
define("mydefname1",["file1",...]});
define("mydefname2",["file2",....]});
I have another require statement where i check if the two definetions are loaded.
require(['def1','def2'], function(){alert('loaded')});
this works fine..
but if I try
require(['mydefname1','mydefname2'], function(){alert('loaded')});,
it does not work.
Is there a way I could actually use mydefname1 and mydefname2.. i.e. the module name to load them, and not the file name?
As far as I know, that's not possible. What I usually do is format my file names to include my module name, so I can include it that way.
For a metaclass name Overlay, the file name would be class.Overlay.js and my require/include functions would take my module name and build the file name from it.
As I understand it, RequireJS recommends that you avoid manually assigning module names - see here: http://requirejs.org/docs/api.html#modulename
FWIW, have you played around with the "path" configuration option? I don't think you can use it to do exactly what you are looking to do but it may offer an acceptable alternate approach. See here: http://requirejs.org/docs/api.html#config
Hope it helps!