使用Sonarqube扫描Javascript代码的示例


Posted in Javascript onDecember 26, 2018

使用sonarqube对javascript代码进行扫描,分析代码质量,最简单的方式莫过于使用缺省的sonar-way中的javascript的规则,使用sonar-scanner进行扫描,这篇文章通过最简单的例子,来进行说明。

事前准备

Sonarqube

Sonarqube可以使用docker版本快速搭建,可以参看一下Easypack整理的镜像,具体使用可以参看如下链接,这里不再赘述:

https://hub.docker.com/r/liumiaocn/sonarqube/

环境假定

本文使用到的sonarqube为本机32003可以访问到的服务。版本为5.6.5

sonar-scanner

sonar-scanner详细信息如下:

liumiaocn:sonar liumiao$ sonar-scanner -v
INFO: Scanner configuration file: /Users/liumiao/Desktop/sonar/sonar-scanner-3.2.0.1227-macosx/conf/sonar-scanner.properties
INFO: Project root configuration file: /Users/liumiao/sonar/sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Mac OS X 10.14 x86_64
liumiaocn:sonar liumiao$

项目目录构成

项目文件与目录构成信息如下:

liumiaocn:sonar liumiao$ tree
.
├── sonar-project.properties
└── src
  └── Person.js
1 directory, 2 files
liumiaocn:sonar liumiao$

JavaScript源码

使用如下Person.js的JavaScript源码,详细信息如下

liumiaocn:sonar liumiao$ cat src/Person.js 
var Person = function(first, last, middle) {
  this.first = first;
  this.middle = middle;
  this.last = last;
};
Person.prototype = {
  whoAreYou : function() {
    return this.first + (this.middle ? ' ' + this.middle: '') + ' ' + this.last;
  }
};
var a = NaN;
if (a === NaN) { // Noncompliant; always false
 console.log("a is not a number"); // this is dead code
}
if (a !== NaN) { // Noncompliant; always true
 console.log("a is not NaN"); // this statement is not necessarily true
}
for (var i = 0; i < strings.length; i--) {
 console.log("dead code")
}
if (str == null && str.length == 0) {
 console.log("String is empty");
}
liumiaocn:sonar liumiao$

sonar-project.properties设定文件

项目设定文件信息详细如下:

liumiaocn:sonar liumiao$ cat sonar-project.properties 
sonar.projectKey=javascript-prj
sonar.projectName=JavaScript Demo Project
sonar.projectVersion=1.0
sonar.sources=src
sonar.host.url=http://127.0.0.1:32003
sonar.login=admin
sonar.password=admin
liumiaocn:sonar liumiao$

执行sonar-scanner

liumiaocn:sonar liumiao$ pwd
/Users/liumiao/sonar
liumiaocn:sonar liumiao$ ls
sonar-project.properties src
liumiaocn:sonar liumiao$ sonar-scanner
INFO: Scanner configuration file: /Users/liumiao/Desktop/sonar/sonar-scanner-3.2.0.1227-macosx/conf/sonar-scanner.properties
INFO: Project root configuration file: /Users/liumiao/sonar/sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Mac OS X 10.14 x86_64
INFO: User cache: /Users/liumiao/.sonar/cache
INFO: SonarQube server 5.6.5
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global repositories
INFO: Load global repositories (done) | time=129ms
INFO: User cache: /Users/liumiao/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=3ms
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=126ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=41ms
INFO: Load active rules
INFO: Load active rules (done) | time=609ms
WARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
INFO: Publish mode
INFO: ------------- Scan JavaScript Demo Project
INFO: Load server rules
INFO: Load server rules (done) | time=73ms
INFO: Base dir: /Users/liumiao/sonar
INFO: Working dir: /Users/liumiao/sonar/.scannerwork
INFO: Source paths: src
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 1 files indexed
INFO: Quality profile for js: Sonar way
INFO: JaCoCoSensor: JaCoCo report not found : /Users/liumiao/sonar/target/jacoco.exec
INFO: JaCoCoItSensor: JaCoCo IT report not found: /Users/liumiao/sonar/target/jacoco-it.exec
INFO: Sensor Lines Sensor
INFO: Sensor Lines Sensor (done) | time=11ms
INFO: Sensor JavaScriptSquidSensor
INFO: 1 source files to be analyzed
INFO: Sensor JavaScriptSquidSensor (done) | time=200ms
INFO: 1/1 source files have been analyzed
INFO: Sensor SCM Sensor
INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: Sensor SCM Sensor (done) | time=0ms
INFO: Sensor org.sonar.plugins.javascript.lcov.UTCoverageSensor
INFO: Sensor org.sonar.plugins.javascript.lcov.UTCoverageSensor (done) | time=0ms
INFO: Sensor org.sonar.plugins.javascript.lcov.ITCoverageSensor
INFO: Sensor org.sonar.plugins.javascript.lcov.ITCoverageSensor (done) | time=0ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=7ms
INFO: Sensor Code Colorizer Sensor
INFO: Sensor Code Colorizer Sensor (done) | time=0ms
INFO: Sensor CPD Block Indexer
INFO: DefaultCpdBlockIndexer is used for js
INFO: Sensor CPD Block Indexer (done) | time=20ms
INFO: Calculating CPD for 1 files
INFO: CPD calculation finished
INFO: Analysis report generated in 53ms, dir size=13 KB
INFO: Analysis reports compressed in 17ms, zip size=6 KB
INFO: Analysis report uploaded in 29ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://127.0.0.1:32003/dashboard/index/javascript-prj
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://127.0.0.1:32003/api/ce/task?id=AWcNw2JuTV5bsL-6UV7v
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 3.719s
INFO: Final Memory: 19M/278M
INFO: ------------------------------------------------------------------------
liumiaocn:sonar liumiao$

确认结果

代码扫描整体结果

使用Sonarqube扫描Javascript代码的示例

详细代码级别的扫描结果展示

使用Sonarqube扫描Javascript代码的示例

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Javascript 相关文章推荐
前淘宝前端开发工程师阿当的PPT中有JS技术理念问题
Jan 15 Javascript
同时使用n个window onload加载实例介绍
Apr 25 Javascript
JS事件在IE与FF中的区别详细解析
Nov 20 Javascript
ExtJS4 动态生成的grid导出为excel示例
May 02 Javascript
JavaScript中的闭包
Feb 24 Javascript
一波JavaScript日期判断脚本分享
Mar 06 Javascript
Bootstrap Table服务器分页与在线编辑应用总结
Aug 08 Javascript
JavaScript中最常用的10种代码简写技巧总结
Jun 28 Javascript
Vue2.0如何发布项目实战
Jul 27 Javascript
webpack多入口文件页面打包配置详解
Jan 09 Javascript
vuex直接赋值的三种方法总结
Sep 16 Javascript
vue的路由映射问题及解决方案
Oct 14 Javascript
angular6的table组件开发的实现示例
Dec 26 #Javascript
详解VUE里子组件如何获取父组件动态变化的值
Dec 26 #Javascript
JavaScript基础之静态方法和实例方法分析
Dec 26 #Javascript
微信小程序实现文字跑马灯
May 26 #Javascript
基于JavaScript canvas绘制贝塞尔曲线
Dec 25 #Javascript
基于js Canvas实现二次贝塞尔曲线
Dec 25 #Javascript
JavaScript实现小球沿正弦曲线运动
Sep 07 #Javascript
You might like
php 注释规范
2012/03/29 PHP
PHP实现的多彩标签效果代码分享
2014/08/21 PHP
php中Array2xml类实现数组转化成XML实例
2014/12/08 PHP
javascript 清空form表单中某种元素的值
2009/12/26 Javascript
ext jquery 简单比较
2010/04/07 Javascript
JS判断表单输入是否为空(示例代码)
2013/12/23 Javascript
JS模式之简单的订阅者和发布者模式完整实例
2015/06/30 Javascript
ionic实现带字的toggle滑动组件
2016/08/27 Javascript
javascript 分号总结及详细介绍
2016/09/24 Javascript
jQuery双向列表选择器select版
2016/11/01 Javascript
Vue-router路由判断页面未登录跳转到登录页面的实例
2017/10/26 Javascript
Vue利用History记录上一页面的数据方法实例
2018/11/02 Javascript
JQuery绑定事件四种实现方法解析
2020/12/02 jQuery
[02:54]DOTA2英雄基础教程 暗影牧师戴泽
2013/12/05 DOTA
编写Python脚本来实现最简单的FTP下载的教程
2015/05/04 Python
Python加pyGame实现的简单拼图游戏实例
2015/05/15 Python
python中zip和unzip数据的方法
2015/05/27 Python
Python获取某一天是星期几的方法示例
2017/01/17 Python
python验证码识别教程之滑动验证码
2018/06/04 Python
Pandas读取并修改excel的示例代码
2019/02/17 Python
python中将两组数据放在一起按照某一固定顺序shuffle的实例
2019/07/15 Python
详解python3 GUI刷屏器(附源码)
2021/02/18 Python
小学生打架检讨书
2014/01/26 职场文书
化学专业毕业生求职信
2014/07/28 职场文书
感恩祖国演讲稿
2014/09/09 职场文书
离婚协议书包括哪些内容
2014/10/16 职场文书
2015年党支部公开承诺书
2015/01/22 职场文书
南湾猴岛导游词
2015/02/09 职场文书
幼儿园毕业致辞
2015/07/29 职场文书
我爱我班主题班会
2015/08/13 职场文书
诚实守信主题班会
2015/08/13 职场文书
如何利用js在两个html窗口间通信
2021/04/27 Javascript
使用react+redux实现计数器功能及遇到问题
2021/06/02 Javascript
go开发alertmanger实现钉钉报警
2021/07/16 Golang
html5调用摄像头截图功能
2022/01/18 Javascript
GoFrame框架数据校验之校验结果Error接口对象
2022/06/21 Golang