Skip to content

Commit f34ee91

Browse files
committed
[Fix] no-typos: avoid a crash on bindingless prop-types import; add warning
Fixes #2899
1 parent 4f2d1eb commit f34ee91

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1212
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
1313
* [`jsx-no-constructed-context-values`]: avoid a crash with boolean shorthand ([#2895][] @ljharb)
1414
* [`static-property-placement`]: do not report non-components ([#2893][] @golopot)
15-
* [ `no-array-index-key`]: support optional chaining ([#2897][] @SyMind)
15+
* [`no-array-index-key`]: support optional chaining ([#2897][] @SyMind)
16+
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)
1617

18+
[#2899]: https://github.com/yannickcr/eslint-plugin-react/issues/2899
1719
[#2897]: https://github.com/yannickcr/eslint-plugin-react/pull/2897
1820
[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
1921
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894

lib/rules/no-typos.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ module.exports = {
177177
return {
178178
ImportDeclaration(node) {
179179
if (node.source && node.source.value === 'prop-types') { // import PropType from "prop-types"
180-
propTypesPackageName = node.specifiers[0].local.name;
180+
if (node.specifiers.length > 0) {
181+
propTypesPackageName = node.specifiers[0].local.name;
182+
} else {
183+
context.report({
184+
node,
185+
message: '`\'prop-types\'` imported without a local `PropTypes` binding.'
186+
});
187+
}
181188
} else if (node.source && node.source.value === 'react') { // import { PropTypes } from "react"
182189
if (node.specifiers.length > 0) {
183190
reactPackageName = node.specifiers[0].local.name; // guard against accidental anonymous `import "react"`

tests/lib/rules/no-typos.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const ERROR_MESSAGE_STATIC = (method) => `Lifecycle method should be static: ${m
3232

3333
const ruleTester = new RuleTester();
3434
ruleTester.run('no-typos', rule, {
35-
valid: [{
35+
valid: [].concat({
3636
code: `
3737
import createReactClass from 'create-react-class'
3838
function hello (extra = {}) {
@@ -647,9 +647,9 @@ ruleTester.run('no-typos', rule, {
647647
}
648648
`,
649649
parserOptions
650-
}],
650+
}),
651651

652-
invalid: [{
652+
invalid: [].concat({
653653
code: `
654654
class Component extends React.Component {
655655
static PropTypes = {};
@@ -1729,5 +1729,23 @@ ruleTester.run('no-typos', rule, {
17291729
parserOptions: parserOptions
17301730
},
17311731
*/
1732-
}]
1732+
}, parsers.TS({
1733+
code: `
1734+
import 'prop-types'
1735+
`,
1736+
parser: parsers.TYPESCRIPT_ESLINT,
1737+
parserOptions,
1738+
errors: [{
1739+
message: '`\'prop-types\'` imported without a local `PropTypes` binding.'
1740+
}]
1741+
}, {
1742+
code: `
1743+
import 'prop-types'
1744+
`,
1745+
parser: parsers['@TYPESCRIPT_ESLINT'],
1746+
parserOptions,
1747+
errors: [{
1748+
message: '`\'prop-types\'` imported without a local `PropTypes` binding.'
1749+
}]
1750+
}))
17331751
});

0 commit comments

Comments
 (0)