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 * @copyright 2014 Jonathan Kingston. All rights reserved.
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function(context) {
15 var irregularWhitespace = /[\u0085\u00A0\ufeff\f\v\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u202f\u205f\u3000]+/mg;
17 // Module store of errors that we have found
21 * Removes errors that occur inside a string node
22 * @param {ASTNode} node to check for matching errors.
26 function removeStringError(node) {
27 var locStart = node.loc.start;
28 var locEnd = node.loc.end;
30 errors = errors.filter(function (error) {
31 var errorLoc = error[1];
32 if (errorLoc.line >= locStart.line && errorLoc.line <= locEnd.line) {
33 if (errorLoc.column >= locStart.column && errorLoc.column <= locEnd.column) {
42 * Checks nodes for errors that we are choosing to ignore and calls the relevent methods to remove the errors
43 * @param {ASTNode} node to check for matching errors.
47 function removeInvalidNodeErrors(node) {
48 if (typeof node.value === "string") {
49 // If we have irregular characters remove them from the errors list
50 if (node.value.match(irregularWhitespace)) {
51 removeStringError(node);
57 "Program": function (node) {
59 * As we can easily fire warnings for all white space issues with all the source its simpler to fire them here
60 * This means we can check all the application code without having to worry about issues caused in the parser tokens
61 * When writing this code also evaluating per node was missing out connecting tokens in some cases
62 * We can later filter the errors when they are found to be not an issue in nodes we don't care about
64 var sourceLines = context.getSourceLines();
66 sourceLines.forEach(function (sourceLine, lineIndex) {
68 match = irregularWhitespace.exec(sourceLine);
76 errors.push([node, location, "Irregular whitespace not allowed"]);
80 "Identifier": removeInvalidNodeErrors,
81 "Literal": removeInvalidNodeErrors,
82 "Statement": removeInvalidNodeErrors,
83 "Expression": removeInvalidNodeErrors,
84 "Program:exit": function () {
86 // If we have any errors remaining report on them
87 errors.forEach(function (error) {
88 context.report.apply(this, error);