Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Add prettier as pre-commit hook #432

Merged
merged 9 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ env:

SONAR_TOKEN: VAULT[development/kv/data/sonarcloud data.token]
ARTIFACTORY_URL: https://repox.jfrog.io/
ARTIFACTORY_DEPLOY_ACCESS_TOKEN : VAULT[development/artifactory/token/${CIRRUS_REPO_OWNER}-${CIRRUS_REPO_NAME}-qa-deployer access_token]
ARTIFACTORY_PROMOTE_ACCESS_TOKEN : VAULT[development/artifactory/token/${CIRRUS_REPO_OWNER}-${CIRRUS_REPO_NAME}-promoter access_token]
ARTIFACTORY_DEPLOY_ACCESS_TOKEN: VAULT[development/artifactory/token/${CIRRUS_REPO_OWNER}-${CIRRUS_REPO_NAME}-qa-deployer access_token]
ARTIFACTORY_PROMOTE_ACCESS_TOKEN: VAULT[development/artifactory/token/${CIRRUS_REPO_OWNER}-${CIRRUS_REPO_NAME}-promoter access_token]
# Use bash (instead of sh on linux or cmd.exe on windows)
CIRRUS_SHELL: bash

Expand Down Expand Up @@ -75,7 +75,7 @@ ws_scan_task:
folder: ${CIRRUS_WORKING_DIR}/.m2/repository
whitesource_script:
- ./scripts/run_ws_scan.sh
allow_failures: "true"
allow_failures: 'true'
always:
ws_artifacts:
path: "whitesource/**/*"
path: 'whitesource/**/*'
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run precommit
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.github
coverage
lib
javascript-test-sources
12 changes: 6 additions & 6 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Report bugs and suggest improvements. If something doesn't work well for you or

## Create New Rule

* Create a new file for the rule implementation in `src/rules`. File name should be lowercased, words must be separated by dashes (`-`).
* Create a test file `<rule name>.test.ts` in `test/rules`.
* Add the rule to `src/index.ts`.
* In folder `docs/rules` create a rule documentation file `<rule name>.md`
* In `README.md` add a reference to this documentation file.
* Run [Ruling](#ruling) test.
- Create a new file for the rule implementation in `src/rules`. File name should be lowercased, words must be separated by dashes (`-`).
- Create a test file `<rule name>.test.ts` in `test/rules`.
- Add the rule to `src/index.ts`.
- In folder `docs/rules` create a rule documentation file `<rule name>.md`
- In `README.md` add a reference to this documentation file.
- Run [Ruling](#ruling) test.

## Testing

Expand Down
6 changes: 3 additions & 3 deletions docs/RELEASING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Releasing npm package

* [Create a Jira release ticket](https://jira.sonarsource.com/projects/REL) like [this one](https://sonarsource.atlassian.net/browse/REL-2313)
* [Publish a new GitHub release](https://github.com/SonarSource/eslint-plugin-sonarjs/releases/new)
* [Bump the package version for the next development iteration](https://github.com/SonarSource/eslint-plugin-sonarjs/pull/418/files)
- [Create a Jira release ticket](https://jira.sonarsource.com/projects/REL) like [this one](https://sonarsource.atlassian.net/browse/REL-2313)
- [Publish a new GitHub release](https://github.com/SonarSource/eslint-plugin-sonarjs/releases/new)
- [Bump the package version for the next development iteration](https://github.com/SonarSource/eslint-plugin-sonarjs/pull/418/files)
2 changes: 1 addition & 1 deletion docs/rules/cognitive-complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Cognitive Complexity is a measure of how hard the control flow of a function is

## See

* [Cognitive Complexity](http://redirect.sonarsource.com/doc/cognitive-complexity.html)
- [Cognitive Complexity](http://redirect.sonarsource.com/doc/cognitive-complexity.html)

## Configuration

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/elseif-without-else.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (x == 0) {
doSomethingElse();
}
```

## Compliant Solution

```javascript
Expand All @@ -23,7 +24,7 @@ if (x == 0) {
} else if (x == 1) {
doSomethingElse();
} else {
throw "Unexpected value for x";
throw 'Unexpected value for x';
}
```

Expand Down
7 changes: 5 additions & 2 deletions docs/rules/no-all-duplicated-branches.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ or there shouldn't be a `switch`/`if` chain at all. Note that this rule does not
## Noncompliant Code Example

```javascript
if (b == 0) { // Noncompliant
if (b == 0) {
// Noncompliant
doOneMoreThing();
} else {
doOneMoreThing();
}

let a = b === 0 ? getValue() : getValue(); // Noncompliant

switch (i) { // Noncompliant
switch (
i // Noncompliant
) {
case 1:
doSomething();
break;
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-collection-size-mischeck.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# no-collection-size-mischeck

:wrench: *fixable*
:wrench: _fixable_

The size of a collection and the length of an array are always greater than or equal to zero. So testing that a size or length is greater than or equal to zero doesn't make sense, since the result is always `true`. Similarly testing that it is less than zero will always return `false`. Perhaps the intent was to check the non-emptiness of the collection or array instead.

Expand Down
5 changes: 4 additions & 1 deletion docs/rules/no-duplicate-string.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# no-duplicate-string

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

## Exceptions
To prevent generating some false-positives, literals having less than 10 characters are excluded as well as literals matching /^\w*$/. String literals inside import/export statements are also ignored. The same goes for statement-like string literals, e.g. `'use strict';`

To prevent generating some false-positives, literals having less than 10 characters are excluded as well as literals matching /^\w\*$/. String literals inside import/export statements are also ignored. The same goes for statement-like string literals, e.g. `'use strict';`

## Configuration

Expand All @@ -23,3 +25,4 @@ Comma-separated list of strings that must be ignored. Default is `"application/j
"no-duplicate-string": "error",
"no-duplicate-string": ["error", { "ignoreStrings": "foo,bar,baz" }]
}
```
8 changes: 4 additions & 4 deletions docs/rules/no-element-overwrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ It is highly suspicious when a value is saved for a key or index and then uncond
## Noncompliant Code Example

```javascript
fruits[1] = "banana";
fruits[1] = "apple"; // Noncompliant - value on index 1 is overwritten
fruits[1] = 'banana';
fruits[1] = 'apple'; // Noncompliant - value on index 1 is overwritten

myMap.set("key", 1);
myMap.set("key", 2); // Noncompliant - value for key "key" is replaced
myMap.set('key', 1);
myMap.set('key', 2); // Noncompliant - value for key "key" is replaced

mySet.add(1);
mySet.add(1); // Noncompliant - element is already in the set
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-empty-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ When a collection is empty it makes no sense to access or iterate it. Doing so a
```javascript
let strings = [];

if (strings.includes("foo")) {} // Noncompliant
if (strings.includes('foo')) {
} // Noncompliant

for (str of strings) {} // Noncompliant
for (str of strings) {
} // Noncompliant

strings.forEach(str => doSomething(str)); // Noncompliant
```
4 changes: 2 additions & 2 deletions docs/rules/no-extra-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ You can easily call a JavaScript function with more arguments than the function

```javascript
function say(a, b) {
print(a + " " + b);
print(a + ' ' + b);
}

say("hello", "world", "!"); // Noncompliant; last argument is not used
say('hello', 'world', '!'); // Noncompliant; last argument is not used
```

## Exceptions
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-gratuitous-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ because it does not match the programmer’s intent, then it’s a bug and the e

```javascript
if (a) {
if (a) { // Noncompliant
if (a) {
// Noncompliant
vdiez marked this conversation as resolved.
Show resolved Hide resolved
doSomething();
}
}
Expand Down
12 changes: 5 additions & 7 deletions docs/rules/no-identical-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ if (param == 1) {
openWindow();
} else if (param == 2) {
closeWindow();
} else if (param == 1) { // Noncompliant
} else if (param == 1) {
// Noncompliant
vdiez marked this conversation as resolved.
Show resolved Hide resolved
moveWindowToTheBackground();
}

Expand All @@ -34,12 +35,9 @@ switch (param) {
## Compliant Solution

```javascript
if (param == 1)
openWindow();
else if (param == 2)
closeWindow();
else if (param == 3)
moveWindowToTheBackground();
if (param == 1) openWindow();
else if (param == 2) closeWindow();
else if (param == 3) moveWindowToTheBackground();

switch (param) {
case 1:
Expand Down
21 changes: 12 additions & 9 deletions docs/rules/no-identical-expressions.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# no-identical-expressions

Using the same value on either side of a binary operator is almost always a mistake. In the case
of logical operators, it is either a copy/paste error and therefore a bug, or it is simply wasted
code, and should be simplified. In the case of bitwise operators and most binary mathematical
operators, having the same value on both sides of an operator yields predictable results, and
Using the same value on either side of a binary operator is almost always a mistake. In the case
of logical operators, it is either a copy/paste error and therefore a bug, or it is simply wasted
code, and should be simplified. In the case of bitwise operators and most binary mathematical
operators, having the same value on both sides of an operator yields predictable results, and
should be simplified.

This rule ignores `*`, `+`, and `=`.

## Noncompliant Code Example

```javascript
if (a == b && a == b) { // if the first one is true, the second one is too
if (a == b && a == b) {
// if the first one is true, the second one is too
doX();
}
if (a > a) { // always false
if (a > a) {
// always false
doW();
}

Expand All @@ -31,10 +33,11 @@ Similarly, left-shifting 1 onto 1 is common in the construction of bit masks, an
Moreover comma operator , and `instanceof` operator are ignored as there are use-cases when there usage is valid.

```javascript
if (f !== f) { // test for NaN value
console.log("f is NaN");
if (f !== f) {
// test for NaN value
console.log('f is NaN');
}

var i = 1 << 1; // Compliant
var j = a << a; // Noncompliant
```
```
3 changes: 2 additions & 1 deletion docs/rules/no-identical-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function calculateCode() {
return code;
}

function getName() { // Noncompliant
function getName() {
// Noncompliant
vdiez marked this conversation as resolved.
Show resolved Hide resolved
doTheThing();
doOtherThing();
return code;
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-inverted-boolean-check.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# no-inverted-boolean-check

:wrench: *fixable*
:wrench: _fixable_

It is needlessly complex to invert the result of a boolean comparison. The opposite comparison should be made instead.

Expand Down
16 changes: 10 additions & 6 deletions docs/rules/no-nested-switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ Specifically, you should structure your code to avoid the need for nested `switc
function foo(n, m) {
switch (n) {
case 0:
switch (m) { // Noncompliant; nested switch
switch (
m // Noncompliant; nested switch
vdiez marked this conversation as resolved.
Show resolved Hide resolved
// ...
) {
}
case 1:
// ...
// ...
default:
// ...
// ...
}
}
```
Expand All @@ -29,15 +31,17 @@ function foo(n, m) {
case 0:
bar(m);
case 1:
// ...
// ...
default:
// ...
// ...
}
}

function bar(m) {
switch(m) {
switch (
m
// ...
) {
}
}
```
6 changes: 3 additions & 3 deletions docs/rules/no-nested-template-literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ In such situations, it’s preferable to move the nested template into a separat
## Noncompliant Code Example

```javascript
let color = "red";
let color = 'red';
let count = 3;
let message = `I have ${color ? `${count} ${color}` : count} apples`; // Noncompliant; nested template strings not easy to read
```

## Compliant Solution

```javascript
let color = "red";
let color = 'red';
let count = 3;
let apples = color ? `${count} ${color}` : count;
let message = `I have ${apples} apples`;
Expand All @@ -28,7 +28,7 @@ let message = `I have ${apples} apples`;
Nested template is ignored when the nesting one starts and ends on a different line.

```javascript
let color = "red";
let color = 'red';
let count = 3;
let message = `I have
${color ? `${count} ${color}` : count}
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-one-iteration-loop.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# no-one-iteration-loop

A loop with at most one iteration is equivalent to the use of an `if` statement to conditionally execute one piece of code. No developer expects to find such a use of a loop statement. If the initial intention of the author was really to conditionally execute one piece of code, an `if` statement should be used instead.

At worst that was not the initial intention of the author and so the body of the loop should be fixed to use the nested `return`, `break` or `throw` statements in a more appropriate way.

## Noncompliant Code Example
Expand Down
25 changes: 18 additions & 7 deletions docs/rules/no-redundant-boolean.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ Redundant Boolean literals should be removed from expressions to improve readabi
## Noncompliant Code Example

```javascript
if (booleanMethod() == true) { /* ... */ }
if (booleanMethod() == false) { /* ... */ }
if (booleanMethod() || false) { /* ... */ }
if (booleanMethod() == true) {
/* ... */
}
if (booleanMethod() == false) {
/* ... */
}
if (booleanMethod() || false) {
/* ... */
}
doSomething(!false);
doSomething(booleanMethod() == true);
```

## Compliant Solution

```javascript
if (booleanMethod()) { /* ... */ }
if (!booleanMethod()) { /* ... */ }
if (booleanMethod()) { /* ... */ }
if (booleanMethod()) {
/* ... */
}
if (!booleanMethod()) {
/* ... */
}
if (booleanMethod()) {
/* ... */
}
doSomething(true);
doSomething(booleanMethod());
```

Loading