]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/switch-colon-spacing.js
2 * @fileoverview Rule to enforce spacing around colons of switch statements.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 /** @type {import('../shared/types').Rule} */
24 description
: "enforce spacing around colons of switch statements",
26 url
: "https://eslint.org/docs/rules/switch-colon-spacing"
33 before
: { type
: "boolean", default: false },
34 after
: { type
: "boolean", default: true }
36 additionalProperties
: false
39 fixable
: "whitespace",
41 expectedBefore
: "Expected space(s) before this colon.",
42 expectedAfter
: "Expected space(s) after this colon.",
43 unexpectedBefore
: "Unexpected space(s) before this colon.",
44 unexpectedAfter
: "Unexpected space(s) after this colon."
49 const sourceCode
= context
.getSourceCode();
50 const options
= context
.options
[0] || {};
51 const beforeSpacing
= options
.before
=== true; // false by default
52 const afterSpacing
= options
.after
!== false; // true by default
55 * Check whether the spacing between the given 2 tokens is valid or not.
56 * @param {Token} left The left token to check.
57 * @param {Token} right The right token to check.
58 * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
59 * @returns {boolean} `true` if the spacing between the tokens is valid.
61 function isValidSpacing(left
, right
, expected
) {
63 astUtils
.isClosingBraceToken(right
) ||
64 !astUtils
.isTokenOnSameLine(left
, right
) ||
65 sourceCode
.isSpaceBetweenTokens(left
, right
) === expected
70 * Check whether comments exist between the given 2 tokens.
71 * @param {Token} left The left token to check.
72 * @param {Token} right The right token to check.
73 * @returns {boolean} `true` if comments exist between the given 2 tokens.
75 function commentsExistBetween(left
, right
) {
76 return sourceCode
.getFirstTokenBetween(
80 includeComments
: true,
81 filter
: astUtils
.isCommentToken
87 * Fix the spacing between the given 2 tokens.
88 * @param {RuleFixer} fixer The fixer to fix.
89 * @param {Token} left The left token of fix range.
90 * @param {Token} right The right token of fix range.
91 * @param {boolean} spacing The spacing style. `true` if there should be a space.
92 * @returns {Fix|null} The fix object.
94 function fix(fixer
, left
, right
, spacing
) {
95 if (commentsExistBetween(left
, right
)) {
99 return fixer
.insertTextAfter(left
, " ");
101 return fixer
.removeRange([left
.range
[1], right
.range
[0]]);
106 const colonToken
= astUtils
.getSwitchCaseColonToken(node
, sourceCode
);
107 const beforeToken
= sourceCode
.getTokenBefore(colonToken
);
108 const afterToken
= sourceCode
.getTokenAfter(colonToken
);
110 if (!isValidSpacing(beforeToken
, colonToken
, beforeSpacing
)) {
114 messageId
: beforeSpacing
? "expectedBefore" : "unexpectedBefore",
115 fix
: fixer
=> fix(fixer
, beforeToken
, colonToken
, beforeSpacing
)
118 if (!isValidSpacing(colonToken
, afterToken
, afterSpacing
)) {
122 messageId
: afterSpacing
? "expectedAfter" : "unexpectedAfter",
123 fix
: fixer
=> fix(fixer
, colonToken
, afterToken
, afterSpacing
)