[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview A rule to ensure whitespace before blocks.
|
---|
| 3 | * @author Mathias Schreck <https://github.com/lo1tuma>
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Requirements
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | const astUtils = require("./utils/ast-utils");
|
---|
| 14 |
|
---|
| 15 | //------------------------------------------------------------------------------
|
---|
| 16 | // Helpers
|
---|
| 17 | //------------------------------------------------------------------------------
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Checks whether the given node represents the body of a function.
|
---|
| 21 | * @param {ASTNode} node the node to check.
|
---|
| 22 | * @returns {boolean} `true` if the node is function body.
|
---|
| 23 | */
|
---|
| 24 | function isFunctionBody(node) {
|
---|
| 25 | const parent = node.parent;
|
---|
| 26 |
|
---|
| 27 | return (
|
---|
| 28 | node.type === "BlockStatement" &&
|
---|
| 29 | astUtils.isFunction(parent) &&
|
---|
| 30 | parent.body === node
|
---|
| 31 | );
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | //------------------------------------------------------------------------------
|
---|
| 35 | // Rule Definition
|
---|
| 36 | //------------------------------------------------------------------------------
|
---|
| 37 |
|
---|
| 38 | /** @type {import('../shared/types').Rule} */
|
---|
| 39 | module.exports = {
|
---|
| 40 | meta: {
|
---|
| 41 | deprecated: true,
|
---|
| 42 | replacedBy: [],
|
---|
| 43 | type: "layout",
|
---|
| 44 |
|
---|
| 45 | docs: {
|
---|
| 46 | description: "Enforce consistent spacing before blocks",
|
---|
| 47 | recommended: false,
|
---|
| 48 | url: "https://eslint.org/docs/latest/rules/space-before-blocks"
|
---|
| 49 | },
|
---|
| 50 |
|
---|
| 51 | fixable: "whitespace",
|
---|
| 52 |
|
---|
| 53 | schema: [
|
---|
| 54 | {
|
---|
| 55 | oneOf: [
|
---|
| 56 | {
|
---|
| 57 | enum: ["always", "never"]
|
---|
| 58 | },
|
---|
| 59 | {
|
---|
| 60 | type: "object",
|
---|
| 61 | properties: {
|
---|
| 62 | keywords: {
|
---|
| 63 | enum: ["always", "never", "off"]
|
---|
| 64 | },
|
---|
| 65 | functions: {
|
---|
| 66 | enum: ["always", "never", "off"]
|
---|
| 67 | },
|
---|
| 68 | classes: {
|
---|
| 69 | enum: ["always", "never", "off"]
|
---|
| 70 | }
|
---|
| 71 | },
|
---|
| 72 | additionalProperties: false
|
---|
| 73 | }
|
---|
| 74 | ]
|
---|
| 75 | }
|
---|
| 76 | ],
|
---|
| 77 |
|
---|
| 78 | messages: {
|
---|
| 79 | unexpectedSpace: "Unexpected space before opening brace.",
|
---|
| 80 | missingSpace: "Missing space before opening brace."
|
---|
| 81 | }
|
---|
| 82 | },
|
---|
| 83 |
|
---|
| 84 | create(context) {
|
---|
| 85 | const config = context.options[0],
|
---|
| 86 | sourceCode = context.sourceCode;
|
---|
| 87 | let alwaysFunctions = true,
|
---|
| 88 | alwaysKeywords = true,
|
---|
| 89 | alwaysClasses = true,
|
---|
| 90 | neverFunctions = false,
|
---|
| 91 | neverKeywords = false,
|
---|
| 92 | neverClasses = false;
|
---|
| 93 |
|
---|
| 94 | if (typeof config === "object") {
|
---|
| 95 | alwaysFunctions = config.functions === "always";
|
---|
| 96 | alwaysKeywords = config.keywords === "always";
|
---|
| 97 | alwaysClasses = config.classes === "always";
|
---|
| 98 | neverFunctions = config.functions === "never";
|
---|
| 99 | neverKeywords = config.keywords === "never";
|
---|
| 100 | neverClasses = config.classes === "never";
|
---|
| 101 | } else if (config === "never") {
|
---|
| 102 | alwaysFunctions = false;
|
---|
| 103 | alwaysKeywords = false;
|
---|
| 104 | alwaysClasses = false;
|
---|
| 105 | neverFunctions = true;
|
---|
| 106 | neverKeywords = true;
|
---|
| 107 | neverClasses = true;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Checks whether the spacing before the given block is already controlled by another rule:
|
---|
| 112 | * - `arrow-spacing` checks spaces after `=>`.
|
---|
| 113 | * - `keyword-spacing` checks spaces after keywords in certain contexts.
|
---|
| 114 | * - `switch-colon-spacing` checks spaces after `:` of switch cases.
|
---|
| 115 | * @param {Token} precedingToken first token before the block.
|
---|
| 116 | * @param {ASTNode|Token} node `BlockStatement` node or `{` token of a `SwitchStatement` node.
|
---|
| 117 | * @returns {boolean} `true` if requiring or disallowing spaces before the given block could produce conflicts with other rules.
|
---|
| 118 | */
|
---|
| 119 | function isConflicted(precedingToken, node) {
|
---|
| 120 | return (
|
---|
| 121 | astUtils.isArrowToken(precedingToken) ||
|
---|
| 122 | (
|
---|
| 123 | astUtils.isKeywordToken(precedingToken) &&
|
---|
| 124 | !isFunctionBody(node)
|
---|
| 125 | ) ||
|
---|
| 126 | (
|
---|
| 127 | astUtils.isColonToken(precedingToken) &&
|
---|
| 128 | node.parent &&
|
---|
| 129 | node.parent.type === "SwitchCase" &&
|
---|
| 130 | precedingToken === astUtils.getSwitchCaseColonToken(node.parent, sourceCode)
|
---|
| 131 | )
|
---|
| 132 | );
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Checks the given BlockStatement node has a preceding space if it doesn’t start on a new line.
|
---|
| 137 | * @param {ASTNode|Token} node The AST node of a BlockStatement.
|
---|
| 138 | * @returns {void} undefined.
|
---|
| 139 | */
|
---|
| 140 | function checkPrecedingSpace(node) {
|
---|
| 141 | const precedingToken = sourceCode.getTokenBefore(node);
|
---|
| 142 |
|
---|
| 143 | if (precedingToken && !isConflicted(precedingToken, node) && astUtils.isTokenOnSameLine(precedingToken, node)) {
|
---|
| 144 | const hasSpace = sourceCode.isSpaceBetweenTokens(precedingToken, node);
|
---|
| 145 | let requireSpace;
|
---|
| 146 | let requireNoSpace;
|
---|
| 147 |
|
---|
| 148 | if (isFunctionBody(node)) {
|
---|
| 149 | requireSpace = alwaysFunctions;
|
---|
| 150 | requireNoSpace = neverFunctions;
|
---|
| 151 | } else if (node.type === "ClassBody") {
|
---|
| 152 | requireSpace = alwaysClasses;
|
---|
| 153 | requireNoSpace = neverClasses;
|
---|
| 154 | } else {
|
---|
| 155 | requireSpace = alwaysKeywords;
|
---|
| 156 | requireNoSpace = neverKeywords;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if (requireSpace && !hasSpace) {
|
---|
| 160 | context.report({
|
---|
| 161 | node,
|
---|
| 162 | messageId: "missingSpace",
|
---|
| 163 | fix(fixer) {
|
---|
| 164 | return fixer.insertTextBefore(node, " ");
|
---|
| 165 | }
|
---|
| 166 | });
|
---|
| 167 | } else if (requireNoSpace && hasSpace) {
|
---|
| 168 | context.report({
|
---|
| 169 | node,
|
---|
| 170 | messageId: "unexpectedSpace",
|
---|
| 171 | fix(fixer) {
|
---|
| 172 | return fixer.removeRange([precedingToken.range[1], node.range[0]]);
|
---|
| 173 | }
|
---|
| 174 | });
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Checks if the CaseBlock of an given SwitchStatement node has a preceding space.
|
---|
| 181 | * @param {ASTNode} node The node of a SwitchStatement.
|
---|
| 182 | * @returns {void} undefined.
|
---|
| 183 | */
|
---|
| 184 | function checkSpaceBeforeCaseBlock(node) {
|
---|
| 185 | const cases = node.cases;
|
---|
| 186 | let openingBrace;
|
---|
| 187 |
|
---|
| 188 | if (cases.length > 0) {
|
---|
| 189 | openingBrace = sourceCode.getTokenBefore(cases[0]);
|
---|
| 190 | } else {
|
---|
| 191 | openingBrace = sourceCode.getLastToken(node, 1);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | checkPrecedingSpace(openingBrace);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | return {
|
---|
| 198 | BlockStatement: checkPrecedingSpace,
|
---|
| 199 | ClassBody: checkPrecedingSpace,
|
---|
| 200 | SwitchStatement: checkSpaceBeforeCaseBlock
|
---|
| 201 | };
|
---|
| 202 |
|
---|
| 203 | }
|
---|
| 204 | };
|
---|