Skip to content

Commit d73cd51

Browse files
akulsr0ljharb
authored andcommitted
[Fix] no-deprecated: prevent false positive on commonjs import
1 parent 0667fb0 commit d73cd51

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Fixed
9+
* [`no-deprecated`]: prevent false positive on commonjs import ([#3614][] @akulsr0)
10+
11+
[#3614]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3614
12+
813
## [7.33.1] - 2023.07.29
914

1015
### Fixed

lib/rules/no-deprecated.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
'use strict';
99

10-
const values = require('object.values');
10+
const entries = require('object.entries');
1111
const astUtil = require('../util/ast');
1212
const componentUtil = require('../util/componentUtil');
1313
const docsUrl = require('../util/docsUrl');
@@ -162,11 +162,22 @@ module.exports = {
162162
function getReactModuleName(node) {
163163
let moduleName = false;
164164
if (!node.init) {
165-
return moduleName;
165+
return false;
166166
}
167167

168-
values(MODULES).some((moduleNames) => {
169-
moduleName = moduleNames.find((name) => name === node.init.name);
168+
entries(MODULES).some((entry) => {
169+
const key = entry[0];
170+
const moduleNames = entry[1];
171+
if (
172+
node.init.arguments
173+
&& node.init.arguments.length > 0
174+
&& node.init.arguments[0]
175+
&& key === node.init.arguments[0].value
176+
) {
177+
moduleName = MODULES[key][0];
178+
} else {
179+
moduleName = moduleNames.find((name) => name === node.init.name);
180+
}
170181
return moduleName;
171182
});
172183

tests/lib/rules/no-deprecated.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ ruleTester.run('no-deprecated', rule, {
153153
renderToPipeableStream(<App />, {});
154154
`,
155155
},
156+
{
157+
code: `
158+
import { renderToString } from 'react-dom/server';
159+
`,
160+
},
161+
{
162+
code: `
163+
const { renderToString } = require('react-dom/server');
164+
`,
165+
},
156166
]),
157167

158168
invalid: parsers.all([

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy