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 e76ade4
Show file tree
Hide file tree
Showing 3 changed files with 28 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
17 changes: 13 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,20 @@ module.exports = {
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(([key, moduleNames]) => {
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 e76ade4

Please sign in to comment.