Skip to content

Commit

Permalink
[Fix] no-typos: avoid a crash on bindingless prop-types import; a…
Browse files Browse the repository at this point in the history
…dd warning

Fixes #2899
  • Loading branch information
ljharb committed Jan 8, 2021
1 parent 4f2d1eb commit f34ee91
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -12,8 +12,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
* [`jsx-no-constructed-context-values`]: avoid a crash with boolean shorthand ([#2895][] @ljharb)
* [`static-property-placement`]: do not report non-components ([#2893][] @golopot)
* [ `no-array-index-key`]: support optional chaining ([#2897][] @SyMind)
* [`no-array-index-key`]: support optional chaining ([#2897][] @SyMind)
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)

[#2899]: https://github.com/yannickcr/eslint-plugin-react/issues/2899
[#2897]: https://github.com/yannickcr/eslint-plugin-react/pull/2897
[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894
Expand Down
9 changes: 8 additions & 1 deletion lib/rules/no-typos.js
Expand Up @@ -177,7 +177,14 @@ module.exports = {
return {
ImportDeclaration(node) {
if (node.source && node.source.value === 'prop-types') { // import PropType from "prop-types"
propTypesPackageName = node.specifiers[0].local.name;
if (node.specifiers.length > 0) {
propTypesPackageName = node.specifiers[0].local.name;
} else {
context.report({
node,
message: '`\'prop-types\'` imported without a local `PropTypes` binding.'
});
}
} else if (node.source && node.source.value === 'react') { // import { PropTypes } from "react"
if (node.specifiers.length > 0) {
reactPackageName = node.specifiers[0].local.name; // guard against accidental anonymous `import "react"`
Expand Down
26 changes: 22 additions & 4 deletions tests/lib/rules/no-typos.js
Expand Up @@ -32,7 +32,7 @@ const ERROR_MESSAGE_STATIC = (method) => `Lifecycle method should be static: ${m

const ruleTester = new RuleTester();
ruleTester.run('no-typos', rule, {
valid: [{
valid: [].concat({
code: `
import createReactClass from 'create-react-class'
function hello (extra = {}) {
Expand Down Expand Up @@ -647,9 +647,9 @@ ruleTester.run('no-typos', rule, {
}
`,
parserOptions
}],
}),

invalid: [{
invalid: [].concat({
code: `
class Component extends React.Component {
static PropTypes = {};
Expand Down Expand Up @@ -1729,5 +1729,23 @@ ruleTester.run('no-typos', rule, {
parserOptions: parserOptions
},
*/
}]
}, parsers.TS({
code: `
import 'prop-types'
`,
parser: parsers.TYPESCRIPT_ESLINT,
parserOptions,
errors: [{
message: '`\'prop-types\'` imported without a local `PropTypes` binding.'
}]
}, {
code: `
import 'prop-types'
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
parserOptions,
errors: [{
message: '`\'prop-types\'` imported without a local `PropTypes` binding.'
}]
}))
});

0 comments on commit f34ee91

Please sign in to comment.