Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 9.3KB

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