Skip to content

Commit 5be539b

Browse files
golopotljharb
authored andcommitted
[Fix] no-typos: improve report location
1 parent d9d2193 commit 5be539b

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/rules/no-typos.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,18 @@ module.exports = {
124124
}
125125
}
126126

127-
function reportErrorIfPropertyCasingTypo(node, propertyName, isClassProperty) {
127+
function reportErrorIfPropertyCasingTypo(propertyValue, propertyKey, isClassProperty) {
128+
const propertyName = propertyKey.name;
128129
if (propertyName === 'propTypes' || propertyName === 'contextTypes' || propertyName === 'childContextTypes') {
129-
checkValidPropObject(node);
130+
checkValidPropObject(propertyValue);
130131
}
131132
STATIC_CLASS_PROPERTIES.forEach((CLASS_PROP) => {
132133
if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) {
133134
const message = isClassProperty ?
134135
'Typo in static class property declaration' :
135136
'Typo in property declaration';
136137
context.report({
137-
node,
138+
node: propertyKey,
138139
message
139140
});
140141
}
@@ -176,9 +177,7 @@ module.exports = {
176177
return;
177178
}
178179

179-
const tokens = context.getFirstTokens(node, 2);
180-
const propertyName = tokens[1].value;
181-
reportErrorIfPropertyCasingTypo(node.value, propertyName, true);
180+
reportErrorIfPropertyCasingTypo(node.value, node.key, true);
182181
},
183182

184183
MemberExpression(node) {
@@ -198,7 +197,7 @@ module.exports = {
198197
(utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node)) &&
199198
(node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right)
200199
) {
201-
reportErrorIfPropertyCasingTypo(node.parent.right, propertyName, true);
200+
reportErrorIfPropertyCasingTypo(node.parent.right, node.property, true);
202201
}
203202
},
204203

@@ -218,7 +217,7 @@ module.exports = {
218217
}
219218

220219
node.properties.forEach((property) => {
221-
reportErrorIfPropertyCasingTypo(property.value, property.key.name, false);
220+
reportErrorIfPropertyCasingTypo(property.value, property.key, false);
222221
reportErrorIfLifecycleMethodCasingTypo(property);
223222
});
224223
}

tests/lib/rules/no-typos.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -618,21 +618,21 @@ ruleTester.run('no-typos', rule, {
618618
`,
619619
parser: parsers.BABEL_ESLINT,
620620
parserOptions,
621-
errors: [{message: ERROR_MESSAGE}]
621+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
622622
}, {
623623
code: `
624624
class Component extends React.Component {}
625625
Component.PropTypes = {}
626626
`,
627627
parserOptions,
628-
errors: [{message: ERROR_MESSAGE}]
628+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
629629
}, {
630630
code: `
631631
function MyComponent() { return (<div>{this.props.myProp}</div>) }
632632
MyComponent.PropTypes = {}
633633
`,
634634
parserOptions,
635-
errors: [{message: ERROR_MESSAGE}]
635+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
636636
}, {
637637
code: `
638638
class Component extends React.Component {
@@ -664,7 +664,7 @@ ruleTester.run('no-typos', rule, {
664664
`,
665665
parser: parsers.BABEL_ESLINT,
666666
parserOptions,
667-
errors: [{message: ERROR_MESSAGE}]
667+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
668668
}, {
669669
code: `
670670
class Component extends React.Component {}
@@ -756,21 +756,21 @@ ruleTester.run('no-typos', rule, {
756756
`,
757757
parser: parsers.BABEL_ESLINT,
758758
parserOptions,
759-
errors: [{message: ERROR_MESSAGE}]
759+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
760760
}, {
761761
code: `
762762
class Component extends React.Component {}
763763
Component.DefaultProps = {}
764764
`,
765765
parserOptions,
766-
errors: [{message: ERROR_MESSAGE}]
766+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
767767
}, {
768768
code: `
769769
function MyComponent() { return (<div>{this.props.myProp}</div>) }
770770
MyComponent.DefaultProps = {}
771771
`,
772772
parserOptions,
773-
errors: [{message: ERROR_MESSAGE}]
773+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
774774
}, {
775775
code: `
776776
class Component extends React.Component {
@@ -1567,13 +1567,13 @@ ruleTester.run('no-typos', rule, {
15671567
parserOptions,
15681568
errors: [{
15691569
message: ERROR_MESSAGE_ES5,
1570-
type: 'ObjectExpression'
1570+
type: 'Identifier'
15711571
}, {
15721572
message: ERROR_MESSAGE_ES5,
1573-
type: 'ObjectExpression'
1573+
type: 'Identifier'
15741574
}, {
15751575
message: ERROR_MESSAGE_ES5,
1576-
type: 'ObjectExpression'
1576+
type: 'Identifier'
15771577
}, {
15781578
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
15791579
type: 'Property'
@@ -1619,13 +1619,13 @@ ruleTester.run('no-typos', rule, {
16191619
parserOptions,
16201620
errors: [{
16211621
message: ERROR_MESSAGE_ES5,
1622-
type: 'ObjectExpression'
1622+
type: 'Identifier'
16231623
}, {
16241624
message: ERROR_MESSAGE_ES5,
1625-
type: 'ObjectExpression'
1625+
type: 'Identifier'
16261626
}, {
16271627
message: ERROR_MESSAGE_ES5,
1628-
type: 'ObjectExpression'
1628+
type: 'Identifier'
16291629
}, {
16301630
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
16311631
type: 'Property'

0 commit comments

Comments
 (0)