]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-trailing-spaces.js
1930098bea00404a393a14b8936ed3ef2f9864fa
2 * @fileoverview Disallow trailing spaces at the end of lines.
3 * @author Nodeca Team <https://github.com/nodeca>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils
= require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
22 description
: "disallow trailing whitespace at the end of lines",
24 url
: "https://eslint.org/docs/rules/no-trailing-spaces"
27 fixable
: "whitespace",
42 additionalProperties
: false
47 trailingSpace
: "Trailing spaces not allowed."
52 const sourceCode
= context
.getSourceCode();
54 const BLANK_CLASS
= "[ \t\u00a0\u2000-\u200b\u3000]",
55 SKIP_BLANK
= `^${BLANK_CLASS}*$`,
56 NONBLANK
= `${BLANK_CLASS}+$`;
58 const options
= context
.options
[0] || {},
59 skipBlankLines
= options
.skipBlankLines
|| false,
60 ignoreComments
= options
.ignoreComments
|| false;
63 * Report the error message
64 * @param {ASTNode} node node to report
65 * @param {int[]} location range information
66 * @param {int[]} fixRange Range based on the whole program
69 function report(node
, location
, fixRange
) {
72 * Passing node is a bit dirty, because message data will contain big
73 * text in `source`. But... who cares :) ?
74 * One more kludge will not make worse the bloody wizardry of this
80 messageId
: "trailingSpace",
82 return fixer
.removeRange(fixRange
);
88 * Given a list of comment nodes, return the line numbers for those comments.
89 * @param {Array} comments An array of comment nodes.
90 * @returns {number[]} An array of line numbers containing comments.
92 function getCommentLineNumbers(comments
) {
93 const lines
= new Set();
95 comments
.forEach(comment
=> {
96 const endLine
= comment
.type
=== "Block"
97 ? comment
.loc
.end
.line
- 1
98 : comment
.loc
.end
.line
;
100 for (let i
= comment
.loc
.start
.line
; i
<= endLine
; i
++) {
108 //--------------------------------------------------------------------------
110 //--------------------------------------------------------------------------
114 Program
: function checkTrailingSpaces(node
) {
117 * Let's hack. Since Espree does not return whitespace nodes,
118 * fetch the source code and do matching via regexps.
121 const re
= new RegExp(NONBLANK
, "u"),
122 skipMatch
= new RegExp(SKIP_BLANK
, "u"),
123 lines
= sourceCode
.lines
,
124 linebreaks
= sourceCode
.getText().match(astUtils
.createGlobalLinebreakMatcher()),
125 comments
= sourceCode
.getAllComments(),
126 commentLineNumbers
= getCommentLineNumbers(comments
);
131 for (let i
= 0, ii
= lines
.length
; i
< ii
; i
++) {
132 const lineNumber
= i
+ 1;
135 * Always add linebreak length to line length to accommodate for line break (\n or \r\n)
136 * Because during the fix time they also reserve one spot in the array.
137 * Usually linebreak length is 2 for \r\n (CRLF) and 1 for \n (LF)
139 const linebreakLength
= linebreaks
&& linebreaks
[i
] ? linebreaks
[i
].length
: 1;
140 const lineLength
= lines
[i
].length
+ linebreakLength
;
142 const matches
= re
.exec(lines
[i
]);
148 column
: matches
.index
152 column
: lineLength
- linebreakLength
156 const rangeStart
= totalLength
+ location
.start
.column
;
157 const rangeEnd
= totalLength
+ location
.end
.column
;
158 const containingNode
= sourceCode
.getNodeByRangeIndex(rangeStart
);
160 if (containingNode
&& containingNode
.type
=== "TemplateElement" &&
161 rangeStart
> containingNode
.parent
.range
[0] &&
162 rangeEnd
< containingNode
.parent
.range
[1]) {
163 totalLength
+= lineLength
;
168 * If the line has only whitespace, and skipBlankLines
169 * is true, don't report it
171 if (skipBlankLines
&& skipMatch
.test(lines
[i
])) {
172 totalLength
+= lineLength
;
176 fixRange
= [rangeStart
, rangeEnd
];
178 if (!ignoreComments
|| !commentLineNumbers
.has(lineNumber
)) {
179 report(node
, location
, fixRange
);
183 totalLength
+= lineLength
;