]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-empty-function.js
8b1073a59d86444d89bf1cb16b0a274507a3fb6f
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",
99 url
: "https://eslint.org/docs/rules/no-empty-function"
108 items
: { enum: ALLOW_OPTIONS
},
112 additionalProperties
: false
117 unexpected
: "Unexpected empty {{name}}."
122 const options
= context
.options
[0] || {};
123 const allowed
= options
.allow
|| [];
125 const sourceCode
= context
.getSourceCode();
128 * Reports a given function node if the node matches the following patterns.
130 * - Not allowed by options.
131 * - The body is empty.
132 * - The body doesn't have any comments.
133 * @param {ASTNode} node A function node to report. This is one of
134 * an ArrowFunctionExpression, a FunctionDeclaration, or a
135 * FunctionExpression.
138 function reportIfEmpty(node
) {
139 const kind
= getKind(node
);
140 const name
= astUtils
.getFunctionNameWithKind(node
);
141 const innerComments
= sourceCode
.getTokens(node
.body
, {
142 includeComments
: true,
143 filter
: astUtils
.isCommentToken
146 if (allowed
.indexOf(kind
) === -1 &&
147 node
.body
.type
=== "BlockStatement" &&
148 node
.body
.body
.length
=== 0 &&
149 innerComments
.length
=== 0
154 messageId
: "unexpected",
161 ArrowFunctionExpression
: reportIfEmpty
,
162 FunctionDeclaration
: reportIfEmpty
,
163 FunctionExpression
: reportIfEmpty