1/** 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 5 */ 6 7"use strict"; 8 9//------------------------------------------------------------------------------ 10// Requirements 11//------------------------------------------------------------------------------ 12 13const astUtils = require("./utils/ast-utils"); 14 15//------------------------------------------------------------------------------ 16// Constants 17//------------------------------------------------------------------------------ 18 19const 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; 20const 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; 21const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/mgu; 22const LINE_BREAK = astUtils.createGlobalLinebreakMatcher(); 23 24//------------------------------------------------------------------------------ 25// Rule Definition 26//------------------------------------------------------------------------------ 27 28module.exports = { 29 meta: { 30 type: "problem", 31 32 docs: { 33 description: "disallow irregular whitespace", 34 category: "Possible Errors", 35 recommended: true, 36 url: "https://eslint.org/docs/rules/no-irregular-whitespace" 37 }, 38 39 schema: [ 40 { 41 type: "object", 42 properties: { 43 skipComments: { 44 type: "boolean", 45 default: false 46 }, 47 skipStrings: { 48 type: "boolean", 49 default: true 50 }, 51 skipTemplates: { 52 type: "boolean", 53 default: false 54 }, 55 skipRegExps: { 56 type: "boolean", 57 default: false 58 } 59 }, 60 additionalProperties: false 61 } 62 ], 63 64 messages: { 65 noIrregularWhitespace: "Irregular whitespace not allowed." 66 } 67 }, 68 69 create(context) { 70 71 // Module store of errors that we have found 72 let errors = []; 73 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; 80 81 const sourceCode = context.getSourceCode(); 82 const commentNodes = sourceCode.getAllComments(); 83 84 /** 85 * Removes errors that occur inside a string node 86 * @param {ASTNode} node to check for matching errors. 87 * @returns {void} 88 * @private 89 */ 90 function removeWhitespaceError(node) { 91 const locStart = node.loc.start; 92 const locEnd = node.loc.end; 93 94 errors = errors.filter(({ loc: { start: errorLoc } }) => { 95 if (errorLoc.line >= locStart.line && errorLoc.line <= locEnd.line) { 96 if (errorLoc.column >= locStart.column && (errorLoc.column <= locEnd.column || errorLoc.line < locEnd.line)) { 97 return false; 98 } 99 } 100 return true; 101 }); 102 } 103 104 /** 105 * Checks identifier or literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors 106 * @param {ASTNode} node to check for matching errors. 107 * @returns {void} 108 * @private 109 */ 110 function removeInvalidNodeErrorsInIdentifierOrLiteral(node) { 111 const shouldCheckStrings = skipStrings && (typeof node.value === "string"); 112 const shouldCheckRegExps = skipRegExps && Boolean(node.regex); 113 114 if (shouldCheckStrings || shouldCheckRegExps) { 115 116 // If we have irregular characters remove them from the errors list 117 if (ALL_IRREGULARS.test(node.raw)) { 118 removeWhitespaceError(node); 119 } 120 } 121 } 122 123 /** 124 * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors 125 * @param {ASTNode} node to check for matching errors. 126 * @returns {void} 127 * @private 128 */ 129 function removeInvalidNodeErrorsInTemplateLiteral(node) { 130 if (typeof node.value.raw === "string") { 131 if (ALL_IRREGULARS.test(node.value.raw)) { 132 removeWhitespaceError(node); 133 } 134 } 135 } 136 137 /** 138 * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors 139 * @param {ASTNode} node to check for matching errors. 140 * @returns {void} 141 * @private 142 */ 143 function removeInvalidNodeErrorsInComment(node) { 144 if (ALL_IRREGULARS.test(node.value)) { 145 removeWhitespaceError(node); 146 } 147 } 148 149 /** 150 * Checks the program source for irregular whitespace 151 * @param {ASTNode} node The program node 152 * @returns {void} 153 * @private 154 */ 155 function checkForIrregularWhitespace(node) { 156 const sourceLines = sourceCode.lines; 157 158 sourceLines.forEach((sourceLine, lineIndex) => { 159 const lineNumber = lineIndex + 1; 160 let match; 161 162 while ((match = IRREGULAR_WHITESPACE.exec(sourceLine)) !== null) { 163 errors.push({ 164 node, 165 messageId: "noIrregularWhitespace", 166 loc: { 167 start: { 168 line: lineNumber, 169 column: match.index 170 }, 171 end: { 172 line: lineNumber, 173 column: match.index + match[0].length 174 } 175 } 176 }); 177 } 178 }); 179 } 180 181 /** 182 * Checks the program source for irregular line terminators 183 * @param {ASTNode} node The program node 184 * @returns {void} 185 * @private 186 */ 187 function checkForIrregularLineTerminators(node) { 188 const source = sourceCode.getText(), 189 sourceLines = sourceCode.lines, 190 linebreaks = source.match(LINE_BREAK); 191 let lastLineIndex = -1, 192 match; 193 194 while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) { 195 const lineIndex = linebreaks.indexOf(match[0], lastLineIndex + 1) || 0; 196 197 errors.push({ 198 node, 199 messageId: "noIrregularWhitespace", 200 loc: { 201 start: { 202 line: lineIndex + 1, 203 column: sourceLines[lineIndex].length 204 }, 205 end: { 206 line: lineIndex + 2, 207 column: 0 208 } 209 } 210 }); 211 212 lastLineIndex = lineIndex; 213 } 214 } 215 216 /** 217 * A no-op function to act as placeholder for comment accumulation when the `skipComments` option is `false`. 218 * @returns {void} 219 * @private 220 */ 221 function noop() {} 222 223 const nodes = {}; 224 225 if (ALL_IRREGULARS.test(sourceCode.getText())) { 226 nodes.Program = function(node) { 227 228 /* 229 * As we can easily fire warnings for all white space issues with 230 * all the source its simpler to fire them here. 231 * This means we can check all the application code without having 232 * to worry about issues caused in the parser tokens. 233 * When writing this code also evaluating per node was missing out 234 * connecting tokens in some cases. 235 * We can later filter the errors when they are found to be not an 236 * issue in nodes we don't care about. 237 */ 238 checkForIrregularWhitespace(node); 239 checkForIrregularLineTerminators(node); 240 }; 241 242 nodes.Identifier = removeInvalidNodeErrorsInIdentifierOrLiteral; 243 nodes.Literal = removeInvalidNodeErrorsInIdentifierOrLiteral; 244 nodes.TemplateElement = skipTemplates ? removeInvalidNodeErrorsInTemplateLiteral : noop; 245 nodes["Program:exit"] = function() { 246 if (skipComments) { 247 248 // First strip errors occurring in comment nodes. 249 commentNodes.forEach(removeInvalidNodeErrorsInComment); 250 } 251 252 // If we have any errors remaining report on them 253 errors.forEach(error => context.report(error)); 254 }; 255 } else { 256 nodes.Program = noop; 257 } 258 259 return nodes; 260 } 261}; 262