[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Validates spacing before and after semicolon
|
---|
| 3 | * @author Mathias Schreck
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | const astUtils = require("./utils/ast-utils");
|
---|
| 10 |
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 | // Rule Definition
|
---|
| 13 | //------------------------------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | /** @type {import('../shared/types').Rule} */
|
---|
| 16 | module.exports = {
|
---|
| 17 | meta: {
|
---|
| 18 | deprecated: true,
|
---|
| 19 | replacedBy: [],
|
---|
| 20 | type: "layout",
|
---|
| 21 |
|
---|
| 22 | docs: {
|
---|
| 23 | description: "Enforce consistent spacing before and after semicolons",
|
---|
| 24 | recommended: false,
|
---|
| 25 | url: "https://eslint.org/docs/latest/rules/semi-spacing"
|
---|
| 26 | },
|
---|
| 27 |
|
---|
| 28 | fixable: "whitespace",
|
---|
| 29 |
|
---|
| 30 | schema: [
|
---|
| 31 | {
|
---|
| 32 | type: "object",
|
---|
| 33 | properties: {
|
---|
| 34 | before: {
|
---|
| 35 | type: "boolean",
|
---|
| 36 | default: false
|
---|
| 37 | },
|
---|
| 38 | after: {
|
---|
| 39 | type: "boolean",
|
---|
| 40 | default: true
|
---|
| 41 | }
|
---|
| 42 | },
|
---|
| 43 | additionalProperties: false
|
---|
| 44 | }
|
---|
| 45 | ],
|
---|
| 46 |
|
---|
| 47 | messages: {
|
---|
| 48 | unexpectedWhitespaceBefore: "Unexpected whitespace before semicolon.",
|
---|
| 49 | unexpectedWhitespaceAfter: "Unexpected whitespace after semicolon.",
|
---|
| 50 | missingWhitespaceBefore: "Missing whitespace before semicolon.",
|
---|
| 51 | missingWhitespaceAfter: "Missing whitespace after semicolon."
|
---|
| 52 | }
|
---|
| 53 | },
|
---|
| 54 |
|
---|
| 55 | create(context) {
|
---|
| 56 |
|
---|
| 57 | const config = context.options[0],
|
---|
| 58 | sourceCode = context.sourceCode;
|
---|
| 59 | let requireSpaceBefore = false,
|
---|
| 60 | requireSpaceAfter = true;
|
---|
| 61 |
|
---|
| 62 | if (typeof config === "object") {
|
---|
| 63 | requireSpaceBefore = config.before;
|
---|
| 64 | requireSpaceAfter = config.after;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Checks if a given token has leading whitespace.
|
---|
| 69 | * @param {Object} token The token to check.
|
---|
| 70 | * @returns {boolean} True if the given token has leading space, false if not.
|
---|
| 71 | */
|
---|
| 72 | function hasLeadingSpace(token) {
|
---|
| 73 | const tokenBefore = sourceCode.getTokenBefore(token);
|
---|
| 74 |
|
---|
| 75 | return tokenBefore && astUtils.isTokenOnSameLine(tokenBefore, token) && sourceCode.isSpaceBetweenTokens(tokenBefore, token);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Checks if a given token has trailing whitespace.
|
---|
| 80 | * @param {Object} token The token to check.
|
---|
| 81 | * @returns {boolean} True if the given token has trailing space, false if not.
|
---|
| 82 | */
|
---|
| 83 | function hasTrailingSpace(token) {
|
---|
| 84 | const tokenAfter = sourceCode.getTokenAfter(token);
|
---|
| 85 |
|
---|
| 86 | return tokenAfter && astUtils.isTokenOnSameLine(token, tokenAfter) && sourceCode.isSpaceBetweenTokens(token, tokenAfter);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Checks if the given token is the last token in its line.
|
---|
| 91 | * @param {Token} token The token to check.
|
---|
| 92 | * @returns {boolean} Whether or not the token is the last in its line.
|
---|
| 93 | */
|
---|
| 94 | function isLastTokenInCurrentLine(token) {
|
---|
| 95 | const tokenAfter = sourceCode.getTokenAfter(token);
|
---|
| 96 |
|
---|
| 97 | return !(tokenAfter && astUtils.isTokenOnSameLine(token, tokenAfter));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Checks if the given token is the first token in its line
|
---|
| 102 | * @param {Token} token The token to check.
|
---|
| 103 | * @returns {boolean} Whether or not the token is the first in its line.
|
---|
| 104 | */
|
---|
| 105 | function isFirstTokenInCurrentLine(token) {
|
---|
| 106 | const tokenBefore = sourceCode.getTokenBefore(token);
|
---|
| 107 |
|
---|
| 108 | return !(tokenBefore && astUtils.isTokenOnSameLine(token, tokenBefore));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Checks if the next token of a given token is a closing parenthesis.
|
---|
| 113 | * @param {Token} token The token to check.
|
---|
| 114 | * @returns {boolean} Whether or not the next token of a given token is a closing parenthesis.
|
---|
| 115 | */
|
---|
| 116 | function isBeforeClosingParen(token) {
|
---|
| 117 | const nextToken = sourceCode.getTokenAfter(token);
|
---|
| 118 |
|
---|
| 119 | return (nextToken && astUtils.isClosingBraceToken(nextToken) || astUtils.isClosingParenToken(nextToken));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * Report location example :
|
---|
| 124 | *
|
---|
| 125 | * for unexpected space `before`
|
---|
| 126 | *
|
---|
| 127 | * var a = 'b' ;
|
---|
| 128 | * ^^^
|
---|
| 129 | *
|
---|
| 130 | * for unexpected space `after`
|
---|
| 131 | *
|
---|
| 132 | * var a = 'b'; c = 10;
|
---|
| 133 | * ^^
|
---|
| 134 | *
|
---|
| 135 | * Reports if the given token has invalid spacing.
|
---|
| 136 | * @param {Token} token The semicolon token to check.
|
---|
| 137 | * @param {ASTNode} node The corresponding node of the token.
|
---|
| 138 | * @returns {void}
|
---|
| 139 | */
|
---|
| 140 | function checkSemicolonSpacing(token, node) {
|
---|
| 141 | if (astUtils.isSemicolonToken(token)) {
|
---|
| 142 | if (hasLeadingSpace(token)) {
|
---|
| 143 | if (!requireSpaceBefore) {
|
---|
| 144 | const tokenBefore = sourceCode.getTokenBefore(token);
|
---|
| 145 | const loc = {
|
---|
| 146 | start: tokenBefore.loc.end,
|
---|
| 147 | end: token.loc.start
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 | context.report({
|
---|
| 151 | node,
|
---|
| 152 | loc,
|
---|
| 153 | messageId: "unexpectedWhitespaceBefore",
|
---|
| 154 | fix(fixer) {
|
---|
| 155 |
|
---|
| 156 | return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
|
---|
| 157 | }
|
---|
| 158 | });
|
---|
| 159 | }
|
---|
| 160 | } else {
|
---|
| 161 | if (requireSpaceBefore) {
|
---|
| 162 | const loc = token.loc;
|
---|
| 163 |
|
---|
| 164 | context.report({
|
---|
| 165 | node,
|
---|
| 166 | loc,
|
---|
| 167 | messageId: "missingWhitespaceBefore",
|
---|
| 168 | fix(fixer) {
|
---|
| 169 | return fixer.insertTextBefore(token, " ");
|
---|
| 170 | }
|
---|
| 171 | });
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (!isFirstTokenInCurrentLine(token) && !isLastTokenInCurrentLine(token) && !isBeforeClosingParen(token)) {
|
---|
| 176 | if (hasTrailingSpace(token)) {
|
---|
| 177 | if (!requireSpaceAfter) {
|
---|
| 178 | const tokenAfter = sourceCode.getTokenAfter(token);
|
---|
| 179 | const loc = {
|
---|
| 180 | start: token.loc.end,
|
---|
| 181 | end: tokenAfter.loc.start
|
---|
| 182 | };
|
---|
| 183 |
|
---|
| 184 | context.report({
|
---|
| 185 | node,
|
---|
| 186 | loc,
|
---|
| 187 | messageId: "unexpectedWhitespaceAfter",
|
---|
| 188 | fix(fixer) {
|
---|
| 189 |
|
---|
| 190 | return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
|
---|
| 191 | }
|
---|
| 192 | });
|
---|
| 193 | }
|
---|
| 194 | } else {
|
---|
| 195 | if (requireSpaceAfter) {
|
---|
| 196 | const loc = token.loc;
|
---|
| 197 |
|
---|
| 198 | context.report({
|
---|
| 199 | node,
|
---|
| 200 | loc,
|
---|
| 201 | messageId: "missingWhitespaceAfter",
|
---|
| 202 | fix(fixer) {
|
---|
| 203 | return fixer.insertTextAfter(token, " ");
|
---|
| 204 | }
|
---|
| 205 | });
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * Checks the spacing of the semicolon with the assumption that the last token is the semicolon.
|
---|
| 214 | * @param {ASTNode} node The node to check.
|
---|
| 215 | * @returns {void}
|
---|
| 216 | */
|
---|
| 217 | function checkNode(node) {
|
---|
| 218 | const token = sourceCode.getLastToken(node);
|
---|
| 219 |
|
---|
| 220 | checkSemicolonSpacing(token, node);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | return {
|
---|
| 224 | VariableDeclaration: checkNode,
|
---|
| 225 | ExpressionStatement: checkNode,
|
---|
| 226 | BreakStatement: checkNode,
|
---|
| 227 | ContinueStatement: checkNode,
|
---|
| 228 | DebuggerStatement: checkNode,
|
---|
| 229 | DoWhileStatement: checkNode,
|
---|
| 230 | ReturnStatement: checkNode,
|
---|
| 231 | ThrowStatement: checkNode,
|
---|
| 232 | ImportDeclaration: checkNode,
|
---|
| 233 | ExportNamedDeclaration: checkNode,
|
---|
| 234 | ExportAllDeclaration: checkNode,
|
---|
| 235 | ExportDefaultDeclaration: checkNode,
|
---|
| 236 | ForStatement(node) {
|
---|
| 237 | if (node.init) {
|
---|
| 238 | checkSemicolonSpacing(sourceCode.getTokenAfter(node.init), node);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | if (node.test) {
|
---|
| 242 | checkSemicolonSpacing(sourceCode.getTokenAfter(node.test), node);
|
---|
| 243 | }
|
---|
| 244 | },
|
---|
| 245 | PropertyDefinition: checkNode
|
---|
| 246 | };
|
---|
| 247 | }
|
---|
| 248 | };
|
---|