New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create rule S6790: Disallow using string references #4360
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
its/ruling/src/test/expected/jsts/ant-design/javascript-S6790.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,6 @@ | ||
{ | ||
"ant-design:components/upload/__tests__/upload.test.js": [ | ||
37, | ||
43 | ||
] | ||
} |
72 changes: 72 additions & 0 deletions
72
its/ruling/src/test/expected/jsts/console/typescript-S6790.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,72 @@ | ||
{ | ||
"console:src/components/ApiLayover/ApiLayover.tsx": [ | ||
35 | ||
], | ||
"console:src/components/Datepicker/Datepicker.tsx": [ | ||
83 | ||
], | ||
"console:src/components/Help/Alert.tsx": [ | ||
28, | ||
38 | ||
], | ||
"console:src/components/Help/Help.tsx": [ | ||
28, | ||
36 | ||
], | ||
"console:src/components/PopupWrapper/PopupWrapper.tsx": [ | ||
47, | ||
56 | ||
], | ||
"console:src/components/ScrollBox/ScrollBox.tsx": [ | ||
35, | ||
49, | ||
53 | ||
], | ||
"console:src/components/Tether/Tether.tsx": [ | ||
42, | ||
42, | ||
43, | ||
74 | ||
], | ||
"console:src/components/ToggleButton/ToggleButton.tsx": [ | ||
59, | ||
83 | ||
], | ||
"console:src/views/ProjectRootView/AddModelPopup.tsx": [ | ||
139, | ||
163 | ||
], | ||
"console:src/views/ProjectRootView/EditModelPopup.tsx": [ | ||
145, | ||
186 | ||
], | ||
"console:src/views/ProjectSettingsView/ProjectSettingsView.tsx": [ | ||
82, | ||
216 | ||
], | ||
"console:src/views/RootView/RootView.tsx": [ | ||
26, | ||
50 | ||
], | ||
"console:src/views/models/DatabrowserView/Cell.tsx": [ | ||
116, | ||
127 | ||
], | ||
"console:src/views/models/DatabrowserView/Cell/DefaultCell.tsx": [ | ||
11 | ||
], | ||
"console:src/views/models/DatabrowserView/Cell/FloatCell.tsx": [ | ||
44 | ||
], | ||
"console:src/views/models/DatabrowserView/Cell/IntCell.tsx": [ | ||
28 | ||
], | ||
"console:src/views/models/DatabrowserView/Cell/JsonCell.tsx": [ | ||
11 | ||
], | ||
"console:src/views/models/DatabrowserView/Cell/StringCell.tsx": [ | ||
37, | ||
49, | ||
71 | ||
] | ||
} |
11 changes: 11 additions & 0 deletions
11
its/ruling/src/test/expected/jsts/redux/javascript-S6790.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,11 @@ | ||
{ | ||
"redux:examples/real-world/components/Explore.js": [ | ||
19, | ||
26, | ||
44 | ||
], | ||
"redux:examples/todos-with-undo/components/AddTodo.js": [ | ||
6, | ||
18 | ||
] | ||
} |
37 changes: 37 additions & 0 deletions
37
its/ruling/src/test/expected/jsts/sonar-web/javascript-S6790.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,37 @@ | ||
{ | ||
"sonar-web:src/main/js/apps/background-tasks/search.js": [ | ||
82, | ||
84, | ||
110, | ||
143 | ||
], | ||
"sonar-web:src/main/js/apps/background-tasks/stats.js": [ | ||
23, | ||
37, | ||
40, | ||
47, | ||
62, | ||
72 | ||
], | ||
"sonar-web:src/main/js/apps/overview/components/domain-timeline.js": [ | ||
165, | ||
179 | ||
], | ||
"sonar-web:src/main/js/apps/project-permissions/search.js": [ | ||
33 | ||
], | ||
"sonar-web:src/main/js/apps/projects/search.js": [ | ||
108 | ||
], | ||
"sonar-web:src/main/js/apps/system/item-log-level.js": [ | ||
28 | ||
], | ||
"sonar-web:src/main/js/components/charts/bubble-chart.js": [ | ||
100, | ||
115 | ||
], | ||
"sonar-web:src/main/js/main/nav/global/global-nav-search.js": [ | ||
76, | ||
80 | ||
] | ||
} |
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/NoStringRefsCheck.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 = "S6790") | ||
public class NoStringRefsCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "no-string-refs"; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6790.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,45 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>React Refs provide a way to access DOM nodes or React elements created in the render method.</p> | ||
<p>Older React versions allowed the ref attribute to be a string, like <code>"textInput"</code>, later accessible as <code>this.refs.textInput</code>. | ||
This is considered legacy code due to multiple reasons:</p> | ||
<ul> | ||
<li> String <code>refs</code> make React slower as they force React to keep track of what component is currently executing. </li> | ||
<li> String <code>refs</code> are not composable: if a library puts a ref on the passed child, the user can’t put another ref on it. </li> | ||
<li> The owner of a string <code>ref</code> is determined by the currently executing component. </li> | ||
</ul> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
const Hello = createReactClass({ | ||
componentDidMount: function() { | ||
const component = this.refs.hello; // Noncompliant | ||
// ... | ||
}, | ||
render: function() { | ||
return <div ref="hello">Hello, world.</div>; | ||
} | ||
}); | ||
</pre> | ||
<p>Instead, reference callbacks should be used. These do not have the limitations mentioned above. When the DOM node is added to the screen, React | ||
will call the <code>ref</code> callback with the DOM node as the argument. When that DOM node is removed, React will call your <code>ref</code> | ||
callback with <code>null</code>. One should return <code>undefined</code> from the <code>ref</code> callback.</p> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
const Hello = createReactClass({ | ||
componentDidMount: function() { | ||
const component = this.hello; | ||
// ... | ||
}, | ||
render() { | ||
return <div ref={(c) => { this.hello = c; }}>Hello, world.</div>; | ||
} | ||
}); | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> React Documentation - <a href="https://react.dev/reference/react-dom/components/common#ref-callback"><code>ref</code> callback function</a> | ||
</li> | ||
<li> React Documentation - <a href="https://legacy.reactjs.org/docs/refs-and-the-dom.html">Refs and the DOM</a> </li> | ||
<li> React Documentation - <a href="https://react.dev/learn/manipulating-the-dom-with-refs">Manipulating the DOM with Refs</a> </li> | ||
<li> React Documentation - <a href="https://react.dev/reference/react/useRef"><code>useRef</code></a> </li> | ||
<li> React Documentation - <a href="https://react.dev/reference/react/createRef#createref"><code>createRef</code></a> </li> | ||
</ul> | ||
|
28 changes: 28 additions & 0 deletions
28
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6790.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,28 @@ | ||
{ | ||
"title": "String references should not be used", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"react" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6790", | ||
"sqKey": "S6790", | ||
"scope": "All", | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "MEDIUM", | ||
"RELIABILITY": "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 |
---|---|---|
|
@@ -292,6 +292,7 @@ | |
"S6772", | ||
"S6774", | ||
"S6775", | ||
"S6790", | ||
"S6793", | ||
"S6807", | ||
"S6811", | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion, but to be consistent between the noncompliant and compliant snippets, we could maybe use the method definition shortcut everywhere:
instead of
Same for the
render
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oki, I will create a PR on rspec