/
no-typos.js
89 lines (77 loc) · 2.51 KB
/
no-typos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @fileoverview Prevent common casing typos
*/
'use strict';
const Components = require('../util/Components');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const STATIC_CLASS_PROPERTIES = ['propTypes', 'contextTypes', 'childContextTypes', 'defaultProps'];
const LIFECYCLE_METHODS = [
'componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount',
'render'
];
module.exports = {
meta: {
docs: {
description: 'Prevent common casing typos',
category: 'Stylistic Issues',
recommended: false
},
schema: []
},
create: Components.detect((context, components, utils) => {
function reportErrorIfClassPropertyCasingTypo(node, propertyName) {
STATIC_CLASS_PROPERTIES.forEach(CLASS_PROP => {
if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) {
context.report({
node: node,
message: 'Typo in static class property declaration'
});
}
});
}
function reportErrorIfLifecycleMethodCasingTypo(node) {
LIFECYCLE_METHODS.forEach(method => {
if (method.toLowerCase() === node.key.name.toLowerCase() && method !== node.key.name) {
context.report({
node: node,
message: 'Typo in component lifecycle method declaration'
});
}
});
}
return {
ClassProperty: function(node) {
if (!node.static || !utils.isES6Component(node.parent.parent)) {
return;
}
const tokens = context.getFirstTokens(node, 2);
const propertyName = tokens[1].value;
reportErrorIfClassPropertyCasingTypo(node, propertyName);
},
MemberExpression: function(node) {
const relatedComponent = utils.getRelatedComponent(node);
if (
relatedComponent &&
(utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node))
) {
const propertyName = node.property.name;
reportErrorIfClassPropertyCasingTypo(node, propertyName);
}
},
MethodDefinition: function (node) {
if (!utils.isES6Component(node.parent.parent)) {
return;
}
reportErrorIfLifecycleMethodCasingTypo(node);
}
};
})
};