1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | module.exports = function(context) {
|
12 |
|
13 | function directives(block) {
|
14 | var ds = [], body = block.body, e, i, l;
|
15 |
|
16 | for (i = 0, l = body.length; i < l; ++i) {
|
17 | e = body[i];
|
18 |
|
19 | if (
|
20 | e.type === "ExpressionStatement" &&
|
21 | e.expression.type === "Literal" &&
|
22 | typeof e.expression.value === "string"
|
23 | ) {
|
24 | ds.push(e.expression);
|
25 | } else {
|
26 | break;
|
27 | }
|
28 | }
|
29 | return ds;
|
30 | }
|
31 |
|
32 | function isStrict(directive) {
|
33 | return directive.value === "use strict";
|
34 | }
|
35 |
|
36 | function checkForUnnecessaryUseStrict(node) {
|
37 | var useStrictDirectives, scope;
|
38 | useStrictDirectives = directives(node).filter(isStrict);
|
39 |
|
40 | switch(useStrictDirectives.length) {
|
41 | case 0:
|
42 | break;
|
43 |
|
44 | case 1:
|
45 | scope = context.getScope();
|
46 | if (scope.upper && scope.upper.isStrict) {
|
47 | context.report(useStrictDirectives[0], "Unnecessary 'use strict'.");
|
48 | }
|
49 | break;
|
50 |
|
51 | default:
|
52 | context.report(useStrictDirectives[1], "Multiple 'use strict' directives.");
|
53 | }
|
54 | }
|
55 |
|
56 | return {
|
57 |
|
58 | "Program": checkForUnnecessaryUseStrict,
|
59 |
|
60 | "FunctionExpression": function(node) {
|
61 | checkForUnnecessaryUseStrict(node.body);
|
62 | },
|
63 |
|
64 | "FunctionDeclaration": function(node) {
|
65 | checkForUnnecessaryUseStrict(node.body);
|
66 | }
|
67 | };
|
68 |
|
69 | };
|