Skip to content
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 S6748 (react/no-children-prop): React children should not be passed as prop #4149

Merged
merged 4 commits into from Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions its/ruling/src/test/expected/ts/desktop/typescript-S6748.json
@@ -0,0 +1,8 @@
{
"desktop:app/src/ui/lib/list/list.tsx": [
844
],
"desktop:app/src/ui/lib/tooltipped-content.tsx": [
39
]
}
9 changes: 9 additions & 0 deletions its/ruling/src/test/expected/ts/eigen/typescript-S6748.json
@@ -0,0 +1,9 @@
{
"eigen:src/app/utils/placeholders.tsx": [
61,
90
],
"eigen:src/palette/elements/Text/Text.tsx": [
63
]
}
Expand Up @@ -228,6 +228,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoArrayDeleteCheck.class,
NoArrayIndexKeyCheck.class,
NoBaseToStringCheck.class,
NoChildrenPropCheck.class,
NoClearTextProtocolsCheck.class,
NoCodeAfterDoneCheck.class,
NoConfusingNonNullAssertionCheck.class,
Expand Down
@@ -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 = "S6748")
public class NoChildrenPropCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "no-children-prop";
}
}
@@ -0,0 +1,23 @@
<h2>Why is this an issue?</h2>
<p>When using JSX the component children should be passed between opening and closing tags. Passing children in a <code>children</code> prop may work
sometimes, but will lead to errors if children are passed both as nested components and <code>children</code> prop at the same time.</p>
<p>When not using JSX, the children should be passed to <code>createElement()</code> method as extra arguments after the <code>props</code>
object.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
&lt;div children='Children' /&gt;
&lt;Foo children={&lt;Bar /&gt;} /&gt;

React.createElement("div", { children: 'Children' })
</pre>
<pre data-diff-id="1" data-diff-type="compliant">
&lt;div&gt;Children&lt;/div&gt;
&lt;Foo&gt;&lt;Bar /&gt;&lt;/Foo&gt;

React.createElement("div", {}, 'Children');
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://react.dev/learn/passing-props-to-a-component">React - Passing Props to a Component</a> </li>
</ul>

@@ -0,0 +1,29 @@
{
"title": "React \"children\" should not be passed as prop",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6748",
"sqKey": "S6748",
"scope": "All",
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM",
"SECURITY": "LOW"
},
"attribute": "CONVENTIONAL"
},
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Expand Up @@ -276,6 +276,7 @@
"S6679",
"S6746",
"S6747",
"S6748",
"S6749"
]
}