Automate code review with static code analysis.
TSLint & SonarQube configuration and rules for the Tour of Heroes original Angular tutorial. Overview of ways to speed up the code review process.
Check out this project on stackblitz.
It is always good to ask yourself about the quality of the code, whether it will be easy to maintain, and maybe management also wants to know about it. When the SonarQube report can help, TSLint can check and enforce the high quality code of your peers.
- Angular v. 8.0.0 - The web framework used
- Node.js v. 10.16.3 - JavaScript runtime built
- NPM v. 6.9.0 - Dependency Management
- CircleCI - Continuous Integration
Show instructions on how to get a copy of the project up and running on your local machine with SonarCloud
- VS Code (you will get extensions that I recommend) or other IDE
- Node.js v. 10.16.3 or higher
-
Clone repo
$ git clone https://github.com/crappylime/pure-heroes.git
-
Go to the project root
$ cd pure-heroes
-
Install dependencies
$ npm i
-
Run tests with code coverage
$ npm run test-ci
-
Log in to SonarCloud. You can log in with your Github account.
-
Click analyze new project.
-
Click set up manually and fill out the form.
-
Click analyze your project manually.
-
Click other, Linux and copy values of
Dsonar.login
,Dsonar.projectKey
andDsonar.organization
. -
Paste copied values to
sonar-project.properties
:- sonar.projectKey=crappylime_pure-heroes + sonar.projectKey=YOUR_KEY - sonar.organization=crappylime + sonar.organization=YOUR_ORGANIZATION + sonar.login=YOUR_LOGIN
-
Run sonar
$ npm run sonar
-
Check out the produced SonarQube analysis on SonarCloud.
the standard linter for TypeScript
. The default linting tool for Angular
.
-
Why?
Having lint rules in place means that you will get a nice error when you are doing something that you should not be. This will enforce consistency in your application and readability. Some lint rules even come with fixes to resolve the lint error. If you want to configure your own custom lint rule, you can do that too.
-
The default configuration for
Angular
is specified in the project'stslint.json
file.tslint.json
extends"tslint:recommended"
to include rules recommended for TypeScript projects, they can be found here.The
Angular
tslint.json
file also contains Codelyzer rules that are specific toAngular
:"rulesDirectory": [ "codelyzer" ]
All tslint rules are described here along with the given scheme and example.
The TS rules are divided into 5 categories:
- TS-specific,
- Functionality,
- Maintainability,
- Style,
- Format.
Each rule can have one of the flags: TS Only, Has Fixer, Requires type info.
There is a playground available to test each of them.
-
Custom rule sets from the community
If you follow the Angular Style Guide, there are several npm packages that implement these rules:
-
You can exclude files from linting in the
angular.json
, for example tests:"lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": [ "tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json" ], "exclude": [ - "**/node_modules/**" + "**/node_modules/**", + "**/*.spec.ts" ] }
You can also exclude some files in the
tslint.json
usinglinterOptions?: { exclude?: string[] }
. -
Running tslint
lint
script is defined in thepackage.json
:"lint": "ng lint"
ng lint has several flags. Among them
--fix
that automatically fixes errors for rules marked with Has Fixer.You can run it with
npm
:$ npm run lint
or
$ ng lint
or with tslint cli, for example:
$ tslint ./src/**/*.ts -t verbose
will lint only
ts
files withinsrc
directory and display the errors in verbose format. -
Are default rules not enough?
It depends on your team's workflow. However, if you tend to put the same comment over and over when reviewing the code, you can probably automate it.
Some tslint rules that I found interesting in the angular-tslint-rules: no-implicit-dependencies, prefer-template, typedef, strict-boolean-expressions, no-boolean-literal-compare, arrow-return-shorthand, max-file-line-count, restrict-plus-operands, prefer-conditional-expression, template-cyclomatic-complexity, prefer-readonly.
-
Deprecation
TSLint will be deprecated some time in 2019. See this issue for more details: Roadmap: TSLint → ESLint. If you're interested in helping with the TSLint/ESLint migration, please check out our OSS Fellowship program.
--tslint readme as of 15th October 2019.
-
- platform for continuous inspection of code quality,
- performs automatic reviews with static analysis of code, detects bugs, code smells, security vulnerabilities,
- on 20+ programming languages,
- installed and runs on computers of the person or organization using the software,
- community version available.
-
SonarCloud is the same as SonarQube, but works in the cloud, free for open-source projects - used in this repository.
Show instructions on how to integrate an existing Angular project with Sonar
-
Install sonar-scanner:
$ npm i -D sonar-scanner
-
Install tslint-sonarts:
$ npm i -D tslint-sonarts
-
Add the
sonar
script to yourpackage.json
:"scripts": { "build": "ng build --prod", + "sonar": "sonar-scanner", "e2e": "e2e" }
-
Create
sonar-project.properties
file in the project root, content example:sonar.host.url=https://sonarcloud.io sonar.projectKey=crappylime_pure-heroes sonar.organization=crappylime sonar.projectName=pure-heroes sonar.projectVersion=1.0 sonar.sources=src sonar.sourceEncoding=UTF-8 sonar.ts.tslintconfigpath=tslint.json sonar.exclusions=**/*.spec.ts,**/src/assets/**/*,**/src/favicon.ico,**/src/karma.conf.js sonar.typescript.exclusions=**/main.ts,**/environments/environment*.ts,**/*routing.module.ts sonar.tests.inclusions=**/*.spec.ts sonar.javascript.lcov.reportPaths=coverage/angular.io-example/lcov.info
SonarQube docs:
- Analysis properties
- Narrowing the focus with
sonar.sources
.
-
Do not forget to change in
sonar-project.properties
the following:sonar.host.url
to your host if you have SonarQube deployedsonar.projectKey
sonar.organization
sonar.projectName
- add
sonar.login
if necessary
-
Extend
reports
types withlcov
in thekarma.conf.js
file:coverageInstanbulReporter: {} - reports: ['html', 'lcovonly', 'text-summary'], + reports: ['html', 'lcov', 'lcovonly', 'text-summary'],
-
Extend
tslint.json
with sonar rules for typescript that are documented here:{ - "extends": "tslint:recommended", + "extends": ["tslint:recommended", "tslint-sonarts"],
-
Run tests with code coverage:
$ ng test --code-coverage --no-watch --browsers=ChromeHeadless
-
Trigger the sonar analysis for project:
$ npm run sonar
CI Pipeline example with linter, tests and sonar:
- build that fails on linter
- build that succeeds
This project is licensed under the MIT License - see the LICENSE file for details
- analyzed code from Tour of Heroes App and Tutorial
- inspiration for
sonar-project.properties
content from Isaac Martinez - why customize tslint from 11. Make use of lint rules