1 | /**
|
---|
2 | * @fileoverview Rule to enforce spacing around colons of switch statements.
|
---|
3 | * @author Toru Nagashima
|
---|
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 | // Rule Definition
|
---|
17 | //------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | /** @type {import('../shared/types').Rule} */
|
---|
20 | module.exports = {
|
---|
21 | meta: {
|
---|
22 | deprecated: true,
|
---|
23 | replacedBy: [],
|
---|
24 | type: "layout",
|
---|
25 |
|
---|
26 | docs: {
|
---|
27 | description: "Enforce spacing around colons of switch statements",
|
---|
28 | recommended: false,
|
---|
29 | url: "https://eslint.org/docs/latest/rules/switch-colon-spacing"
|
---|
30 | },
|
---|
31 |
|
---|
32 | schema: [
|
---|
33 | {
|
---|
34 | type: "object",
|
---|
35 | properties: {
|
---|
36 | before: { type: "boolean", default: false },
|
---|
37 | after: { type: "boolean", default: true }
|
---|
38 | },
|
---|
39 | additionalProperties: false
|
---|
40 | }
|
---|
41 | ],
|
---|
42 | fixable: "whitespace",
|
---|
43 | messages: {
|
---|
44 | expectedBefore: "Expected space(s) before this colon.",
|
---|
45 | expectedAfter: "Expected space(s) after this colon.",
|
---|
46 | unexpectedBefore: "Unexpected space(s) before this colon.",
|
---|
47 | unexpectedAfter: "Unexpected space(s) after this colon."
|
---|
48 | }
|
---|
49 | },
|
---|
50 |
|
---|
51 | create(context) {
|
---|
52 | const sourceCode = context.sourceCode;
|
---|
53 | const options = context.options[0] || {};
|
---|
54 | const beforeSpacing = options.before === true; // false by default
|
---|
55 | const afterSpacing = options.after !== false; // true by default
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Check whether the spacing between the given 2 tokens is valid or not.
|
---|
59 | * @param {Token} left The left token to check.
|
---|
60 | * @param {Token} right The right token to check.
|
---|
61 | * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
|
---|
62 | * @returns {boolean} `true` if the spacing between the tokens is valid.
|
---|
63 | */
|
---|
64 | function isValidSpacing(left, right, expected) {
|
---|
65 | return (
|
---|
66 | astUtils.isClosingBraceToken(right) ||
|
---|
67 | !astUtils.isTokenOnSameLine(left, right) ||
|
---|
68 | sourceCode.isSpaceBetweenTokens(left, right) === expected
|
---|
69 | );
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Check whether comments exist between the given 2 tokens.
|
---|
74 | * @param {Token} left The left token to check.
|
---|
75 | * @param {Token} right The right token to check.
|
---|
76 | * @returns {boolean} `true` if comments exist between the given 2 tokens.
|
---|
77 | */
|
---|
78 | function commentsExistBetween(left, right) {
|
---|
79 | return sourceCode.getFirstTokenBetween(
|
---|
80 | left,
|
---|
81 | right,
|
---|
82 | {
|
---|
83 | includeComments: true,
|
---|
84 | filter: astUtils.isCommentToken
|
---|
85 | }
|
---|
86 | ) !== null;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Fix the spacing between the given 2 tokens.
|
---|
91 | * @param {RuleFixer} fixer The fixer to fix.
|
---|
92 | * @param {Token} left The left token of fix range.
|
---|
93 | * @param {Token} right The right token of fix range.
|
---|
94 | * @param {boolean} spacing The spacing style. `true` if there should be a space.
|
---|
95 | * @returns {Fix|null} The fix object.
|
---|
96 | */
|
---|
97 | function fix(fixer, left, right, spacing) {
|
---|
98 | if (commentsExistBetween(left, right)) {
|
---|
99 | return null;
|
---|
100 | }
|
---|
101 | if (spacing) {
|
---|
102 | return fixer.insertTextAfter(left, " ");
|
---|
103 | }
|
---|
104 | return fixer.removeRange([left.range[1], right.range[0]]);
|
---|
105 | }
|
---|
106 |
|
---|
107 | return {
|
---|
108 | SwitchCase(node) {
|
---|
109 | const colonToken = astUtils.getSwitchCaseColonToken(node, sourceCode);
|
---|
110 | const beforeToken = sourceCode.getTokenBefore(colonToken);
|
---|
111 | const afterToken = sourceCode.getTokenAfter(colonToken);
|
---|
112 |
|
---|
113 | if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) {
|
---|
114 | context.report({
|
---|
115 | node,
|
---|
116 | loc: colonToken.loc,
|
---|
117 | messageId: beforeSpacing ? "expectedBefore" : "unexpectedBefore",
|
---|
118 | fix: fixer => fix(fixer, beforeToken, colonToken, beforeSpacing)
|
---|
119 | });
|
---|
120 | }
|
---|
121 | if (!isValidSpacing(colonToken, afterToken, afterSpacing)) {
|
---|
122 | context.report({
|
---|
123 | node,
|
---|
124 | loc: colonToken.loc,
|
---|
125 | messageId: afterSpacing ? "expectedAfter" : "unexpectedAfter",
|
---|
126 | fix: fixer => fix(fixer, colonToken, afterToken, afterSpacing)
|
---|
127 | });
|
---|
128 | }
|
---|
129 | }
|
---|
130 | };
|
---|
131 | }
|
---|
132 | };
|
---|