1 | /**
|
2 | * @fileoverview Rule to disallow empty functions.
|
3 | * @author Toru Nagashima
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Requirements
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Helpers
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | const ALLOW_OPTIONS = Object.freeze([
|
19 | "functions",
|
20 | "arrowFunctions",
|
21 | "generatorFunctions",
|
22 | "methods",
|
23 | "generatorMethods",
|
24 | "getters",
|
25 | "setters",
|
26 | "constructors"
|
27 | ]);
|
28 |
|
29 | /**
|
30 | * Gets the kind of a given function node.
|
31 | *
|
32 | * @param {ASTNode} node - A function node to get. This is one of
|
33 | * an ArrowFunctionExpression, a FunctionDeclaration, or a
|
34 | * FunctionExpression.
|
35 | * @returns {string} The kind of the function. This is one of "functions",
|
36 | * "arrowFunctions", "generatorFunctions", "asyncFunctions", "methods",
|
37 | * "generatorMethods", "asyncMethods", "getters", "setters", and
|
38 | * "constructors".
|
39 | */
|
40 | function getKind(node) {
|
41 | const parent = node.parent;
|
42 | let kind = "";
|
43 |
|
44 | if (node.type === "ArrowFunctionExpression") {
|
45 | return "arrowFunctions";
|
46 | }
|
47 |
|
48 | // Detects main kind.
|
49 | if (parent.type === "Property") {
|
50 | if (parent.kind === "get") {
|
51 | return "getters";
|
52 | }
|
53 | if (parent.kind === "set") {
|
54 | return "setters";
|
55 | }
|
56 | kind = parent.method ? "methods" : "functions";
|
57 |
|
58 | } else if (parent.type === "MethodDefinition") {
|
59 | if (parent.kind === "get") {
|
60 | return "getters";
|
61 | }
|
62 | if (parent.kind === "set") {
|
63 | return "setters";
|
64 | }
|
65 | if (parent.kind === "constructor") {
|
66 | return "constructors";
|
67 | }
|
68 | kind = "methods";
|
69 |
|
70 | } else {
|
71 | kind = "functions";
|
72 | }
|
73 |
|
74 | // Detects prefix.
|
75 | let prefix = "";
|
76 |
|
77 | if (node.generator) {
|
78 | prefix = "generator";
|
79 | } else if (node.async) {
|
80 | prefix = "async";
|
81 | } else {
|
82 | return kind;
|
83 | }
|
84 | return prefix + kind[0].toUpperCase() + kind.slice(1);
|
85 | }
|
86 |
|
87 | //------------------------------------------------------------------------------
|
88 | // Rule Definition
|
89 | //------------------------------------------------------------------------------
|
90 |
|
91 | module.exports = {
|
92 | meta: {
|
93 | docs: {
|
94 | description: "disallow empty functions",
|
95 | category: "Best Practices",
|
96 | recommended: false,
|
97 | url: "https://eslint.org/docs/rules/no-empty-function"
|
98 | },
|
99 |
|
100 | schema: [
|
101 | {
|
102 | type: "object",
|
103 | properties: {
|
104 | allow: {
|
105 | type: "array",
|
106 | items: { enum: ALLOW_OPTIONS },
|
107 | uniqueItems: true
|
108 | }
|
109 | },
|
110 | additionalProperties: false
|
111 | }
|
112 | ],
|
113 |
|
114 | messages: {
|
115 | unexpected: "Unexpected empty {{name}}."
|
116 | }
|
117 | },
|
118 |
|
119 | create(context) {
|
120 | const options = context.options[0] || {};
|
121 | const allowed = options.allow || [];
|
122 |
|
123 | const sourceCode = context.getSourceCode();
|
124 |
|
125 | /**
|
126 | * Reports a given function node if the node matches the following patterns.
|
127 | *
|
128 | * - Not allowed by options.
|
129 | * - The body is empty.
|
130 | * - The body doesn't have any comments.
|
131 | *
|
132 | * @param {ASTNode} node - A function node to report. This is one of
|
133 | * an ArrowFunctionExpression, a FunctionDeclaration, or a
|
134 | * FunctionExpression.
|
135 | * @returns {void}
|
136 | */
|
137 | function reportIfEmpty(node) {
|
138 | const kind = getKind(node);
|
139 | const name = astUtils.getFunctionNameWithKind(node);
|
140 | const innerComments = sourceCode.getTokens(node.body, {
|
141 | includeComments: true,
|
142 | filter: astUtils.isCommentToken
|
143 | });
|
144 |
|
145 | if (allowed.indexOf(kind) === -1 &&
|
146 | node.body.type === "BlockStatement" &&
|
147 | node.body.body.length === 0 &&
|
148 | innerComments.length === 0
|
149 | ) {
|
150 | context.report({
|
151 | node,
|
152 | loc: node.body.loc.start,
|
153 | messageId: "unexpected",
|
154 | data: { name }
|
155 | });
|
156 | }
|
157 | }
|
158 |
|
159 | return {
|
160 | ArrowFunctionExpression: reportIfEmpty,
|
161 | FunctionDeclaration: reportIfEmpty,
|
162 | FunctionExpression: reportIfEmpty
|
163 | };
|
164 | }
|
165 | };
|