]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-multi-spaces.js
2 * @fileoverview Disallow use of multiple spaces.
3 * @author Nicholas C. Zakas
8 const astUtils
= require("./utils/ast-utils");
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
19 description
: "disallow multiple spaces",
21 url
: "https://eslint.org/docs/rules/no-multi-spaces"
24 fixable
: "whitespace",
37 additionalProperties
: false
44 additionalProperties
: false
49 multipleSpaces
: "Multiple spaces found before '{{displayValue}}'."
54 const sourceCode
= context
.getSourceCode();
55 const options
= context
.options
[0] || {};
56 const ignoreEOLComments
= options
.ignoreEOLComments
;
57 const exceptions
= Object
.assign({ Property
: true }, options
.exceptions
);
58 const hasExceptions
= Object
.keys(exceptions
).filter(key
=> exceptions
[key
]).length
> 0;
61 * Formats value of given comment token for error message by truncating its length.
62 * @param {Token} token comment token
63 * @returns {string} formatted value
66 function formatReportedCommentValue(token
) {
67 const valueLines
= token
.value
.split("\n");
68 const value
= valueLines
[0];
69 const formattedValue
= `${value.slice(0, 12)}...`;
71 return valueLines
.length
=== 1 && value
.length
<= 12 ? value
: formattedValue
;
74 //--------------------------------------------------------------------------
76 //--------------------------------------------------------------------------
80 sourceCode
.tokensAndComments
.forEach((leftToken
, leftIndex
, tokensAndComments
) => {
81 if (leftIndex
=== tokensAndComments
.length
- 1) {
84 const rightToken
= tokensAndComments
[leftIndex
+ 1];
86 // Ignore tokens that don't have 2 spaces between them or are on different lines
88 !sourceCode
.text
.slice(leftToken
.range
[1], rightToken
.range
[0]).includes(" ") ||
89 leftToken
.loc
.end
.line
< rightToken
.loc
.start
.line
94 // Ignore comments that are the last token on their line if `ignoreEOLComments` is active.
97 astUtils
.isCommentToken(rightToken
) &&
99 leftIndex
=== tokensAndComments
.length
- 2 ||
100 rightToken
.loc
.end
.line
< tokensAndComments
[leftIndex
+ 2].loc
.start
.line
106 // Ignore tokens that are in a node in the "exceptions" object
108 const parentNode
= sourceCode
.getNodeByRangeIndex(rightToken
.range
[0] - 1);
110 if (parentNode
&& exceptions
[parentNode
.type
]) {
117 if (rightToken
.type
=== "Block") {
118 displayValue
= `/*${formatReportedCommentValue(rightToken)}*/`;
119 } else if (rightToken
.type
=== "Line") {
120 displayValue
= `//${formatReportedCommentValue(rightToken)}`;
122 displayValue
= rightToken
.value
;
127 loc
: { start
: leftToken
.loc
.end
, end
: rightToken
.loc
.start
},
128 messageId
: "multipleSpaces",
129 data
: { displayValue
},
130 fix
: fixer
=> fixer
.replaceTextRange([leftToken
.range
[1], rightToken
.range
[0]], " ")