-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6759: React props should be read-only (#3043)
- Loading branch information
1 parent
9a888ec
commit a630b5d
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"title": "React props should be read-only", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"react" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6759", | ||
"sqKey": "S6759", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "covered", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "HIGH", | ||
"RELIABILITY": "MEDIUM", | ||
"SECURITY": "LOW" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
== Why is this an issue? | ||
|
||
React props should be read-only because it helps to enforce the principle of immutability in React 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. | ||
|
||
If props were mutable, child components could modify the props directly, leading to unpredictable behavior and making it harder to track down bugs. By enforcing read-only props, React promotes a more predictable and maintainable codebase. Additionally, read-only props enable performance optimizations in React's rendering process by avoiding unnecessary re-renders of components. | ||
|
||
Overall, enforcing read-only props in React helps improve code reliability, maintainability, and performance. | ||
|
||
== How to fix it in Flow.js | ||
|
||
You should use Flow.js's modifier `+` to mark your component props as read-only. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,javascript,diff-id=1,diff-type=noncompliant] | ||
---- | ||
type Props = { | ||
name: string, // Noncompliant: 'name' is mutable | ||
} | ||
class Welcome extends React.Component<Props> { | ||
render () { | ||
return <div>Hello {this.props.name}</div>; | ||
} | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,javascript,diff-id=1,diff-type=compliant] | ||
---- | ||
type Props = { | ||
+name: string, | ||
} | ||
class Welcome extends React.Component<Props> { | ||
render () { | ||
return <div>Hello {this.props.name}</div>; | ||
} | ||
} | ||
---- | ||
|
||
== How to fix it in TypeScript | ||
|
||
You should use TypeScript's modifier `readonly` to mark your component props as read-only. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,javascript,diff-id=2,diff-type=noncompliant] | ||
---- | ||
type Props = { | ||
name: string; // Noncompliant: 'name' is mutable | ||
} | ||
class Welcome extends React.Component<Props> { | ||
render () { | ||
return <div>Hello {this.props.name}</div>; | ||
} | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,javascript,diff-id=2,diff-type=compliant] | ||
---- | ||
type Props = { | ||
readonly name: string; | ||
} | ||
class Welcome extends React.Component<Props> { | ||
render () { | ||
return <div>Hello {this.props.name}</div>; | ||
} | ||
} | ||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
|
||
* https://react.dev/learn/passing-props-to-a-component[React - Passing Props to a Component] | ||
* https://flow.org/en/docs/types/objects/#read-only-object-properties[Flow.js - Read-only object properties] | ||
* https://www.typescriptlang.org/docs/handbook/2/classes.html#readonly[TypeScript - readonly] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |