Features
The plugin provides Javascript static code analysis with JSLint
The following metrics are supported:
- sizing (files, lines of code)
- duplication
- comments
- cyclomatic complexity (function complexity, file complexity, complexity density)
The plugin scans the following files by default: .js. The file extensions are configurable.
The plugin imports the source code in Sonar, calculates measurements and scans the code for violations, duplications. The checks are configurable in the Sonar rules repository.
Version information
Plugin |
|
|---|---|
0.3 |
1.4.6 |
Usage & Installation
Installation
- Copy the jar into /extensions/plugins/ directory
- Restart Sonar Web server
Analysis
Create a maven pom for your project. Set the following properties:
- sonar.language: js
- sonar.dynamicAnalysis: false
Quick analysis
For an existing Javascript project that is built/packaged with Maven, you might start an analysis by the following command:
You may set the property sonar.branch to make sure the web analysis will be reported in sonar under another project name, e.g. -Dsonar.branch=Javascript.
Please note you can run sonar analysis for an artefact in only one language http://jira.codehaus.org/browse/SONAR-926. So you cannot run a web analysis and a java analysis on the same maven project. The fix is to make a separate maven pom for the web project with a different name.
Configuration
System wide/Project wide settings
Setting |
Key |
Corresponding parameter for JSLint |
|---|---|---|
File suffixes |
sonar.javascript.file.suffixes |
|
Assume a browser |
sonar.javascript.lslint.browser |
browser |
Assume console, alert, ... |
sonar.javascript.lslint.devel |
devel |
Assume a Yahoo Widget |
sonar.javascript.lslint.widget |
widget |
Assume Windows |
sonar.javascript.lslint.windows |
windows |
Assume Rhino |
sonar.javascript.lslint.rhino |
rhino |
Safe Subset |
sonar.javascript.lslint.safe |
safe |
Predefined variables |
sonar.javascript.lslint.predef |
predef |
Maximum number of errors |
sonar.javascript.lslint.maxerr |
maxerr (default value: 50) |
Rules
Rule |
Corresponding parameter for JSLint |
|---|---|
ADsafe rules should be enforced. See http://www.ADsafe.org |
adsafe |
Allow one var statement per function |
onevar |
Do Not Tolerate debugger statements |
!debug |
Do Not Tolerate eval |
!evil |
Do Not Tolerate sloppy line breaking |
!laxbreak |
Do Not Tolerate unfiltered for in |
!forin |
Do Not Tolerate inefficient subscripting |
!sub |
Do Not Tolerate CSS workarounds |
!css |
Do Not Tolerate HTML case |
!cap |
Do Not Tolerate HTML event handlers |
!on |
Do Not Tolerate HTML fragments |
!fragments |
Do Not Tolerate ES5 syntax |
!es5 |
Allow one var statement per function |
onevar |
Disallow undefined variables (has parameter "Predefined ( , separated)") |
undef: true, predef:<set value> |
Disallow dangling _ in identifiers |
nomen |
Disallow == and != |
eqeqeq |
Disallow ++ and – |
plusplus |
Disallow bitwise operators |
bitwise |
Disallow insecure . and [^...] in /RegExp/ |
regexp |
Require Initial Caps for constructors |
newcap |
Require parens around immediate invocations |
immed |
Require "use strict"; |
strict |
Maximum line length |
maxlen |
Strict white space (has parameter "Strict white space indentation") |
white:true, indent:<set value> |
Unsafe character. |
|
Use the array literal notation []. |
|
Use the object literal notation {}. |
|
Missing semicolon. |
|
Mixed spaces and tabs. |
|
Other Rules |
|
Unused names |
|
Do not use wrapper forms |
|
Cyclomatic complexity (has parameter "maximum threshold allowed. Default is 10." |
|
- parameters marked with ! are set if corresponding rule is not enabled
- rules that do not have parameter can not do not have special parameter in JSLint
Plugin Architecture
Code Analysis
JSLint as Javascript static analysis tool. Integrated through jslint4java
Code Duplication
For duplication detection plugin uses CPD with ANTLR Lexer generated from ECMAScript Grammar
Cyclomatic complexity
ANTLR based AST generated with ECMAScript Grammar used to count branching statements
Changelog
Roadmap ideas
- Unit test execution: integrate http://code.google.com/p/js-test-driver/ through http://code.google.com/p/jstd-maven-plugin/
- Code coverage: integrate http://code.google.com/p/js-test-driver/wiki/CodeCoverage

