-
Notifications
You must be signed in to change notification settings - Fork 25
/
no-thenable-actions-in-views.js
45 lines (40 loc) · 1.56 KB
/
no-thenable-actions-in-views.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
const _ = require('underscore');
const lodashGet = require('lodash/get');
const path = require('path');
const isReactViewFile = require('./utils').isReactViewFile;
const message = require('./CONST').MESSAGE.NO_THENABLE_ACTIONS_IN_VIEWS;
module.exports = {
create: (context) => {
const actionsNamespaces = [];
return {
// Using import declaration to create a map of all the imports for this file and which ones are "actions"
ImportDeclaration(node) {
const pathName = path.resolve(lodashGet(node, 'source.value'));
if (!pathName || !pathName.includes('/actions/')) {
return;
}
actionsNamespaces.push(_.last(pathName.split('/')));
},
MemberExpression(node) {
if (!isReactViewFile(context.getFilename())) {
return;
}
if (lodashGet(node, 'property.name') !== 'then') {
return;
}
const actionModuleName = lodashGet(node, 'object.callee.object.name');
if (!_.includes(actionsNamespaces, actionModuleName)) {
return;
}
const actionMethodName = lodashGet(node, 'object.callee.property.name');
context.report({
node,
message,
data: {
method: `${actionModuleName}.${actionMethodName}()`,
},
});
},
};
},
};