-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (63 loc) · 1.56 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
function isTopLevel(node){
let scope = node.parent;
while (scope.type === 'BlockStatement'){
scope = scope.parent;
}
return (scope.type === 'Program');
}
function se(context){
return node=>{
if (isTopLevel(node)){
context.report({
node,
message: `Side effects in toplevel are not allowed.`,
})
}
}
}
module.exports = {
rules: {
'no-toplevel-var': function(context){
return {
"VariableDeclaration": function(node) {
if (node.kind === "var") {
if(isTopLevel(node)){
context.report({
node,
message: `Unexpected var in toplevel, use const.`,
});
}
}
}
};
},
'no-toplevel-let': function(context){
return {
"VariableDeclaration": function(node) {
if (node.kind === "let") {
if(isTopLevel(node)){
context.report({
node,
message: `Unexpected let in toplevel, use const.`,
});
}
}
}
};
},
'no-toplevel-side-effect': (context)=>({
ExpressionStatement: (node) => {
const options = context.options[0] || {};
const allowedStatements = options.allow || [];
if (allowedStatements.includes(node.expression.value)) {
return;
}
return se(context)(node);
},
IfStatement:se(context),
ForStatement:se(context),
WhileStatement:se(context),
SwitchStatement:se(context),
}),
}
}