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

[Fix] no-deprecated: false positive on commonjs import in no-deprecated rule #3614

Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
* [`no-deprecated`]: prevent false positive on commonjs import ([#3614][] @akulsr0)

[#3614]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3614

## [7.33.1] - 2023.07.29

### Fixed
Expand Down
19 changes: 15 additions & 4 deletions lib/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const values = require('object.values');
const entries = require('object.entries');
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -162,11 +162,22 @@
function getReactModuleName(node) {
let moduleName = false;
if (!node.init) {
return moduleName;
return false;

Check warning on line 165 in lib/rules/no-deprecated.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/no-deprecated.js#L165

Added line #L165 was not covered by tests
}

values(MODULES).some((moduleNames) => {
moduleName = moduleNames.find((name) => name === node.init.name);
entries(MODULES).some((entry) => {
const key = entry[0];
const moduleNames = entry[1];
if (
node.init.arguments
&& node.init.arguments.length > 0
&& node.init.arguments[0]
&& key === node.init.arguments[0].value
) {
moduleName = MODULES[key][0];
} else {
moduleName = moduleNames.find((name) => name === node.init.name);
}
return moduleName;
});

Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ ruleTester.run('no-deprecated', rule, {
renderToPipeableStream(<App />, {});
`,
},
{
code: `
import { renderToString } from 'react-dom/server';
`,
},
{
code: `
const { renderToString } = require('react-dom/server');
`,
},
]),

invalid: parsers.all([
Expand Down