Skip to content

Commit 0f95b17

Browse files
committed
[Fix] no-typos: improve report location
1 parent 11dc56b commit 0f95b17

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/rules/no-typos.js

Lines changed: 7 additions & 8 deletions
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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -646,21 +646,21 @@ ruleTester.run('no-typos', rule, {
646646
`,
647647
parser: parsers.BABEL_ESLINT,
648648
parserOptions,
649-
errors: [{message: ERROR_MESSAGE}]
649+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
650650
}, {
651651
code: `
652652
class Component extends React.Component {}
653653
Component.PropTypes = {}
654654
`,
655655
parserOptions,
656-
errors: [{message: ERROR_MESSAGE}]
656+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
657657
}, {
658658
code: `
659659
function MyComponent() { return (<div>{this.props.myProp}</div>) }
660660
MyComponent.PropTypes = {}
661661
`,
662662
parserOptions,
663-
errors: [{message: ERROR_MESSAGE}]
663+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
664664
}, {
665665
code: `
666666
class Component extends React.Component {
@@ -669,7 +669,7 @@ ruleTester.run('no-typos', rule, {
669669
`,
670670
parser: parsers.BABEL_ESLINT,
671671
parserOptions,
672-
errors: [{message: ERROR_MESSAGE}]
672+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
673673
}, {
674674
code: `
675675
class Component extends React.Component {}
@@ -784,21 +784,21 @@ ruleTester.run('no-typos', rule, {
784784
`,
785785
parser: parsers.BABEL_ESLINT,
786786
parserOptions,
787-
errors: [{message: ERROR_MESSAGE}]
787+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
788788
}, {
789789
code: `
790790
class Component extends React.Component {}
791791
Component.DefaultProps = {}
792792
`,
793793
parserOptions,
794-
errors: [{message: ERROR_MESSAGE}]
794+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
795795
}, {
796796
code: `
797797
function MyComponent() { return (<div>{this.props.myProp}</div>) }
798798
MyComponent.DefaultProps = {}
799799
`,
800800
parserOptions,
801-
errors: [{message: ERROR_MESSAGE}]
801+
errors: [{message: ERROR_MESSAGE, type: 'Identifier'}]
802802
}, {
803803
code: `
804804
class Component extends React.Component {
@@ -1594,13 +1594,13 @@ ruleTester.run('no-typos', rule, {
15941594
parserOptions,
15951595
errors: [{
15961596
message: ERROR_MESSAGE_ES5,
1597-
type: 'ObjectExpression'
1597+
type: 'Identifier'
15981598
}, {
15991599
message: ERROR_MESSAGE_ES5,
1600-
type: 'ObjectExpression'
1600+
type: 'Identifier'
16011601
}, {
16021602
message: ERROR_MESSAGE_ES5,
1603-
type: 'ObjectExpression'
1603+
type: 'Identifier'
16041604
}, {
16051605
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
16061606
type: 'Property'
@@ -1646,13 +1646,13 @@ ruleTester.run('no-typos', rule, {
16461646
parserOptions,
16471647
errors: [{
16481648
message: ERROR_MESSAGE_ES5,
1649-
type: 'ObjectExpression'
1649+
type: 'Identifier'
16501650
}, {
16511651
message: ERROR_MESSAGE_ES5,
1652-
type: 'ObjectExpression'
1652+
type: 'Identifier'
16531653
}, {
16541654
message: ERROR_MESSAGE_ES5,
1655-
type: 'ObjectExpression'
1655+
type: 'Identifier'
16561656
}, {
16571657
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
16581658
type: 'Property'

0 commit comments

Comments
 (0)