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] no-typos: improve report location #2468

Merged
merged 1 commit into from Oct 29, 2019
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
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