]>
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 //------------------------------------------------------------------------------
23 description
: "enforce spacing around colons of switch statements",
24 category
: "Stylistic Issues",
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 * Get the colon token of the given SwitchCase node.
56 * @param {ASTNode} node The SwitchCase node to get.
57 * @returns {Token} The colon token of the node.
59 function getColonToken(node
) {
61 return sourceCode
.getTokenAfter(node
.test
, astUtils
.isColonToken
);
63 return sourceCode
.getFirstToken(node
, 1);
67 * Check whether the spacing between the given 2 tokens is valid or not.
68 * @param {Token} left The left token to check.
69 * @param {Token} right The right token to check.
70 * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
71 * @returns {boolean} `true` if the spacing between the tokens is valid.
73 function isValidSpacing(left
, right
, expected
) {
75 astUtils
.isClosingBraceToken(right
) ||
76 !astUtils
.isTokenOnSameLine(left
, right
) ||
77 sourceCode
.isSpaceBetweenTokens(left
, right
) === expected
82 * Check whether comments exist between the given 2 tokens.
83 * @param {Token} left The left token to check.
84 * @param {Token} right The right token to check.
85 * @returns {boolean} `true` if comments exist between the given 2 tokens.
87 function commentsExistBetween(left
, right
) {
88 return sourceCode
.getFirstTokenBetween(
92 includeComments
: true,
93 filter
: astUtils
.isCommentToken
99 * Fix the spacing between the given 2 tokens.
100 * @param {RuleFixer} fixer The fixer to fix.
101 * @param {Token} left The left token of fix range.
102 * @param {Token} right The right token of fix range.
103 * @param {boolean} spacing The spacing style. `true` if there should be a space.
104 * @returns {Fix|null} The fix object.
106 function fix(fixer
, left
, right
, spacing
) {
107 if (commentsExistBetween(left
, right
)) {
111 return fixer
.insertTextAfter(left
, " ");
113 return fixer
.removeRange([left
.range
[1], right
.range
[0]]);
118 const colonToken
= getColonToken(node
);
119 const beforeToken
= sourceCode
.getTokenBefore(colonToken
);
120 const afterToken
= sourceCode
.getTokenAfter(colonToken
);
122 if (!isValidSpacing(beforeToken
, colonToken
, beforeSpacing
)) {
126 messageId
: beforeSpacing
? "expectedBefore" : "unexpectedBefore",
127 fix
: fixer
=> fix(fixer
, beforeToken
, colonToken
, beforeSpacing
)
130 if (!isValidSpacing(colonToken
, afterToken
, afterSpacing
)) {
134 messageId
: afterSpacing
? "expectedAfter" : "unexpectedAfter",
135 fix
: fixer
=> fix(fixer
, colonToken
, afterToken
, afterSpacing
)