-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (116 loc) · 3.28 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const createVoidOnlySideEffects = (context) => {
let allowTraps = !!context.options[0] && context.options[0].allowTraps;
if (!allowTraps) {
allowTraps = {};
} else if (allowTraps === true) {
allowTraps = {
get: true,
has: true,
ownKeys: true
};
}
const hasSideEffect = node => {
switch (node.type) {
case 'UpdateExpression':
case 'AssignmentExpression':
case 'CallExpression':
case 'OptionalCallExpression':
case 'NewExpression':
case 'YieldExpression':
case 'AwaitExpression':
return true;
case 'MemberExpression':
if (allowTraps.get) return true;
return hasSideEffect(node.object);
case 'UnaryExpression':
if (node.operator === 'delete') return true;
case 'SpreadElement':
return hasSideEffect(node.argument);
case 'BinaryExpression':
if (allowTraps.has && node.operator === 'in') return true;
case 'LogicalExpression':
return hasSideEffect(node.left) || hasSideEffect(node.right);
case 'SequenceExpression':
return node.expressions.some(hasSideEffect);
case 'ArrayExpression':
return node.elements.some(hasSideEffect);
case 'ConditionalExpression':
return hasSideEffect(node.consequent) || hasSideEffect(node.alternative);
case 'ObjectExpression':
return node.properties.some(property => (
property.type === 'Property'
&& (
hasSideEffect(property.value)
|| hasSideEffect(property.key)
)
) || (
property.type === 'ExperimentalSpreadProperty'
&& (
allowTraps.ownKeys ||
hasSideEffect(property.argument)
)
));
case 'TemplateLiteral':
return node.expressions.some(hasSideEffect);
case 'TaggedTemplateExpression':
return hasSideEffect(node.quasi);
default:
return false;
}
};
return {
UnaryExpression(node) {
if (node.operator !== 'void' || hasSideEffect(node.argument)) return;
const fix = fixer => fixer.replaceText(node, 'undefined');
context.report({
node,
message: 'Unexpected void operator on non-side-effect argument. Use undefined instead.',
fix,
suggest: [{
desc: "Replace with undefined.",
fix
}]
});
}
};
};
module.exports = {
rules: {
'void-only-side-effects': {
create: createVoidOnlySideEffects,
meta: {
type: 'suggestion',
docs: {
description: 'Eslint rule to disallow void on non-side-effect arguments',
suggestion: true
},
fixable: 'code',
schema: [{
type: 'object',
properties: {
allowTraps: {
type: ['boolean', 'object'],
properties: {
get: { type: 'boolean' },
has: { type: 'boolean' },
ownKeys: { type: 'boolean' }
},
additionalProperties: false
}
},
additionalProperties: false
}]
}
}
},
configs: {
recommended: {
plugins: [
'void-only-side-effects'
],
rules: {
'void-only-side-effects/void-only-side-effects': 2
}
}
}
};