Skip to content

Commit 3824dc9

Browse files
committed
no-typos: improve error messages
1 parent 7f87310 commit 3824dc9

File tree

2 files changed

+62
-59
lines changed

2 files changed

+62
-59
lines changed

lib/rules/no-typos.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module.exports = {
4949
if (node.name !== 'isRequired') {
5050
context.report({
5151
node,
52-
message: `Typo in prop type chain qualifier: ${node.name}`
52+
message: 'Typo in prop type chain qualifier: {{name}}',
53+
data: {name: node.name}
5354
});
5455
}
5556
}
@@ -58,7 +59,8 @@ module.exports = {
5859
if (node.name && !PROP_TYPES.some(propTypeName => propTypeName === node.name)) {
5960
context.report({
6061
node,
61-
message: `Typo in declared prop type: ${node.name}`
62+
message: 'Typo in declared prop type: {{name}}',
63+
data: {name: node.name}
6264
});
6365
}
6466
}
@@ -151,7 +153,8 @@ module.exports = {
151153
if (method.toLowerCase() === nodeKeyName.toLowerCase() && method !== nodeKeyName) {
152154
context.report({
153155
node,
154-
message: 'Typo in component lifecycle method declaration'
156+
message: 'Typo in component lifecycle method declaration: {{actual}} should be {{expected}}',
157+
data: {actual: nodeKeyName, expected: method}
155158
});
156159
}
157160
});

tests/lib/rules/no-typos.js

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const parserOptions = {
2727

2828
const ERROR_MESSAGE = 'Typo in static class property declaration';
2929
const ERROR_MESSAGE_ES5 = 'Typo in property declaration';
30-
const ERROR_MESSAGE_LIFECYCLE_METHOD = 'Typo in component lifecycle method declaration';
30+
const ERROR_MESSAGE_LIFECYCLE_METHOD = ({actual, expected}) => `Typo in component lifecycle method declaration: ${actual} should be ${expected}`;
3131

3232
const ruleTester = new RuleTester();
3333
ruleTester.run('no-typos', rule, {
@@ -840,43 +840,43 @@ ruleTester.run('no-typos', rule, {
840840
`,
841841
parserOptions,
842842
errors: [{
843-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
843+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'GetDerivedStateFromProps', expected: 'getDerivedStateFromProps'}),
844844
type: 'MethodDefinition'
845845
}, {
846-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
846+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillMount', expected: 'componentWillMount'}),
847847
type: 'MethodDefinition'
848848
}, {
849-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
849+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'UNSAFE_ComponentWillMount', expected: 'UNSAFE_componentWillMount'}),
850850
type: 'MethodDefinition'
851851
}, {
852-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
852+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidMount', expected: 'componentDidMount'}),
853853
type: 'MethodDefinition'
854854
}, {
855-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
855+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillReceiveProps', expected: 'componentWillReceiveProps'}),
856856
type: 'MethodDefinition'
857857
}, {
858-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
858+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'UNSAFE_ComponentWillReceiveProps', expected: 'UNSAFE_componentWillReceiveProps'}),
859859
type: 'MethodDefinition'
860860
}, {
861-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
861+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ShouldComponentUpdate', expected: 'shouldComponentUpdate'}),
862862
type: 'MethodDefinition'
863863
}, {
864-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
864+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillUpdate', expected: 'componentWillUpdate'}),
865865
type: 'MethodDefinition'
866866
}, {
867-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
867+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'UNSAFE_ComponentWillUpdate', expected: 'UNSAFE_componentWillUpdate'}),
868868
type: 'MethodDefinition'
869869
}, {
870-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
870+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'GetSnapshotBeforeUpdate', expected: 'getSnapshotBeforeUpdate'}),
871871
type: 'MethodDefinition'
872872
}, {
873-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
873+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidUpdate', expected: 'componentDidUpdate'}),
874874
type: 'MethodDefinition'
875875
}, {
876-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
876+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidCatch', expected: 'componentDidCatch'}),
877877
type: 'MethodDefinition'
878878
}, {
879-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
879+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillUnmount', expected: 'componentWillUnmount'}),
880880
type: 'MethodDefinition'
881881
}]
882882
}, {
@@ -902,47 +902,47 @@ ruleTester.run('no-typos', rule, {
902902
`,
903903
parserOptions,
904904
errors: [{
905-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
905+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Getderivedstatefromprops', expected: 'getDerivedStateFromProps'}),
906906
type: 'MethodDefinition'
907907
}, {
908-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
908+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentwillmount', expected: 'componentWillMount'}),
909909
type: 'MethodDefinition'
910910
}, {
911-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
911+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'UNSAFE_Componentwillmount', expected: 'UNSAFE_componentWillMount'}),
912912
type: 'MethodDefinition'
913913
}, {
914-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
914+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentdidmount', expected: 'componentDidMount'}),
915915
type: 'MethodDefinition'
916916
}, {
917-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
917+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentwillreceiveprops', expected: 'componentWillReceiveProps'}),
918918
type: 'MethodDefinition'
919919
}, {
920-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
920+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'UNSAFE_Componentwillreceiveprops', expected: 'UNSAFE_componentWillReceiveProps'}),
921921
type: 'MethodDefinition'
922922
}, {
923-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
923+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Shouldcomponentupdate', expected: 'shouldComponentUpdate'}),
924924
type: 'MethodDefinition'
925925
}, {
926-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
926+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentwillupdate', expected: 'componentWillUpdate'}),
927927
type: 'MethodDefinition'
928928
}, {
929-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
929+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'UNSAFE_Componentwillupdate', expected: 'UNSAFE_componentWillUpdate'}),
930930
type: 'MethodDefinition'
931931
}, {
932-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
932+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Getsnapshotbeforeupdate', expected: 'getSnapshotBeforeUpdate'}),
933933
type: 'MethodDefinition'
934934
}, {
935-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
935+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentdidupdate', expected: 'componentDidUpdate'}),
936936
type: 'MethodDefinition'
937937
}, {
938-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
938+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentdidcatch', expected: 'componentDidCatch'}),
939939
type: 'MethodDefinition'
940940
}, {
941-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
941+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Componentwillunmount', expected: 'componentWillUnmount'}),
942942
type: 'MethodDefinition'
943943
}, {
944-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
945-
type: 'MethodDefinition'
944+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'Render', expected: 'render'}),
945+
nodeType: 'MethodDefinition'
946946
}]
947947
}, {
948948
code: `
@@ -967,43 +967,43 @@ ruleTester.run('no-typos', rule, {
967967
`,
968968
parserOptions,
969969
errors: [{
970-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
970+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'getderivedstatefromprops', expected: 'getDerivedStateFromProps'}),
971971
type: 'MethodDefinition'
972972
}, {
973-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
973+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentwillmount', expected: 'componentWillMount'}),
974974
type: 'MethodDefinition'
975975
}, {
976-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
976+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'unsafe_componentwillmount', expected: 'UNSAFE_componentWillMount'}),
977977
type: 'MethodDefinition'
978978
}, {
979-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
979+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentdidmount', expected: 'componentDidMount'}),
980980
type: 'MethodDefinition'
981981
}, {
982-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
982+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentwillreceiveprops', expected: 'componentWillReceiveProps'}),
983983
type: 'MethodDefinition'
984984
}, {
985-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
985+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'unsafe_componentwillreceiveprops', expected: 'UNSAFE_componentWillReceiveProps'}),
986986
type: 'MethodDefinition'
987987
}, {
988-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
988+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'shouldcomponentupdate', expected: 'shouldComponentUpdate'}),
989989
type: 'MethodDefinition'
990990
}, {
991-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
991+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentwillupdate', expected: 'componentWillUpdate'}),
992992
type: 'MethodDefinition'
993993
}, {
994-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
994+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'unsafe_componentwillupdate', expected: 'UNSAFE_componentWillUpdate'}),
995995
type: 'MethodDefinition'
996996
}, {
997-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
997+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'getsnapshotbeforeupdate', expected: 'getSnapshotBeforeUpdate'}),
998998
type: 'MethodDefinition'
999999
}, {
1000-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1000+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentdidupdate', expected: 'componentDidUpdate'}),
10011001
type: 'MethodDefinition'
10021002
}, {
1003-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1003+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentdidcatch', expected: 'componentDidCatch'}),
10041004
type: 'MethodDefinition'
10051005
}, {
1006-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1006+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'componentwillunmount', expected: 'componentWillUnmount'}),
10071007
type: 'MethodDefinition'
10081008
}]
10091009
}, {
@@ -1583,25 +1583,25 @@ ruleTester.run('no-typos', rule, {
15831583
message: ERROR_MESSAGE_ES5,
15841584
type: 'Identifier'
15851585
}, {
1586-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1586+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillMount', expected: 'componentWillMount'}),
15871587
type: 'Property'
15881588
}, {
1589-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1589+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidMount', expected: 'componentDidMount'}),
15901590
type: 'Property'
15911591
}, {
1592-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1592+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillReceiveProps', expected: 'componentWillReceiveProps'}),
15931593
type: 'Property'
15941594
}, {
1595-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1595+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ShouldComponentUpdate', expected: 'shouldComponentUpdate'}),
15961596
type: 'Property'
15971597
}, {
1598-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1598+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillUpdate', expected: 'componentWillUpdate'}),
15991599
type: 'Property'
16001600
}, {
1601-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1601+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidUpdate', expected: 'componentDidUpdate'}),
16021602
type: 'Property'
16031603
}, {
1604-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1604+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillUnmount', expected: 'componentWillUnmount'}),
16051605
type: 'Property'
16061606
}]
16071607
}, {
@@ -1635,25 +1635,25 @@ ruleTester.run('no-typos', rule, {
16351635
message: ERROR_MESSAGE_ES5,
16361636
type: 'Identifier'
16371637
}, {
1638-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1638+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillMount', expected: 'componentWillMount'}),
16391639
type: 'Property'
16401640
}, {
1641-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1641+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidMount', expected: 'componentDidMount'}),
16421642
type: 'Property'
16431643
}, {
1644-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1644+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillReceiveProps', expected: 'componentWillReceiveProps'}),
16451645
type: 'Property'
16461646
}, {
1647-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1647+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ShouldComponentUpdate', expected: 'shouldComponentUpdate'}),
16481648
type: 'Property'
16491649
}, {
1650-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1650+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillUpdate', expected: 'componentWillUpdate'}),
16511651
type: 'Property'
16521652
}, {
1653-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1653+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentDidUpdate', expected: 'componentDidUpdate'}),
16541654
type: 'Property'
16551655
}, {
1656-
message: ERROR_MESSAGE_LIFECYCLE_METHOD,
1656+
message: ERROR_MESSAGE_LIFECYCLE_METHOD({actual: 'ComponentWillUnmount', expected: 'componentWillUnmount'}),
16571657
type: 'Property'
16581658
}]
16591659
/*

0 commit comments

Comments
 (0)