在我们的工作中,我们积极使用SonarQube平台来维护高质量的代码。集成用VueJs + Typescript编写的项目之一时出现问题。因此,我想更详细地介绍我们如何解决这些问题。
正如我在上面所写,本文将讨论SonarQube平台。一点理论-对于第一次听说它的人有什么意义:
SonarQube(以前的Sonar)是一个开源平台,用于连续分析(英语连续检查)和代码质量测量。
根据编程标准MISRA C,MISRA C ++,MITRE / CWE和CERT安全编码标准的规则,支持代码分析和错误搜索。它还可以从OWASP Top-10和CWE / SANS Top-25编程错误列表中识别错误。
尽管平台使用了各种现成的工具,但SonarQube将结果减少到单个信息面板(仪表板)中,保留了运行历史,从而使您可以看到软件质量在开发过程中总体变化的趋势。
更多信息可以在官方网站上找到
. — 25 . . community- Javascript ( typesript), wiki . Javascript SonarJS, Typescript SonarTS .
sonarqube-scanner, , config-, SonarQube .
Javascript npm-. , SonarQube Vue-, Typescript.
SonarQube docker-compose.
sonar.yaml:
version: '1'
services:
simplesample-sonar:
image: sonarqube:lts
ports:
- 9001:9000
- 9092:9092
network_mode: bridge
:
docker-compose -f sonar.yml up
SonarQube – http://localhost:9001 .
![](https://habrastorage.org/webt/o1/p2/j1/o1p2j1ch1vx5yzpepkkguvr5m1m.png)
. . - VueJS+TS+Jest. :
git clone https://github.com/vuejs/vue-test-utils-typescript-example.git
SonarQube, sonar-scanner, npm :
yarn add sonarqube-scanner
scripts .
package.json:
{
…
scripts: {
...
"sonar": "sonar-scanner"
...
},
…
}
, , . .
sonar-project.properties:
sonar.host.url=http://localhost:9001
sonar.projectKey=test-project-vuejs-ts
sonar.projectName=Test Application (VueJS+TS)
sonar.sources=src
# sonar.tests=
sonar.test.inclusions=src/**/*tests*/**
sonar.sourceEncoding=UTF-8
- sonar.host.url – Sonar’;
- sonar.projectKey – Sonar’;
- sonar.projectName – , , projectKey;
- sonar.sources – , src, . , ;
- sonar.tests – , . , . , , 'test', ;
- sonar.test.inclusions – , ;
- sonar.sourceEncoding – .
, : , , .
. — Jest. package.json.
:
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*",
"!src/main.ts",
"!src/App.vue",
"!src/**/*.d.*",
"!src/**/*__tests__*"
],
( ), .
:
yarn test
:
![](https://habrastorage.org/webt/cd/yr/tc/cdyrtcot4y3gqoilyqh3-bontge.png)
, , , . .
HelloWorld.vue:
...
methods: {
calc(n) {
return n + 1;
}
},
mounted() {
this.msg1 = this.msg + this.calc(1);
},
...
.
:
![](https://habrastorage.org/webt/sy/_f/tv/sy_ftvp-hmlgpdg1l-axz6_foeg.png)
, coverage LCOV (LTP GCOV extension).
Gcov — . Gcov . Gcov GCC.
Lcov — gcov. gcov HTML . . Lcov , , .
coverage/lcov.info.
Sonar’ . . : , src , , . , . Typescript, :
sonar-project.properties:
sonar.typescript.coveragePlugin=lcov
sonar.typescript.lcov.reportPaths=coverage/lcov.info
. , Sonar’ . , .
, , package.json:
yarn run sonar
: -X .
, . Sonar’ , . : quality profiles, active rules, metrics repository, server rules.
![](https://habrastorage.org/webt/z4/rv/4i/z4rv4ir9q2va0bfvdmgqfgk1dyy.png)
![](https://habrastorage.org/webt/if/bw/1v/ifbw1voz_wvpy94bxxgoxesautg.png)
: , .
src ( - ) , .
![](https://habrastorage.org/webt/ut/9p/yf/ut9pyfqprjxnlo1lth7kxtfecd4.png)
, (, : , ).
, .
-:
![](https://habrastorage.org/webt/rh/3g/wv/rh3gwvc1zusg7ja4llxj8aqa6r0.png)
, - , - , Jest-.
. , , "" :
![](https://habrastorage.org/webt/y1/o2/ks/y1o2ksf7tbjvpq28xxwivuzmp6o.png)
, HelloWorld.vue, main.ts, . , . , , Jest, , .
:
sonar-project.properties:
...
sonar.exclusions=src/main.ts
...
: , , , sonar.test.inclusions.
:
![](https://habrastorage.org/webt/yo/rl/2y/yorl2ywoqleixwgvvko1uk9vzsy.png)
![](https://habrastorage.org/webt/2q/ue/c1/2quec1pejqtnqhqv6zzusbn_zbm.png)
– Quality profiles. Sonar’ . . , TS, . Sonar’:
sonar-project.properties:
...
sonar.language=ts
...
:
![](https://habrastorage.org/webt/uo/zw/hm/uozwhme74zb3w35slbqfif1canq.png)
.
, :
![](https://habrastorage.org/webt/ow/6y/3r/ow6y3rk4ol9lapzjqpw7jezdhlw.png)
.
: VueJs SonarJS, Javascript.
![](https://habrastorage.org/webt/v6/d7/v5/v6d7v5fbgmqcxlvf-dtrtn0mexi.png)
SonarTS TS, - Sonar’:
- https://jira.sonarsource.com/browse/MMF-1441
- https://github.com/SonarSource/SonarJS/issues/1281
SonarQube, .
![](https://habrastorage.org/webt/xg/x8/nd/xgx8ndb5tf4twrsugdlnyivtfue.png)
![](https://habrastorage.org/webt/ok/zs/fe/okzsfe44d7mkf-vyarc5o6hrnkg.png)
, . , , “”.
.vue- Sonar’, Typescript.
:
sonar-project.properties:
...
sonar.typescript.file.suffixes=.ts,.tsx,.vue
...
:
![](https://habrastorage.org/webt/we/s1/tb/wes1tbidslczd_tuussrclhkuau.png)
, , , Typescript. VueJs+TS SonarQube.
.
:
- Sonar-;
- Jest ;
- Sonar-;
- .vue- + Typescript.
, , ( , ) .
TS (SonarTS) CPD (Copy Paste Detector) .vue-.
, , main.ts - . .vue, .ts -.
main.ts:
...
function name(params:string): void {
console.log(params);
}
...
:
sonar-project.properties:
...
sonar.exclusions=src/main.ts
...
:
yarn test && yarn run sonar
, .
:
![](https://habrastorage.org/webt/49/nh/vj/49nhvjrianczcgtekbdrdzqs7l8.png)
CPD- – jscpd:
npx jscpd src
![](https://habrastorage.org/webt/i3/ed/rn/i3edrnicgi5l9lqlka49oqieqie.png)
:
![](https://habrastorage.org/webt/jr/9j/bc/jr9jbc1ykaviawjq6nr07krdxyi.png)
, SonarJS(TS). , SonarJS, , , .
.
, , . unit- , .
, Jest- Sonar’:
generic test data — https://docs.sonarqube.org/display/SONAR/Generic+Test+Data.
:
yarn add jest-sonar-reporter
Jest:
package.json:
…
"testResultsProcessor": "jest-sonar-reporter"
…
:
yarn test
test-report.xml.
Sonar’:
sonar-project.properties:
…
sonar.testExecutionReportPaths=test-report.xml
…
:
yarn run sonar
, Sonar’:
![](https://habrastorage.org/webt/jf/-e/ie/jf-eieyizhxgkfbituwjt3e8ewg.png)
. , Sonar , Jest-, unit-. , Sonar sonar.tests, ( ):
sonar-project.properties:
…
sonar.tests=src/components/__tests__
…
:
yarn run sonar
, :
![](https://habrastorage.org/webt/1l/u0/wc/1lu0wc3onjmwnf8d2ed2osxldcy.png)
unit- , , :
![](https://habrastorage.org/webt/nk/ub/jo/nkubjo6ofzlpawfbcm2npzktjqu.png)
, SonarQube. , VueJs+TS. . . (, ), SonarQube , . community-. — SonarQube , , GitLab BitBucket. merge pull(merge) request’ . .
PS: , .