Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix static propTypes handling in no-typos #1827

Merged
merged 2 commits into from Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/rules/no-typos.js
Expand Up @@ -45,7 +45,7 @@ module.exports = {
let propTypesPackageName = null;
let reactPackageName = null;

function checkValidPropTypeQualfier(node) {
function checkValidPropTypeQualifier(node) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone put typos in no-typos so I can fix typos while I fix no-typos.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0ca36be035e2c8c79d6af4543c7a6990

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
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