]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-empty-function.js
2 * @fileoverview Rule to disallow empty functions.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const ALLOW_OPTIONS
= Object
.freeze([
32 * Gets the kind of a given function node.
33 * @param {ASTNode} node A function node to get. This is one of
34 * an ArrowFunctionExpression, a FunctionDeclaration, or a
36 * @returns {string} The kind of the function. This is one of "functions",
37 * "arrowFunctions", "generatorFunctions", "asyncFunctions", "methods",
38 * "generatorMethods", "asyncMethods", "getters", "setters", and
41 function getKind(node
) {
42 const parent
= node
.parent
;
45 if (node
.type
=== "ArrowFunctionExpression") {
46 return "arrowFunctions";
50 if (parent
.type
=== "Property") {
51 if (parent
.kind
=== "get") {
54 if (parent
.kind
=== "set") {
57 kind
= parent
.method
? "methods" : "functions";
59 } else if (parent
.type
=== "MethodDefinition") {
60 if (parent
.kind
=== "get") {
63 if (parent
.kind
=== "set") {
66 if (parent
.kind
=== "constructor") {
67 return "constructors";
80 } else if (node
.async
) {
85 return prefix
+ kind
[0].toUpperCase() + kind
.slice(1);
88 //------------------------------------------------------------------------------
90 //------------------------------------------------------------------------------
97 description
: "disallow empty functions",
98 category
: "Best Practices",
100 url
: "https://eslint.org/docs/rules/no-empty-function"
109 items
: { enum: ALLOW_OPTIONS
},
113 additionalProperties
: false
118 unexpected
: "Unexpected empty {{name}}."
123 const options
= context
.options
[0] || {};
124 const allowed
= options
.allow
|| [];
126 const sourceCode
= context
.getSourceCode();
129 * Reports a given function node if the node matches the following patterns.
131 * - Not allowed by options.
132 * - The body is empty.
133 * - The body doesn't have any comments.
134 * @param {ASTNode} node A function node to report. This is one of
135 * an ArrowFunctionExpression, a FunctionDeclaration, or a
136 * FunctionExpression.
139 function reportIfEmpty(node
) {
140 const kind
= getKind(node
);
141 const name
= astUtils
.getFunctionNameWithKind(node
);
142 const innerComments
= sourceCode
.getTokens(node
.body
, {
143 includeComments
: true,
144 filter
: astUtils
.isCommentToken
147 if (allowed
.indexOf(kind
) === -1 &&
148 node
.body
.type
=== "BlockStatement" &&
149 node
.body
.body
.length
=== 0 &&
150 innerComments
.length
=== 0
155 messageId
: "unexpected",
162 ArrowFunctionExpression
: reportIfEmpty
,
163 FunctionDeclaration
: reportIfEmpty
,
164 FunctionExpression
: reportIfEmpty