Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…(`deprecation`) (#4619)
- Loading branch information
1 parent
fe09df4
commit 533319e
Showing
24 changed files
with
782 additions
and
616 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
513 changes: 0 additions & 513 deletions
513
its/ruling/src/test/expected/jsts/sonar-web/javascript-S1874.json
Large diffs are not rendered by default.
Oops, something went wrong.
515 changes: 515 additions & 0 deletions
515
its/ruling/src/test/expected/jsts/sonar-web/javascript-S6957.json
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
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,35 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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 { check } from '../tools'; | ||
import { rule } from './'; | ||
import path from 'path'; | ||
import { clearPackageJsons, loadPackageJsons } from '@sonar/jsts'; | ||
|
||
const sonarId = path.basename(__dirname); | ||
|
||
describe('Rule S6957', () => { | ||
beforeEach(() => { | ||
loadPackageJsons(__dirname, []); | ||
}); | ||
afterAll(() => { | ||
clearPackageJsons(); | ||
}); | ||
check(sonarId, rule, __dirname); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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. | ||
*/ | ||
export { rule } from './rule'; |
File renamed without changes.
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,62 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 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/S6957/javascript | ||
|
||
import { Rule } from 'eslint'; | ||
import { rules } from 'eslint-plugin-react'; | ||
import { getNearestPackageJsons } from '@sonar/jsts'; | ||
|
||
const reactNoDeprecated = rules['no-deprecated']; | ||
|
||
export const rule: Rule.RuleModule = { | ||
meta: { | ||
messages: { | ||
deprecated: '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}', | ||
}, | ||
}, | ||
create(context: Rule.RuleContext) { | ||
function getVersionFromOptions() { | ||
return context.options?.[0]?.['react-version']; | ||
} | ||
function getVersionFromPackageJson() { | ||
for (const { contents: packageJson } of getNearestPackageJsons(context.filename)) { | ||
if (packageJson.dependencies?.react) { | ||
return packageJson.dependencies.react; | ||
} | ||
if (packageJson.devDependencies?.react) { | ||
return packageJson.devDependencies.react; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
const reactVersion = getVersionFromOptions() || getVersionFromPackageJson(); | ||
|
||
const patchedContext = reactVersion | ||
? Object.create(context, { | ||
settings: { | ||
value: { react: { version: reactVersion } }, | ||
writable: false, | ||
}, | ||
}) | ||
: context; | ||
return reactNoDeprecated.create(patchedContext); | ||
}, | ||
}; |
File renamed without changes.
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
...n/javascript-checks/src/main/java/org/sonar/javascript/checks/NoDeprecatedReactCheck.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-2024 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; | ||
|
||
@JavaScriptRule | ||
@TypeScriptRule | ||
@Rule(key = "S6957") | ||
public class NoDeprecatedReactCheck implements EslintBasedCheck { | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "no-deprecated-react"; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...avascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6957.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,52 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>Deprecated methods are functions or properties that are no longer recommended and are likely to be removed in future updates of the library. They | ||
are often replaced with newer methods that offer better performance, security, or usability.</p> | ||
<p>Using deprecated methods in React can lead to the following issues:</p> | ||
<ul> | ||
<li> <strong>Code Maintainability</strong>: As deprecated methods are removed in future versions, the code will break if not updated. This can lead | ||
to increased time and effort in code maintenance. </li> | ||
<li> <strong>Performance</strong>: Newer methods often come with performance improvements. Using deprecated methods can lead to slower app | ||
performance. </li> | ||
<li> <strong>Security</strong>: Deprecated methods may have known security issues that are fixed in newer methods. </li> | ||
</ul> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
function MyComponent(props) { | ||
const [state, setState] = useState(initialState); | ||
|
||
componentWillReceiveProps(nextProps) { // Noncompliant: deprecated lifecycle method | ||
// Some code here... | ||
} | ||
|
||
componentWillUpdate(nextProps, nextState) { // Noncompliant: deprecated lifecycle method | ||
// Some code here... | ||
} | ||
|
||
render() { | ||
return <div>Hello World</div>; | ||
} | ||
} | ||
</pre> | ||
<p>To fix this issue, check React’s upgrading guide, replace the deprecated methods with their newer counterparts, and adapt your component’s | ||
implementation accordingly.</p> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
function MyComponent(props) { | ||
const [state, setState] = useState(initialState); | ||
|
||
// Using useEffect to replace deprecated lifecycle methods | ||
useEffect(() => { | ||
// Code to run on component update | ||
}, [props]); // This will run when `props` changes | ||
|
||
return <div>Hello World</div>; | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> React Documentation - <a href="https://react.dev/reference/react">React Reference Overview</a> </li> | ||
</ul> | ||
|
Oops, something went wrong.