Skip to content

Commit bb7af97

Browse files
committed
ESLint: add few docs for sonarjs
1 parent 50e9c2f commit bb7af97

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Pattern: Missing use of read-only prop
2+
3+
Issue: -
4+
5+
## Description
6+
7+
React props should be read-only because it helps to enforce the principle of immutability in React functional components. By making props read-only, you ensure that the data passed from a parent component to a child component cannot be modified directly by the child component. This helps maintain a clear data flow and prevents unexpected side effects.
8+
9+
Overall, enforcing read-only props in React helps improve code reliability, maintainability, and performance.
10+
11+
Example of **incorrect** code:
12+
13+
```ts
14+
interface Props {
15+
name: string;
16+
}
17+
18+
function Welcome(props: Props) { // Noncompliant: The component props are not read-only
19+
return <div>Hello {props.name}</div>;
20+
}
21+
```
22+
23+
Example of **correct** code:
24+
25+
```ts
26+
interface Props {
27+
name: string;
28+
}
29+
30+
function Welcome(props: Readonly<Props>) {
31+
return <div>Hello {props.name}</div>;
32+
}
33+
```
34+
35+
## Further Reading
36+
37+
* [prefer-read-only-props](https://sonarsource.github.io/rspec/#/rspec/S6759/javascript)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Pattern: Missing use of `RegExp.exec()`
2+
3+
Issue: -
4+
5+
## Description
6+
7+
`String.match()` behaves the same way as `RegExp.exec()` when the regular expression does not include the global flag `g`. While they work the same, `RegExp.exec()` can be slightly faster than `String.match()`. Therefore, it should be preferred for better performance.
8+
9+
10+
Example of **incorrect** code:
11+
12+
```ts
13+
'foo'.match(/bar/);
14+
```
15+
16+
Example of **correct** code:
17+
18+
```ts
19+
/bar/.exec('foo');
20+
```
21+
22+
## Further Reading
23+
24+
* [prefer-regexp-exec](https://sonarsource.github.io/rspec/#/rspec/S6594/javascript)

0 commit comments

Comments
 (0)