1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function(context) {
|
13 |
|
14 | var style = context.options[0],
|
15 | allowArrowFunctions = context.options[1] && context.options[1].allowArrowFunctions === true,
|
16 | enforceDeclarations = (style === "declaration"),
|
17 | stack = [];
|
18 |
|
19 | var nodesToCheck = {
|
20 | "Program": function() {
|
21 | stack = [];
|
22 | },
|
23 |
|
24 | "FunctionDeclaration": function(node) {
|
25 | stack.push(false);
|
26 |
|
27 | if (!enforceDeclarations) {
|
28 | context.report(node, "Expected a function expression.");
|
29 | }
|
30 | },
|
31 | "FunctionDeclaration:exit": function() {
|
32 | stack.pop();
|
33 | },
|
34 |
|
35 | "FunctionExpression": function(node) {
|
36 | stack.push(false);
|
37 |
|
38 | if (enforceDeclarations && node.parent.type === "VariableDeclarator") {
|
39 | context.report(node.parent, "Expected a function declaration.");
|
40 | }
|
41 | },
|
42 | "FunctionExpression:exit": function() {
|
43 | stack.pop();
|
44 | },
|
45 |
|
46 | "ThisExpression": function() {
|
47 | if (stack.length > 0) {
|
48 | stack[stack.length - 1] = true;
|
49 | }
|
50 | }
|
51 | };
|
52 |
|
53 | if (!allowArrowFunctions) {
|
54 | nodesToCheck.ArrowFunctionExpression = function() {
|
55 | stack.push(false);
|
56 | };
|
57 |
|
58 | nodesToCheck["ArrowFunctionExpression:exit"] = function(node) {
|
59 | var hasThisExpr = stack.pop();
|
60 |
|
61 | if (enforceDeclarations && !hasThisExpr && node.parent.type === "VariableDeclarator") {
|
62 | context.report(node.parent, "Expected a function declaration.");
|
63 | }
|
64 | };
|
65 | }
|
66 |
|
67 | return nodesToCheck;
|
68 |
|
69 | };
|
70 |
|
71 | module.exports.schema = [
|
72 | {
|
73 | "enum": ["declaration", "expression"]
|
74 | },
|
75 | {
|
76 | "type": "object",
|
77 | "properties": {
|
78 | "allowArrowFunctions": {
|
79 | "type": "boolean"
|
80 | }
|
81 | },
|
82 | "additionalProperties": false
|
83 | }
|
84 | ];
|