]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-empty.js
import 8.23.1 source
[pve-eslint.git] / eslint / lib / rules / no-empty.js
1 /**
2 * @fileoverview Rule to flag use of an empty block statement
3 * @author Nicholas C. Zakas
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const astUtils = require("./utils/ast-utils");
12
13 //------------------------------------------------------------------------------
14 // Rule Definition
15 //------------------------------------------------------------------------------
16
17 /** @type {import('../shared/types').Rule} */
18 module.exports = {
19 meta: {
20 type: "suggestion",
21
22 docs: {
23 description: "Disallow empty block statements",
24 recommended: true,
25 url: "https://eslint.org/docs/rules/no-empty"
26 },
27
28 schema: [
29 {
30 type: "object",
31 properties: {
32 allowEmptyCatch: {
33 type: "boolean",
34 default: false
35 }
36 },
37 additionalProperties: false
38 }
39 ],
40
41 messages: {
42 unexpected: "Empty {{type}} statement."
43 }
44 },
45
46 create(context) {
47 const options = context.options[0] || {},
48 allowEmptyCatch = options.allowEmptyCatch || false;
49
50 const sourceCode = context.getSourceCode();
51
52 return {
53 BlockStatement(node) {
54
55 // if the body is not empty, we can just return immediately
56 if (node.body.length !== 0) {
57 return;
58 }
59
60 // a function is generally allowed to be empty
61 if (astUtils.isFunction(node.parent)) {
62 return;
63 }
64
65 if (allowEmptyCatch && node.parent.type === "CatchClause") {
66 return;
67 }
68
69 // any other block is only allowed to be empty, if it contains a comment
70 if (sourceCode.getCommentsInside(node).length > 0) {
71 return;
72 }
73
74 context.report({ node, messageId: "unexpected", data: { type: "block" } });
75 },
76
77 SwitchStatement(node) {
78
79 if (typeof node.cases === "undefined" || node.cases.length === 0) {
80 context.report({ node, messageId: "unexpected", data: { type: "switch" } });
81 }
82 }
83 };
84
85 }
86 };