-
Notifications
You must be signed in to change notification settings - Fork 184
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 S6788: Disallow usage of findDOMNode (react/no-find-dom-node
)
#4359
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
its/ruling/src/test/expected/jsts/console/typescript-S6788.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,20 @@ | ||
{ | ||
"console:src/components/Help/Alert.tsx": [ | ||
28 | ||
], | ||
"console:src/components/Help/Help.tsx": [ | ||
28 | ||
], | ||
"console:src/components/PopupWrapper/PopupWrapper.tsx": [ | ||
56 | ||
], | ||
"console:src/views/ProjectRootView/AddModelPopup.tsx": [ | ||
163 | ||
], | ||
"console:src/views/ProjectRootView/EditModelPopup.tsx": [ | ||
186 | ||
], | ||
"console:src/views/ProjectSettingsView/ProjectSettingsView.tsx": [ | ||
216 | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
its/ruling/src/test/expected/jsts/desktop/typescript-S6788.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 @@ | ||
{ | ||
"desktop:app/src/ui/lib/list/list.tsx": [ | ||
1016, | ||
1240 | ||
] | ||
} |
19 changes: 19 additions & 0 deletions
19
its/ruling/src/test/expected/jsts/sonar-web/javascript-S6788.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 @@ | ||
{ | ||
"sonar-web:src/main/js/components/mixins/resize-mixin.js": [ | ||
19 | ||
], | ||
"sonar-web:src/main/js/components/mixins/tooltips-mixin.js": [ | ||
16 | ||
], | ||
"sonar-web:src/main/js/main/nav/component/component-nav.js": [ | ||
37 | ||
], | ||
"sonar-web:tests/apps/background-tasks-test.js": [ | ||
68 | ||
], | ||
"sonar-web:tests/apps/system-test.js": [ | ||
15, | ||
73, | ||
89 | ||
] | ||
} |
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,34 @@ | ||
/* | ||
* 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. | ||
*/ | ||
// https://sonarsource.github.io/rspec/#/rspec/S6788/javascript | ||
|
||
import { Rule } from 'eslint'; | ||
import { interceptReport } from '../helpers'; | ||
|
||
// Rewording issue message reported by the core implementation. | ||
export function decorate(rule: Rule.RuleModule): Rule.RuleModule { | ||
rule.meta!.messages!['noFindDOMNode'] = | ||
"Do not use findDOMNode. It doesn't work with function components and is deprecated in StrictMode."; | ||
return interceptReport(rule, (context, reportDescriptor) => { | ||
context.report({ | ||
...reportDescriptor, | ||
}); | ||
}); | ||
} |
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,23 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import { rules } from 'eslint-plugin-react'; | ||
import { decorate } from './decorator'; | ||
|
||
export const rule = decorate(rules['no-find-dom-node']); |
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,51 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import { rule } from './'; | ||
import { TypeScriptRuleTester } from '../tools'; | ||
|
||
const ruleTester = new TypeScriptRuleTester(); | ||
|
||
ruleTester.run(`Decorated rule should reword the issue message`, rule, { | ||
valid: [ | ||
{ | ||
code: `type Foo = () => number;`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
class MyComponent extends Component { | ||
componentDidMount() { | ||
findDOMNode(this).scrollIntoView(); | ||
} | ||
render() { | ||
return <div /> | ||
} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"Do not use findDOMNode. It doesn't work with function components and is deprecated in StrictMode.", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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
36 changes: 36 additions & 0 deletions
36
...lugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoFindDomNodeCheck.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 = "S6788") | ||
public class NoFindDomNodeCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "no-find-dom-node"; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6788.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,51 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>In React, <code>findDOMNode</code> is used to get the browser DOM node given a component instance. However, using <code>findDOMNode</code> is | ||
fragile because the connection between the JSX node and the code manipulating the corresponding DOM node is not explicit. For example, changing the | ||
external structure of returned JSX will affect the return value of <code>findDOMNode</code>. There are also other <a | ||
href="https://react.dev/reference/react-dom/findDOMNode#caveats">caveats</a> when using <code>findDOMNode</code>.</p> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
import { Component } from 'react'; | ||
import { findDOMNode } from 'react-dom'; | ||
|
||
class AutoselectingInput extends Component { | ||
componentDidMount() { | ||
const input = findDOMNode(this); // Noncompliant: findDOMNode is deprecated | ||
input.select(); | ||
} | ||
|
||
render() { | ||
return <input defaultValue="Hello" /> | ||
} | ||
} | ||
</pre> | ||
<p>Instead, one should get the component’s own DOM node from a ref. Pass your ref as the <code>ref</code> attribute to the JSX tag for which you want | ||
to get the DOM node. This tells React to put this <code><input></code>’s DOM node into <code>inputRef.current</code>.</p> | ||
<p>Use <code>createRef</code> to manage a specific DOM node. In modern React without class components, the equivalent code would call | ||
<code>useRef</code> instead.</p> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
import { createRef, Component } from 'react'; | ||
|
||
class AutoselectingInput extends Component { | ||
inputRef = createRef(null); | ||
|
||
componentDidMount() { | ||
const input = this.inputRef.current; // Always points to the input element | ||
input.select(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<input ref={this.inputRef} defaultValue="Hello" /> | ||
); | ||
} | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> React Documentation - <a href="https://react.dev/reference/react-dom/findDOMNode"><code>findDOMNode</code></a> </li> | ||
<li> React Documentation - <a href="https://react.dev/reference/react/createRef"><code>createRef</code></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/learn/manipulating-the-dom-with-refs">Manipulating the DOM with Refs</a> </li> | ||
</ul> | ||
|
28 changes: 28 additions & 0 deletions
28
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6788.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": "React\u0027s \"findDOMNode\" should not be used", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"react" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6788", | ||
"sqKey": "S6788", | ||
"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", | ||
"S6788", | ||
"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.
I am never sure, but is "function component", "functional component", or both can be used?
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.
both can be used. at least in the docs they use both
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.
👍