blob: 4c6ac16a427670ec1880a09ac31534381926215b (
plain)
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
|
/**
* @fileoverview Rule to flag unnecessary strict directives.
* @author Ian Christian Myers
* @copyright 2014 Ian Christian Myers. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
function directives(block) {
var ds = [], body = block.body, e, i, l;
if (body) {
for (i = 0, l = body.length; i < l; ++i) {
e = body[i];
if (
e.type === "ExpressionStatement" &&
e.expression.type === "Literal" &&
typeof e.expression.value === "string"
) {
ds.push(e.expression);
} else {
break;
}
}
}
return ds;
}
function isStrict(directive) {
return directive.value === "use strict";
}
function checkForUnnecessaryUseStrict(node) {
var useStrictDirectives = directives(node).filter(isStrict),
scope,
upper;
switch (useStrictDirectives.length) {
case 0:
break;
case 1:
scope = context.getScope();
upper = scope.upper;
if (upper && upper.functionExpressionScope) {
upper = upper.upper;
}
if (upper && upper.isStrict) {
context.report(useStrictDirectives[0], "Unnecessary 'use strict'.");
}
break;
default:
context.report(useStrictDirectives[1], "Multiple 'use strict' directives.");
}
}
return {
"Program": checkForUnnecessaryUseStrict,
"ArrowFunctionExpression": function(node) {
checkForUnnecessaryUseStrict(node.body);
},
"FunctionExpression": function(node) {
checkForUnnecessaryUseStrict(node.body);
},
"FunctionDeclaration": function(node) {
checkForUnnecessaryUseStrict(node.body);
}
};
};
|