-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ed in functional components (#4155)
- Loading branch information
1 parent
746e232
commit a3c04a7
Showing
13 changed files
with
157 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,8 @@ | |
], | ||
"file-for-rules:S6304.js": [ | ||
5 | ||
], | ||
"file-for-rules:S6757.js": [ | ||
2 | ||
] | ||
} |
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 |
---|---|---|
|
@@ -61,5 +61,8 @@ | |
"file-for-rules:S6321.js": [ | ||
65, | ||
100 | ||
], | ||
"file-for-rules:S6757.js": [ | ||
3 | ||
] | ||
} |
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
5 changes: 5 additions & 0 deletions
5
its/ruling/src/test/expected/js/file-for-rules/javascript-S6757.json
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,5 @@ | ||
{ | ||
"file-for-rules:S6757.js": [ | ||
3 | ||
] | ||
} |
19 changes: 19 additions & 0 deletions
19
its/ruling/src/test/expected/js/javascript-test-sources/javascript-S6757.json
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,19 @@ | ||
{ | ||
"javascript-test-sources:src/ace/src/edit_session/bracket_match.js": [ | ||
114, | ||
171 | ||
], | ||
"javascript-test-sources:src/ace/src/editor.js": [ | ||
2518, | ||
2519, | ||
2521 | ||
], | ||
"javascript-test-sources:src/ace/src/layer/font_metrics.js": [ | ||
103, | ||
111 | ||
], | ||
"javascript-test-sources:src/ace/src/mode/folding/mixed.js": [ | ||
18, | ||
20 | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
its/ruling/src/test/expected/js/react-cloud-music/javascript-S6757.json
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,8 @@ | ||
{ | ||
"react-cloud-music:src/baseUI/music-note/index.jsx": [ | ||
47, | ||
48, | ||
49, | ||
51 | ||
] | ||
} |
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,7 @@ | ||
import React from 'react'; | ||
function MyComponent(props){ | ||
const foo = this.props.bar; // Noncompliant: remove 'this' | ||
return ( | ||
<div>{foo}</div> | ||
); | ||
} |
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
36 changes: 36 additions & 0 deletions
36
...-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoThisInSfcCheck.java
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,36 @@ | ||
/** | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.javascript.checks; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.javascript.api.EslintBasedCheck; | ||
import org.sonar.plugins.javascript.api.JavaScriptRule; | ||
import org.sonar.plugins.javascript.api.TypeScriptRule; | ||
|
||
@TypeScriptRule | ||
@JavaScriptRule | ||
@Rule(key = "S6757") | ||
public class NoThisInSfcCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "no-this-in-sfc"; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6757.html
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,38 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>Referring to <code>this</code> in React functional component would be an error because the components are just regular JavaScript functions and do | ||
not have an object assotiated with them. The functional components receive its props as a first argument to the component function, so you can access | ||
then directly, and it is a common style to destructure them right away.</p> | ||
<pre> | ||
function UserProfile({firstName, lastName}){ | ||
return ( | ||
<div className="user">{firstName} {lastName}</div> | ||
); | ||
} | ||
</pre> | ||
<p>React also supports legacy class-based components, where <code>this</code> keyword refers to the component instance object, but this style of | ||
writing components is no longer recommended, and mixing it with functional components will lead to errors.</p> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
function MyComponent(props){ | ||
const foo = this.props.bar; // Noncompliant: remove 'this' | ||
return ( | ||
<div>{foo}</div> | ||
); | ||
} | ||
</pre> | ||
<p>To fix the issue remove <code>this</code> from your functional component code (are you mixing functional and class-based component styles?)</p> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
function MyComponent(props){ | ||
const foo = props.bar; | ||
return ( | ||
<div>{foo}</div> | ||
); | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> <a href="https://react.dev/learn/your-first-component#defining-a-component">React - Defining a Component</a> </li> | ||
<li> <a href="https://react.dev/learn/passing-props-to-a-component">React - Passing Props to a Component</a> </li> | ||
<li> <a href="https://react.dev/reference/react/Component">React - Legacy class components</a> </li> | ||
</ul> | ||
|
29 changes: 29 additions & 0 deletions
29
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6757.json
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,29 @@ | ||
{ | ||
"title": "\"this\" should not be used in functional components", | ||
"type": "BUG", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"react" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6757", | ||
"sqKey": "S6757", | ||
"scope": "All", | ||
"quickfix": "infeasible", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "HIGH", | ||
"RELIABILITY": "MEDIUM", | ||
"SECURITY": "LOW" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
}, | ||
"compatibleLanguages": [ | ||
"JAVASCRIPT", | ||
"TYPESCRIPT" | ||
] | ||
} |
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 |
---|---|---|
|
@@ -280,6 +280,7 @@ | |
"S6749", | ||
"S6750", | ||
"S6754", | ||
"S6756" | ||
"S6756", | ||
"S6757" | ||
] | ||
} |