Skip to content

Commit

Permalink
Merge pull request jsx-eslint#1827 from alexzherdev/1677-no-typos-sta…
Browse files Browse the repository at this point in the history
…tic-proptypes

Fix static propTypes handling in no-typos
  • Loading branch information
ljharb authored Jun 15, 2018
2 parents 2a674b0 + d68b9e8 commit 48e386d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
16 changes: 8 additions & 8 deletions lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
let propTypesPackageName = null;
let reactPackageName = null;

function checkValidPropTypeQualfier(node) {
function checkValidPropTypeQualifier(node) {
if (node.name !== 'isRequired') {
context.report({
node: node,
Expand Down Expand Up @@ -101,14 +101,14 @@ module.exports = {
isPropTypesPackage(node.object.object)
) { // PropTypes.myProp.isRequired
checkValidPropType(node.object.property);
checkValidPropTypeQualfier(node.property);
checkValidPropTypeQualifier(node.property);
} else if (
isPropTypesPackage(node.object) &&
node.property.name !== 'isRequired'
) { // PropTypes.myProp
checkValidPropType(node.property);
} else if (node.object.type === 'CallExpression') {
checkValidPropTypeQualfier(node.property);
checkValidPropTypeQualifier(node.property);
checkValidCallExpression(node.object);
}
} else if (node.type === 'CallExpression') {
Expand All @@ -126,8 +126,7 @@ module.exports = {

function reportErrorIfClassPropertyCasingTypo(node, propertyName) {
if (propertyName === 'propTypes' || propertyName === 'contextTypes' || propertyName === 'childContextTypes') {
const propsNode = node && node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right;
checkValidPropObject(propsNode);
checkValidPropObject(node);
}
STATIC_CLASS_PROPERTIES.forEach(CLASS_PROP => {
if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) {
Expand Down Expand Up @@ -176,7 +175,7 @@ module.exports = {

const tokens = context.getFirstTokens(node, 2);
const propertyName = tokens[1].value;
reportErrorIfClassPropertyCasingTypo(node, propertyName);
reportErrorIfClassPropertyCasingTypo(node.value, propertyName);
},

MemberExpression: function(node) {
Expand All @@ -193,9 +192,10 @@ module.exports = {

if (
relatedComponent &&
(utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node))
(utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node)) &&
(node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right)
) {
reportErrorIfClassPropertyCasingTypo(node, propertyName);
reportErrorIfClassPropertyCasingTypo(node.parent.right, propertyName);
}
},

Expand Down
28 changes: 27 additions & 1 deletion tests/lib/rules/no-typos.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,6 @@ ruleTester.run('no-typos', rule, {
a: PropTypes.Number.isRequired
}
`,
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: 'Typo in declared prop type: Number'
Expand All @@ -930,11 +929,38 @@ ruleTester.run('no-typos', rule, {
a: PropTypes.number.isrequired
}
`,
parserOptions: parserOptions,
errors: [{
message: 'Typo in prop type chain qualifier: isrequired'
}]
}, {
code: `
import PropTypes from "prop-types";
class Component extends React.Component {
static propTypes = {
a: PropTypes.number.isrequired
}
};
`,
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: 'Typo in prop type chain qualifier: isrequired'
}]
}, {
code: `
import PropTypes from "prop-types";
class Component extends React.Component {
static propTypes = {
a: PropTypes.Number
}
};
`,
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: 'Typo in declared prop type: Number'
}]
}, {
code: `
import PropTypes from "prop-types";
Expand Down

0 comments on commit 48e386d

Please sign in to comment.