Skip to content

Commit

Permalink
[Fix] no-deprecated: prevent false positive on commonjs import
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 authored and ljharb committed Aug 5, 2023
1 parent 0667fb0 commit d73cd51
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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 @@ 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;
});

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

0 comments on commit d73cd51

Please sign in to comment.