[d565449] | 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 | hasSuggestions: true,
|
---|
| 21 | type: "suggestion",
|
---|
| 22 |
|
---|
| 23 | docs: {
|
---|
| 24 | description: "Disallow empty block statements",
|
---|
| 25 | recommended: true,
|
---|
| 26 | url: "https://eslint.org/docs/latest/rules/no-empty"
|
---|
| 27 | },
|
---|
| 28 |
|
---|
| 29 | schema: [
|
---|
| 30 | {
|
---|
| 31 | type: "object",
|
---|
| 32 | properties: {
|
---|
| 33 | allowEmptyCatch: {
|
---|
| 34 | type: "boolean",
|
---|
| 35 | default: false
|
---|
| 36 | }
|
---|
| 37 | },
|
---|
| 38 | additionalProperties: false
|
---|
| 39 | }
|
---|
| 40 | ],
|
---|
| 41 |
|
---|
| 42 | messages: {
|
---|
| 43 | unexpected: "Empty {{type}} statement.",
|
---|
| 44 | suggestComment: "Add comment inside empty {{type}} statement."
|
---|
| 45 | }
|
---|
| 46 | },
|
---|
| 47 |
|
---|
| 48 | create(context) {
|
---|
| 49 | const options = context.options[0] || {},
|
---|
| 50 | allowEmptyCatch = options.allowEmptyCatch || false;
|
---|
| 51 |
|
---|
| 52 | const sourceCode = context.sourceCode;
|
---|
| 53 |
|
---|
| 54 | return {
|
---|
| 55 | BlockStatement(node) {
|
---|
| 56 |
|
---|
| 57 | // if the body is not empty, we can just return immediately
|
---|
| 58 | if (node.body.length !== 0) {
|
---|
| 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // a function is generally allowed to be empty
|
---|
| 63 | if (astUtils.isFunction(node.parent)) {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (allowEmptyCatch && node.parent.type === "CatchClause") {
|
---|
| 68 | return;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | // any other block is only allowed to be empty, if it contains a comment
|
---|
| 72 | if (sourceCode.getCommentsInside(node).length > 0) {
|
---|
| 73 | return;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | context.report({
|
---|
| 77 | node,
|
---|
| 78 | messageId: "unexpected",
|
---|
| 79 | data: { type: "block" },
|
---|
| 80 | suggest: [
|
---|
| 81 | {
|
---|
| 82 | messageId: "suggestComment",
|
---|
| 83 | data: { type: "block" },
|
---|
| 84 | fix(fixer) {
|
---|
| 85 | const range = [node.range[0] + 1, node.range[1] - 1];
|
---|
| 86 |
|
---|
| 87 | return fixer.replaceTextRange(range, " /* empty */ ");
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | ]
|
---|
| 91 | });
|
---|
| 92 | },
|
---|
| 93 |
|
---|
| 94 | SwitchStatement(node) {
|
---|
| 95 |
|
---|
| 96 | if (typeof node.cases === "undefined" || node.cases.length === 0) {
|
---|
| 97 | context.report({ node, messageId: "unexpected", data: { type: "switch" } });
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | }
|
---|
| 103 | };
|
---|