UNPKG

5.02 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 },
25 schema: [
26 {
27 type: "object",
28 properties: {
29 before: { type: "boolean" },
30 after: { type: "boolean" }
31 },
32 additionalProperties: false
33 }
34 ],
35 fixable: "whitespace"
36 },
37
38 create(context) {
39 const sourceCode = context.getSourceCode();
40 const options = context.options[0] || {};
41 const beforeSpacing = options.before === true; // false by default
42 const afterSpacing = options.after !== false; // true by default
43
44 /**
45 * Get the colon token of the given SwitchCase node.
46 * @param {ASTNode} node The SwitchCase node to get.
47 * @returns {Token} The colon token of the node.
48 */
49 function getColonToken(node) {
50 if (node.test) {
51 return sourceCode.getTokenAfter(node.test, astUtils.isColonToken);
52 }
53 return sourceCode.getFirstToken(node, 1);
54 }
55
56 /**
57 * Check whether the spacing between the given 2 tokens is valid or not.
58 * @param {Token} left The left token to check.
59 * @param {Token} right The right token to check.
60 * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
61 * @returns {boolean} `true` if the spacing between the tokens is valid.
62 */
63 function isValidSpacing(left, right, expected) {
64 return (
65 astUtils.isClosingBraceToken(right) ||
66 !astUtils.isTokenOnSameLine(left, right) ||
67 sourceCode.isSpaceBetweenTokens(left, right) === expected
68 );
69 }
70
71 /**
72 * Check whether comments exist between the given 2 tokens.
73 * @param {Token} left The left token to check.
74 * @param {Token} right The right token to check.
75 * @returns {boolean} `true` if comments exist between the given 2 tokens.
76 */
77 function commentsExistBetween(left, right) {
78 return sourceCode.getFirstTokenBetween(
79 left,
80 right,
81 {
82 includeComments: true,
83 filter: astUtils.isCommentToken
84 }
85 ) !== null;
86 }
87
88 /**
89 * Fix the spacing between the given 2 tokens.
90 * @param {RuleFixer} fixer The fixer to fix.
91 * @param {Token} left The left token of fix range.
92 * @param {Token} right The right token of fix range.
93 * @param {boolean} spacing The spacing style. `true` if there should be a space.
94 * @returns {Fix|null} The fix object.
95 */
96 function fix(fixer, left, right, spacing) {
97 if (commentsExistBetween(left, right)) {
98 return null;
99 }
100 if (spacing) {
101 return fixer.insertTextAfter(left, " ");
102 }
103 return fixer.removeRange([left.range[1], right.range[0]]);
104 }
105
106 return {
107 SwitchCase(node) {
108 const colonToken = getColonToken(node);
109 const beforeToken = sourceCode.getTokenBefore(colonToken);
110 const afterToken = sourceCode.getTokenAfter(colonToken);
111
112 if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) {
113 context.report({
114 node,
115 loc: colonToken.loc,
116 message: "{{verb}} space(s) before this colon.",
117 data: { verb: beforeSpacing ? "Expected" : "Unexpected" },
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 message: "{{verb}} space(s) after this colon.",
126 data: { verb: afterSpacing ? "Expected" : "Unexpected" },
127 fix: fixer => fix(fixer, colonToken, afterToken, afterSpacing)
128 });
129 }
130 }
131 };
132 }
133};