2 * @fileoverview Rule to disalow whitespace that is not a tab or space, whitespace inside strings and comments are allowed
3 * @author Jonathan Kingston
4 * @author Christophe Porteneuve
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const astUtils = require("./utils/ast-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ALL_IRREGULARS = /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000\u2028\u2029]/u;
20 const IRREGULAR_WHITESPACE = /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000]+/mgu;
21 const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/mgu;
22 const LINE_BREAK = astUtils.createGlobalLinebreakMatcher();
24 //------------------------------------------------------------------------------
26 //------------------------------------------------------------------------------
33 description: "disallow irregular whitespace",
34 category: "Possible Errors",
36 url: "https://eslint.org/docs/rules/no-irregular-whitespace"
60 additionalProperties: false
67 // Module store of errors that we have found
70 // Lookup the `skipComments` option, which defaults to `false`.
71 const options = context.options[0] || {};
72 const skipComments = !!options.skipComments;
73 const skipStrings = options.skipStrings !== false;
74 const skipRegExps = !!options.skipRegExps;
75 const skipTemplates = !!options.skipTemplates;
77 const sourceCode = context.getSourceCode();
78 const commentNodes = sourceCode.getAllComments();
81 * Removes errors that occur inside a string node
82 * @param {ASTNode} node to check for matching errors.
86 function removeWhitespaceError(node) {
87 const locStart = node.loc.start;
88 const locEnd = node.loc.end;
90 errors = errors.filter(({ loc: errorLoc }) => {
91 if (errorLoc.line >= locStart.line && errorLoc.line <= locEnd.line) {
92 if (errorLoc.column >= locStart.column && (errorLoc.column <= locEnd.column || errorLoc.line < locEnd.line)) {
101 * Checks identifier or literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
102 * @param {ASTNode} node to check for matching errors.
106 function removeInvalidNodeErrorsInIdentifierOrLiteral(node) {
107 const shouldCheckStrings = skipStrings && (typeof node.value === "string");
108 const shouldCheckRegExps = skipRegExps && Boolean(node.regex);
110 if (shouldCheckStrings || shouldCheckRegExps) {
112 // If we have irregular characters remove them from the errors list
113 if (ALL_IRREGULARS.test(node.raw)) {
114 removeWhitespaceError(node);
120 * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
121 * @param {ASTNode} node to check for matching errors.
125 function removeInvalidNodeErrorsInTemplateLiteral(node) {
126 if (typeof node.value.raw === "string") {
127 if (ALL_IRREGULARS.test(node.value.raw)) {
128 removeWhitespaceError(node);
134 * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
135 * @param {ASTNode} node to check for matching errors.
139 function removeInvalidNodeErrorsInComment(node) {
140 if (ALL_IRREGULARS.test(node.value)) {
141 removeWhitespaceError(node);
146 * Checks the program source for irregular whitespace
147 * @param {ASTNode} node The program node
151 function checkForIrregularWhitespace(node) {
152 const sourceLines = sourceCode.lines;
154 sourceLines.forEach((sourceLine, lineIndex) => {
155 const lineNumber = lineIndex + 1;
158 while ((match = IRREGULAR_WHITESPACE.exec(sourceLine)) !== null) {
164 errors.push({ node, message: "Irregular whitespace not allowed.", loc: location });
170 * Checks the program source for irregular line terminators
171 * @param {ASTNode} node The program node
175 function checkForIrregularLineTerminators(node) {
176 const source = sourceCode.getText(),
177 sourceLines = sourceCode.lines,
178 linebreaks = source.match(LINE_BREAK);
179 let lastLineIndex = -1,
182 while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
183 const lineIndex = linebreaks.indexOf(match[0], lastLineIndex + 1) || 0;
186 column: sourceLines[lineIndex].length
189 errors.push({ node, message: "Irregular whitespace not allowed.", loc: location });
190 lastLineIndex = lineIndex;
195 * A no-op function to act as placeholder for comment accumulation when the `skipComments` option is `false`.
203 if (ALL_IRREGULARS.test(sourceCode.getText())) {
204 nodes.Program = function(node) {
207 * As we can easily fire warnings for all white space issues with
208 * all the source its simpler to fire them here.
209 * This means we can check all the application code without having
210 * to worry about issues caused in the parser tokens.
211 * When writing this code also evaluating per node was missing out
212 * connecting tokens in some cases.
213 * We can later filter the errors when they are found to be not an
214 * issue in nodes we don't care about.
216 checkForIrregularWhitespace(node);
217 checkForIrregularLineTerminators(node);
220 nodes.Identifier = removeInvalidNodeErrorsInIdentifierOrLiteral;
221 nodes.Literal = removeInvalidNodeErrorsInIdentifierOrLiteral;
222 nodes.TemplateElement = skipTemplates ? removeInvalidNodeErrorsInTemplateLiteral : noop;
223 nodes["Program:exit"] = function() {
226 // First strip errors occurring in comment nodes.
227 commentNodes.forEach(removeInvalidNodeErrorsInComment);
230 // If we have any errors remaining report on them
231 errors.forEach(error => context.report(error));
234 nodes.Program = noop;