使用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 相关文章推荐
javaScript Array(数组)相关方法简述
Jul 25 Javascript
javascript中的toFixed固定小数位数 简单实例分享
Jul 12 Javascript
asm.js使用示例代码
Nov 28 Javascript
node.js+express制作网页计算器
Jan 17 Javascript
jQuery Easyui学习之datagrid 动态添加、移除editor
Jan 27 Javascript
js实现短信发送倒计时功能(正则验证)
Feb 10 Javascript
详解Vue使用 vue-cli 搭建项目
Apr 20 Javascript
浅谈vue2 单页面如何设置网页title
Nov 08 Javascript
基于vue-upload-component封装一个图片上传组件的示例
Oct 16 Javascript
jQuery实现的鼠标拖动画矩形框示例【可兼容IE8】
May 17 jQuery
ES6中new Function()语法及应用实例分析
Feb 19 Javascript
React列表栏及购物车组件使用详解
Jun 28 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解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题
2014/06/19 PHP
php判断当前用户已在别处登录的方法
2015/01/06 PHP
Laravel多用户认证系统示例详解
2018/03/13 PHP
关于实现代码语法标亮 dp.SyntaxHighlighter
2007/02/02 Javascript
Ext JS 4实现带week(星期)的日期选择控件(实战二)
2013/08/21 Javascript
js 时间函数应用加、减、比较、格式转换的示例代码
2013/08/23 Javascript
jQuery选择器全面总结
2014/01/06 Javascript
PHP守护进程实例
2015/03/06 Javascript
javascript框架设计之种子模块
2015/06/23 Javascript
客户端验证用户名和密码的方法详解
2016/06/16 Javascript
基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一)
2016/06/22 Javascript
原生JS取代一些JQuery方法的简单实现
2016/09/20 Javascript
JS实现的全排列组合算法示例
2017/10/09 Javascript
swiper移动端轮播插件(触碰图片之后停止轮播)
2017/12/28 Javascript
微信小程序按钮去除边框线分享页面功能
2018/08/27 Javascript
简单了解JavaScript异步
2019/05/23 Javascript
JavaScript arguments.callee作用及替换方案详解
2020/09/02 Javascript
JS实现页面侧边栏效果探究
2021/01/08 Javascript
python Django模板的使用方法(图文)
2013/11/04 Python
Python写的英文字符大小写转换代码示例
2015/03/06 Python
python中利用h5py模块读取h5文件中的主键方法
2018/06/05 Python
Tensorflow 实现修改张量特定元素的值方法
2018/07/30 Python
python取数作为临时极大值(极小值)的方法
2018/10/15 Python
Python网页正文转换语音文件的操作方法
2018/12/09 Python
python爬虫神器Pyppeteer入门及使用
2019/07/13 Python
Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
2020/01/25 Python
社区活动邀请函范文
2014/01/29 职场文书
家居饰品店创业计划书
2014/01/31 职场文书
科学发展观标语
2014/10/08 职场文书
2014年小学体育工作总结
2014/12/11 职场文书
离婚协议书范文
2015/01/26 职场文书
少先队中队工作总结
2015/08/14 职场文书
2016年元旦寄语
2015/08/17 职场文书
MySQL连表查询分组去重的实现示例
2021/07/01 MySQL
MySQL系列之四 SQL语法
2021/07/02 MySQL
python树莓派通过队列实现进程交互的程序分析
2021/07/04 Python