source: imaps-frontend/node_modules/eslint/lib/rules/no-trailing-spaces.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/**
2 * @fileoverview Disallow trailing spaces at the end of lines.
3 * @author Nodeca Team <https://github.com/nodeca>
4 * @deprecated in ESLint v8.53.0
5 */
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("./utils/ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18/** @type {import('../shared/types').Rule} */
19module.exports = {
20 meta: {
21 deprecated: true,
22 replacedBy: [],
23 type: "layout",
24
25 docs: {
26 description: "Disallow trailing whitespace at the end of lines",
27 recommended: false,
28 url: "https://eslint.org/docs/latest/rules/no-trailing-spaces"
29 },
30
31 fixable: "whitespace",
32
33 schema: [
34 {
35 type: "object",
36 properties: {
37 skipBlankLines: {
38 type: "boolean",
39 default: false
40 },
41 ignoreComments: {
42 type: "boolean",
43 default: false
44 }
45 },
46 additionalProperties: false
47 }
48 ],
49
50 messages: {
51 trailingSpace: "Trailing spaces not allowed."
52 }
53 },
54
55 create(context) {
56 const sourceCode = context.sourceCode;
57
58 const BLANK_CLASS = "[ \t\u00a0\u2000-\u200b\u3000]",
59 SKIP_BLANK = `^${BLANK_CLASS}*$`,
60 NONBLANK = `${BLANK_CLASS}+$`;
61
62 const options = context.options[0] || {},
63 skipBlankLines = options.skipBlankLines || false,
64 ignoreComments = options.ignoreComments || false;
65
66 /**
67 * Report the error message
68 * @param {ASTNode} node node to report
69 * @param {int[]} location range information
70 * @param {int[]} fixRange Range based on the whole program
71 * @returns {void}
72 */
73 function report(node, location, fixRange) {
74
75 /*
76 * Passing node is a bit dirty, because message data will contain big
77 * text in `source`. But... who cares :) ?
78 * One more kludge will not make worse the bloody wizardry of this
79 * plugin.
80 */
81 context.report({
82 node,
83 loc: location,
84 messageId: "trailingSpace",
85 fix(fixer) {
86 return fixer.removeRange(fixRange);
87 }
88 });
89 }
90
91 /**
92 * Given a list of comment nodes, return the line numbers for those comments.
93 * @param {Array} comments An array of comment nodes.
94 * @returns {number[]} An array of line numbers containing comments.
95 */
96 function getCommentLineNumbers(comments) {
97 const lines = new Set();
98
99 comments.forEach(comment => {
100 const endLine = comment.type === "Block"
101 ? comment.loc.end.line - 1
102 : comment.loc.end.line;
103
104 for (let i = comment.loc.start.line; i <= endLine; i++) {
105 lines.add(i);
106 }
107 });
108
109 return lines;
110 }
111
112 //--------------------------------------------------------------------------
113 // Public
114 //--------------------------------------------------------------------------
115
116 return {
117
118 Program: function checkTrailingSpaces(node) {
119
120 /*
121 * Let's hack. Since Espree does not return whitespace nodes,
122 * fetch the source code and do matching via regexps.
123 */
124
125 const re = new RegExp(NONBLANK, "u"),
126 skipMatch = new RegExp(SKIP_BLANK, "u"),
127 lines = sourceCode.lines,
128 linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher()),
129 comments = sourceCode.getAllComments(),
130 commentLineNumbers = getCommentLineNumbers(comments);
131
132 let totalLength = 0,
133 fixRange = [];
134
135 for (let i = 0, ii = lines.length; i < ii; i++) {
136 const lineNumber = i + 1;
137
138 /*
139 * Always add linebreak length to line length to accommodate for line break (\n or \r\n)
140 * Because during the fix time they also reserve one spot in the array.
141 * Usually linebreak length is 2 for \r\n (CRLF) and 1 for \n (LF)
142 */
143 const linebreakLength = linebreaks && linebreaks[i] ? linebreaks[i].length : 1;
144 const lineLength = lines[i].length + linebreakLength;
145
146 const matches = re.exec(lines[i]);
147
148 if (matches) {
149 const location = {
150 start: {
151 line: lineNumber,
152 column: matches.index
153 },
154 end: {
155 line: lineNumber,
156 column: lineLength - linebreakLength
157 }
158 };
159
160 const rangeStart = totalLength + location.start.column;
161 const rangeEnd = totalLength + location.end.column;
162 const containingNode = sourceCode.getNodeByRangeIndex(rangeStart);
163
164 if (containingNode && containingNode.type === "TemplateElement" &&
165 rangeStart > containingNode.parent.range[0] &&
166 rangeEnd < containingNode.parent.range[1]) {
167 totalLength += lineLength;
168 continue;
169 }
170
171 /*
172 * If the line has only whitespace, and skipBlankLines
173 * is true, don't report it
174 */
175 if (skipBlankLines && skipMatch.test(lines[i])) {
176 totalLength += lineLength;
177 continue;
178 }
179
180 fixRange = [rangeStart, rangeEnd];
181
182 if (!ignoreComments || !commentLineNumbers.has(lineNumber)) {
183 report(node, location, fixRange);
184 }
185 }
186
187 totalLength += lineLength;
188 }
189 }
190
191 };
192 }
193};
Note: See TracBrowser for help on using the repository browser.