Skip to content

Commit 602e071

Browse files
jsg2021ljharb
authored andcommitted
[Fix] guard against accidental anonymous react imports
There was a file in an old code base that improperly imported react anonymously...and had been unnoticed until we added this no-typos rule and eslint crashed. :)
1 parent 7aa4a03 commit 602e071

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/rules/no-typos.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ module.exports = {
155155
if (node.source && node.source.value === 'prop-types') { // import PropType from "prop-types"
156156
propTypesPackageName = node.specifiers[0].local.name;
157157
} else if (node.source && node.source.value === 'react') { // import { PropTypes } from "react"
158-
reactPackageName = node.specifiers[0].local.name;
159-
158+
if (node.specifiers.length > 0) {
159+
reactPackageName = node.specifiers[0].local.name; // guard against accidental anonymous `import "react"`
160+
}
160161
if (node.specifiers.length >= 1) {
161162
const propTypesSpecifier = node.specifiers.find(specifier => (
162163
specifier.imported && specifier.imported.name === 'PropTypes'

tests/lib/rules/no-typos.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,14 @@ ruleTester.run('no-typos', rule, {
11701170
}, {
11711171
message: 'Typo in prop type chain qualifier: isrequired'
11721172
}]
1173+
}, {
1174+
code: `
1175+
import 'react';
1176+
class Component extends React.Component {};
1177+
`,
1178+
parser: 'babel-eslint',
1179+
parserOptions: parserOptions,
1180+
errors: []
11731181
}, {
11741182
code: `
11751183
import { PropTypes } from 'react';

0 commit comments

Comments
 (0)