项目原始demo,不改动
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

238 lines
8.6 KiB

  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("../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. docs: {
  24. description: "disallow irregular whitespace outside of strings and comments",
  25. category: "Possible Errors",
  26. recommended: true,
  27. url: "https://eslint.org/docs/rules/no-irregular-whitespace"
  28. },
  29. schema: [
  30. {
  31. type: "object",
  32. properties: {
  33. skipComments: {
  34. type: "boolean"
  35. },
  36. skipStrings: {
  37. type: "boolean"
  38. },
  39. skipTemplates: {
  40. type: "boolean"
  41. },
  42. skipRegExps: {
  43. type: "boolean"
  44. }
  45. },
  46. additionalProperties: false
  47. }
  48. ]
  49. },
  50. create(context) {
  51. // Module store of errors that we have found
  52. let errors = [];
  53. // Lookup the `skipComments` option, which defaults to `false`.
  54. const options = context.options[0] || {};
  55. const skipComments = !!options.skipComments;
  56. const skipStrings = options.skipStrings !== false;
  57. const skipRegExps = !!options.skipRegExps;
  58. const skipTemplates = !!options.skipTemplates;
  59. const sourceCode = context.getSourceCode();
  60. const commentNodes = sourceCode.getAllComments();
  61. /**
  62. * Removes errors that occur inside a string node
  63. * @param {ASTNode} node to check for matching errors.
  64. * @returns {void}
  65. * @private
  66. */
  67. function removeWhitespaceError(node) {
  68. const locStart = node.loc.start;
  69. const locEnd = node.loc.end;
  70. errors = errors.filter(error => {
  71. const errorLoc = error[1];
  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, location, "Irregular whitespace not allowed."]);
  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, location, "Irregular whitespace not allowed."]);
  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 => {
  194. context.report.apply(context, error);
  195. });
  196. };
  197. } else {
  198. nodes.Program = noop;
  199. }
  200. return nodes;
  201. }
  202. };