# sonarjs

> SonarJS npm module

Latest version **1.0.0** (published 2017-12-13) · LGPL-3.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install sonarjs
pnpm add sonarjs
yarn add sonarjs
bun add sonarjs
```

Provides the command `sonarjs`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-12-13 |
| First published | 2013-01-10 |
| Weekly downloads | 0 |
| License | LGPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 9 |
| Maintainers | elena-vilchik, inverno |
| Keywords | sonarjs, analyzer |

## Links

- npm: https://www.npmjs.com/package/sonarjs
- Repository: https://github.com/SonarSource/sonarjs-cli
- Issues: https://github.com/SonarSource/sonarjs-cli/issues
- npm.io page: https://npm.io/package/sonarjs

## Dependencies (7)

- [yargs](https://npm.io/package/yargs.md) ^10.0.3
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [tar-fs](https://npm.io/package/tar-fs.md) ^1.16.0
- [request](https://npm.io/package/request.md) ^2.83.0
- [progress](https://npm.io/package/progress.md) ^2.0.0
- [@types/yargs](https://npm.io/package/@types/yargs.md) ^10.0.0
- [unzip-stream](https://npm.io/package/unzip-stream.md) ^0.2.1

## Recent versions

- 1.0.0 (latest) — 2017-12-13
- 1.0.0-0 (next) — 2017-12-13
- 0.2.0 — 2017-12-12
- 0.2.0-8 — 2017-12-12
- 0.2.0-7 — 2017-12-08
- 0.2.0-6 — 2017-12-08
- 0.2.0-5 — 2017-12-08
- 0.2.0-4 — 2017-12-08
- 0.2.0-3 — 2017-12-08
- 0.2.0-2 — 2017-12-06
- 0.2.0-1 — 2017-12-06
- 0.2.0-0 — 2017-12-06
- 0.1.0 — 2013-01-10

## README

# SonarJS [![Build Status](https://travis-ci.org/SonarSource/sonarjs-cli.svg?branch=master)](https://travis-ci.org/SonarSource/sonarjs-cli) [![NPM version](https://badge.fury.io/js/sonarjs.svg)](http://badge.fury.io/js/sonarjs)

[This package](https://www.npmjs.com/package/sonarjs) allows to use the [SonarJS](https://github.com/SonarSource/sonarjs) analyzer as a command line tool and as a JavaScript/TypeScript library.

[SonarJS](https://github.com/SonarSource/sonarjs) is a [static code analyser](https://en.wikipedia.org/wiki/Static_program_analysis) for the JavaScript language. It will allow you to produce stable and easily supported code by helping you to find and to correct bugs, vulnerabilities and code smells in your code.

It provides ~80 rules (including ~40 bug detection), which represent the ["Sonar Way"](https://github.com/SonarSource/SonarJS/blob/master/docs/DOC.md#sonar-way-profile) profile of the SonarJS analyzer.

## Usage
### As a Command Line Tool
* Install SonarJS
```
> npm install -g sonarjs
```
* Run analysis from the project directory
```
> cd <directory of project to analyze>
> sonarjs
```
or
```
> sonarjs -h
```
for more information
* As the result you will get the list of issues found in the project. E.g.
```
MAJOR - S3923: /Users/path/to/foo/file.js [3, 6]: Remove this conditional structure or edit its code blocks so that they're not all the same.
BLOCKER - OctalNumber: /Users/path/to/foo/file.js [3, 10]: Replace the value of the octal number (056) by its decimal equivalent (46).
```
* Note that some code editors (e.g. VS Code) make SonarJS output clickable and you can easily move to the referenced file and line.

![SonarJS in VS Code terminal](/img/vscode.png?raw=true "SonarJS in VS Code terminal")

### As a JavaScript/TypeScript Library
* Install SonarJS in your project
```
> npm install sonarjs
```
* Import it
```typescript
// for TypeScript
import { analyze, Issue } from "sonarjs";

// for JavaScript
const { analyze } = require("sonarjs");
```
* Analyze
```typescript
async function runSonarJS() {
  const issues = await analyze("/path/to/project");
  issues.forEach(issue => {
    // ...
  });
}
```

* Provide options to inject callbacks for `analyze` function to collect logs and to add some behavior before and after analysis
```typescript
function log(message: string) {
  console.log(message);
}

function onStart() {
  console.log("Analysis is started");
}

function onEnd() {
  console.log("Analysis is finished");
}

async function runSonarJS() {
  const issues = await analyze("/path/to/project", { log, onStart, onEnd });
  // ...
}
```

* Provide options to exclude folders from analysis
```typescript
async function runSonarJS() {
  const issues = await analyze("/path/to/project", { exclusions: "**/tests/**" });
  // ...
}
```

## Java Environment
Note that [SonarJS](https://github.com/SonarSource/sonarjs) requires a JRE, so if it's not available on your machine (Java 1.8 version) it will be downloaded during the first analysis.

---
_Source: https://npm.io/package/sonarjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
