hosszubalazs / SonarTS

Static code analyzer for TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SonarTS Build Status NPM version Quality Gate Coverage

Static code analyzer for TypeScript detecting bugs and suspicious patterns in your code.

How does it work?

Rules

Use in TSLint

  • If you don't have TSLint yet configured for your project follow these instructions.
  • Install tslint-sonarts
npm install tslint-sonarts      # install in your project
npm install tslint-sonarts -g   # or install globally
  • Add tslint-sonarts to your tslint.json extends property:
{
  "extends": ["tslint:recommended", "tslint-sonarts"]
}
  • Some of the rules in SonarTS require type information. So in order to provide as much value as possible run TSLint with type-checker, for example:
tslint --type-check --project tsconfig.json -c tslint.json 'src/**/*.ts'

Use in SonarQube

SonarTS is available as plugin for SonarQube. SonarQube is an open source platform for continuous inspection of code quality. Thanks to the platform, SonarTS provides additional features:

  • Code coverage import
  • Duplication detection
  • Different metrics
  • More rules

See the documentation here and example project here.

Also available online on ☁️ sonarcloud.io

Contribution

Create New Rule

  • Create file for rule implementation in src/rules. File name should start with lower case and have suffix Rule
  • Create test folder in test/rules with the name of the rule file
  • In this folder create files <rule file name>.test.ts and <rule file name>.lint.ts
  • Run Ruling test
  • Add rule key to tslint-sonarts.json
  • In folder docs/rules create rule documentation file <rule key>.md
  • In README.md add reference to the documentation file.

Testing

The awesome jest test runner is used. There is just a little configuration required to enable TypeScript support.

To run unit tests:

cd sonarts-code
yarn test

To run unit tests in watch mode:

cd sonarts-code
yarn test -- --watch

And finally to run unit tests with coverage:

cd sonarts-code
yarn test -- --coverage

When you run tests with coverage, the coverage/ directory will be created at the root. You can open the web version of the coverage report coverage/lcov-report/index.html to see which lines are covered by tests.

To run unit tests for SQ plugin part of the project

cd sonarts-sq-plugin
mvn clean install

To build SQ plugin part of the project

./build.sh

Ruling

The ruling test is a special integration test which launches the analysis of a large code base, and then compares those results to the set of expected issues (stored as snapshot files). To have this code base locally:

 git submodule init
 git submodule update

To run the ruling test:

cd sonarts-code
yarn ruling
yarn ruling -- --rule <RuleFileName> # to run ruling for a single rule
yarn ruling -- --update # to update the snapshots
yarn ruling -- --rule <RuleFileName> --update # it is possible to combine both options

Tools we use

  • Visual Studio Code
  • Prettier (and its extension for VSCode)

About

Static code analyzer for TypeScript

License:GNU Lesser General Public License v3.0


Languages

Language:TypeScript 53.7%Language:Java 35.2%Language:HTML 9.3%Language:Shell 1.0%Language:JavaScript 0.8%