-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheslint-local-rules.js
72 lines (72 loc) · 2.31 KB
/
eslint-local-rules.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
module.exports = {
'log-manager': {
meta: {
type: 'problem',
docs: {
description:
'Disable nonblock statement body on LogManager methods calls'
},
fixable: 'code',
schema: []
},
create(context) {
return {
// capture optional-chaining calls
'IfStatement > :not(BlockStatement).consequent CallExpression[optional=true]':
function (node) {
// skip if logging is deeply nested inside a block statement
if (
node.parent &&
node.parent.parent &&
node.parent.parent.parent &&
node.parent.parent.parent.type === 'BlockStatement'
)
return;
findLogger(node);
},
// capture none-optional-chaining calls
'IfStatement > :not(BlockStatement).consequent': function (node) {
if (
node.type === 'ExpressionStatement' &&
node.expression.type === 'CallExpression'
) {
findLogger(node.expression);
}
}
};
function findLogger(node) {
const lookup = findProperty(node.callee);
const callee = lookup.expression ? lookup.parent.property : lookup;
// const method = node.callee.property
// ? node.callee.property.name
// : node.callee.name;
// console.log(callee.name === method ? '' : callee.name, '>', method);
if (callee.name === '_loggerManager') {
const options = {
node,
message: 'LogManager method called as a nonblock statement.'
};
if (node.parent.parent.type === 'ConditionalExpression') {
// no fix if logging is inside a ternary expression
} else {
const expression = context.sourceCode.getText(node);
options.fix = (fixer) =>
fixer.replaceText(node, `{ ${expression} }`);
}
context.report(options);
}
}
function findProperty(obj) {
if (obj.type === 'MemberExpression') {
if (obj.object && obj.object.type !== 'ThisExpression') {
obj = findProperty(obj.object);
}
if (obj.property) {
obj = findProperty(obj.property);
}
}
return obj;
}
}
}
};