UNPKG

5.09 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to enforce spacing around colons of switch statements.
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("../ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18module.exports = {
19 meta: {
20 docs: {
21 description: "enforce spacing around colons of switch statements",
22 category: "Stylistic Issues",
23 recommended: false,
24 url: "https://eslint.org/docs/rules/switch-colon-spacing"
25 },
26 schema: [
27 {
28 type: "object",
29 properties: {
30 before: { type: "boolean" },
31 after: { type: "boolean" }
32 },
33 additionalProperties: false
34 }
35 ],
36 fixable: "whitespace"
37 },
38
39 create(context) {
40 const sourceCode = context.getSourceCode();
41 const options = context.options[0] || {};
42 const beforeSpacing = options.before === true; // false by default
43 const afterSpacing = options.after !== false; // true by default
44
45 /**
46 * Get the colon token of the given SwitchCase node.
47 * @param {ASTNode} node The SwitchCase node to get.
48 * @returns {Token} The colon token of the node.
49 */
50 function getColonToken(node) {
51 if (node.test) {
52 return sourceCode.getTokenAfter(node.test, astUtils.isColonToken);
53 }
54 return sourceCode.getFirstToken(node, 1);
55 }
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 = getColonToken(node);
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 message: "{{verb}} space(s) before this colon.",
118 data: { verb: beforeSpacing ? "Expected" : "Unexpected" },
119 fix: fixer => fix(fixer, beforeToken, colonToken, beforeSpacing)
120 });
121 }
122 if (!isValidSpacing(colonToken, afterToken, afterSpacing)) {
123 context.report({
124 node,
125 loc: colonToken.loc,
126 message: "{{verb}} space(s) after this colon.",
127 data: { verb: afterSpacing ? "Expected" : "Unexpected" },
128 fix: fixer => fix(fixer, colonToken, afterToken, afterSpacing)
129 });
130 }
131 }
132 };
133 }
134};