Skip to content

Commit

Permalink
[Fix] no-typos: improve report location
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot authored and ljharb committed Oct 20, 2019
1 parent d9d2193 commit 5be539b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
15 changes: 7 additions & 8 deletions lib/rules/no-typos.js
Expand Up @@ -124,17 +124,18 @@ module.exports = {
}
}

function reportErrorIfPropertyCasingTypo(node, propertyName, isClassProperty) {
function reportErrorIfPropertyCasingTypo(propertyValue, propertyKey, isClassProperty) {
const propertyName = propertyKey.name;
if (propertyName === 'propTypes' || propertyName === 'contextTypes' || propertyName === 'childContextTypes') {
checkValidPropObject(node);
checkValidPropObject(propertyValue);
}
STATIC_CLASS_PROPERTIES.forEach((CLASS_PROP) => {
if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) {
const message = isClassProperty ?
'Typo in static class property declaration' :
'Typo in property declaration';
context.report({
node,
node: propertyKey,
message
});
}
Expand Down Expand Up @@ -176,9 +177,7 @@ module.exports = {
return;
}

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

MemberExpression(node) {
Expand All @@ -198,7 +197,7 @@ module.exports = {
(utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node)) &&
(node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right)
) {
reportErrorIfPropertyCasingTypo(node.parent.right, propertyName, true);
reportErrorIfPropertyCasingTypo(node.parent.right, node.property, true);
}
},

Expand All @@ -218,7 +217,7 @@ module.exports = {
}

node.properties.forEach((property) => {
reportErrorIfPropertyCasingTypo(property.value, property.key.name, false);
reportErrorIfPropertyCasingTypo(property.value, property.key, false);
reportErrorIfLifecycleMethodCasingTypo(property);
});
}
Expand Down
26 changes: 13 additions & 13 deletions tests/lib/rules/no-typos.js
Expand Up @@ -618,21 +618,21 @@ ruleTester.run('no-typos', rule, {
`,
parser: parsers.BABEL_ESLINT,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
class Component extends React.Component {}
Component.PropTypes = {}
`,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
function MyComponent() { return (<div>{this.props.myProp}</div>) }
MyComponent.PropTypes = {}
`,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
class Component extends React.Component {
Expand Down Expand Up @@ -664,7 +664,7 @@ ruleTester.run('no-typos', rule, {
`,
parser: parsers.BABEL_ESLINT,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
class Component extends React.Component {}
Expand Down Expand Up @@ -756,21 +756,21 @@ ruleTester.run('no-typos', rule, {
`,
parser: parsers.BABEL_ESLINT,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
class Component extends React.Component {}
Component.DefaultProps = {}
`,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
function MyComponent() { return (<div>{this.props.myProp}</div>) }
MyComponent.DefaultProps = {}
`,
parserOptions,
errors: [{message: ERROR_MESSAGE}]
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
}, {
code: `
class Component extends React.Component {
Expand Down Expand Up @@ -1567,13 +1567,13 @@ ruleTester.run('no-typos', rule, {
parserOptions,
errors: [{
message: ERROR_MESSAGE_ES5,
type: 'ObjectExpression'
type: 'Identifier'
}, {
message: ERROR_MESSAGE_ES5,
type: 'ObjectExpression'
type: 'Identifier'
}, {
message: ERROR_MESSAGE_ES5,
type: 'ObjectExpression'
type: 'Identifier'
}, {
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
type: 'Property'
Expand Down Expand Up @@ -1619,13 +1619,13 @@ ruleTester.run('no-typos', rule, {
parserOptions,
errors: [{
message: ERROR_MESSAGE_ES5,
type: 'ObjectExpression'
type: 'Identifier'
}, {
message: ERROR_MESSAGE_ES5,
type: 'ObjectExpression'
type: 'Identifier'
}, {
message: ERROR_MESSAGE_ES5,
type: 'ObjectExpression'
type: 'Identifier'
}, {
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
type: 'Property'
Expand Down

0 comments on commit 5be539b

Please sign in to comment.