Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-irregular-whitespace.js 8.6KB

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