-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
no-arrow-function-lifecycle.js
55 lines (50 loc) · 1.64 KB
/
no-arrow-function-lifecycle.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
/**
* @fileoverview It is not necessary to use arrow function for lifecycle methods
* @author Tan Nguyen
*/
'use strict';
const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const lifecycleMethods = require('../util/lifecycleMethods');
module.exports = {
meta: {
docs: {
description: 'It is not necessary to use arrow function for lifecycle methods',
category: 'Best Practices',
recommended: false,
url: docsUrl('no-arrow-function-lifecycle')
},
schema: []
},
create: Components.detect((context, components) => {
/**
* @param {Array} properties list of component properties
*/
function reportNoArrowFunctionLifecycle(properties) {
properties.forEach(node => {
const propertyName = astUtil.getPropertyName(node);
const nodeType = node.value && node.value.type;
const isLifecycleMethod = lifecycleMethods.indexOf(propertyName) !== -1;
if (nodeType === 'ArrowFunctionExpression' && isLifecycleMethod) {
context.report({
node,
message: '{{propertyName}} is a React lifecycle method, and should not be an arrow function. Use an instance method instead.',
data: {
propertyName
}
});
}
});
}
return {
'Program:exit': function() {
const list = components.list();
Object.keys(list).forEach(component => {
const properties = astUtil.getComponentProperties(list[component].node);
reportNoArrowFunctionLifecycle(properties);
});
}
};
})
};