diff --git a/CHANGELOG.md b/CHANGELOG.md index 379bca681f..c56e0b16d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/no-deprecated.js b/lib/rules/no-deprecated.js index 877680658a..6e04a612ef 100644 --- a/lib/rules/no-deprecated.js +++ b/lib/rules/no-deprecated.js @@ -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'); @@ -162,11 +162,22 @@ module.exports = { function getReactModuleName(node) { let moduleName = false; if (!node.init) { - return moduleName; + return false; } - 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; }); diff --git a/tests/lib/rules/no-deprecated.js b/tests/lib/rules/no-deprecated.js index dfea11f833..cf91991cec 100644 --- a/tests/lib/rules/no-deprecated.js +++ b/tests/lib/rules/no-deprecated.js @@ -153,6 +153,16 @@ ruleTester.run('no-deprecated', rule, { renderToPipeableStream(, {}); `, }, + { + code: ` + import { renderToString } from 'react-dom/server'; + `, + }, + { + code: ` + const { renderToString } = require('react-dom/server'); + `, + }, ]), invalid: parsers.all([