2 * @fileoverview Rule to disallow 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 //------------------------------------------------------------------------------
28 /** @type {import('../shared/types').Rule} */
34 description
: "Disallow irregular whitespace",
36 url
: "https://eslint.org/docs/rules/no-irregular-whitespace"
60 additionalProperties
: false
65 noIrregularWhitespace
: "Irregular whitespace not allowed."
71 // Module store of errors that we have found
74 // Lookup the `skipComments` option, which defaults to `false`.
75 const options
= context
.options
[0] || {};
76 const skipComments
= !!options
.skipComments
;
77 const skipStrings
= options
.skipStrings
!== false;
78 const skipRegExps
= !!options
.skipRegExps
;
79 const skipTemplates
= !!options
.skipTemplates
;
81 const sourceCode
= context
.getSourceCode();
82 const commentNodes
= sourceCode
.getAllComments();
85 * Removes errors that occur inside the given node
86 * @param {ASTNode} node to check for matching errors.
90 function removeWhitespaceError(node
) {
91 const locStart
= node
.loc
.start
;
92 const locEnd
= node
.loc
.end
;
94 errors
= errors
.filter(({ loc
: { start
: errorLocStart
} }) => (
95 errorLocStart
.line
< locStart
.line
||
96 errorLocStart
.line
=== locStart
.line
&& errorLocStart
.column
< locStart
.column
||
97 errorLocStart
.line
=== locEnd
.line
&& errorLocStart
.column
>= locEnd
.column
||
98 errorLocStart
.line
> locEnd
.line
103 * Checks identifier or literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
104 * @param {ASTNode} node to check for matching errors.
108 function removeInvalidNodeErrorsInIdentifierOrLiteral(node
) {
109 const shouldCheckStrings
= skipStrings
&& (typeof node
.value
=== "string");
110 const shouldCheckRegExps
= skipRegExps
&& Boolean(node
.regex
);
112 if (shouldCheckStrings
|| shouldCheckRegExps
) {
114 // If we have irregular characters remove them from the errors list
115 if (ALL_IRREGULARS
.test(node
.raw
)) {
116 removeWhitespaceError(node
);
122 * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
123 * @param {ASTNode} node to check for matching errors.
127 function removeInvalidNodeErrorsInTemplateLiteral(node
) {
128 if (typeof node
.value
.raw
=== "string") {
129 if (ALL_IRREGULARS
.test(node
.value
.raw
)) {
130 removeWhitespaceError(node
);
136 * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
137 * @param {ASTNode} node to check for matching errors.
141 function removeInvalidNodeErrorsInComment(node
) {
142 if (ALL_IRREGULARS
.test(node
.value
)) {
143 removeWhitespaceError(node
);
148 * Checks the program source for irregular whitespace
149 * @param {ASTNode} node The program node
153 function checkForIrregularWhitespace(node
) {
154 const sourceLines
= sourceCode
.lines
;
156 sourceLines
.forEach((sourceLine
, lineIndex
) => {
157 const lineNumber
= lineIndex
+ 1;
160 while ((match
= IRREGULAR_WHITESPACE
.exec(sourceLine
)) !== null) {
163 messageId
: "noIrregularWhitespace",
171 column
: match
.index
+ match
[0].length
180 * Checks the program source for irregular line terminators
181 * @param {ASTNode} node The program node
185 function checkForIrregularLineTerminators(node
) {
186 const source
= sourceCode
.getText(),
187 sourceLines
= sourceCode
.lines
,
188 linebreaks
= source
.match(LINE_BREAK
);
189 let lastLineIndex
= -1,
192 while ((match
= IRREGULAR_LINE_TERMINATORS
.exec(source
)) !== null) {
193 const lineIndex
= linebreaks
.indexOf(match
[0], lastLineIndex
+ 1) || 0;
197 messageId
: "noIrregularWhitespace",
201 column
: sourceLines
[lineIndex
].length
210 lastLineIndex
= lineIndex
;
215 * A no-op function to act as placeholder for comment accumulation when the `skipComments` option is `false`.
223 if (ALL_IRREGULARS
.test(sourceCode
.getText())) {
224 nodes
.Program = function(node
) {
227 * As we can easily fire warnings for all white space issues with
228 * all the source its simpler to fire them here.
229 * This means we can check all the application code without having
230 * to worry about issues caused in the parser tokens.
231 * When writing this code also evaluating per node was missing out
232 * connecting tokens in some cases.
233 * We can later filter the errors when they are found to be not an
234 * issue in nodes we don't care about.
236 checkForIrregularWhitespace(node
);
237 checkForIrregularLineTerminators(node
);
240 nodes
.Identifier
= removeInvalidNodeErrorsInIdentifierOrLiteral
;
241 nodes
.Literal
= removeInvalidNodeErrorsInIdentifierOrLiteral
;
242 nodes
.TemplateElement
= skipTemplates
? removeInvalidNodeErrorsInTemplateLiteral
: noop
;
243 nodes
["Program:exit"] = function() {
246 // First strip errors occurring in comment nodes.
247 commentNodes
.forEach(removeInvalidNodeErrorsInComment
);
250 // If we have any errors remaining report on them
251 errors
.forEach(error
=> context
.report(error
));
254 nodes
.Program
= noop
;