no-irregular-whitespace.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @author Yosuke Ota
  3. * @fileoverview Rule to disalow whitespace that is not a tab or space, whitespace inside strings and comments are allowed
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Constants
  12. // ------------------------------------------------------------------------------
  13. 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
  14. 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
  15. const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/mgu
  16. // ------------------------------------------------------------------------------
  17. // Rule Definition
  18. // ------------------------------------------------------------------------------
  19. module.exports = {
  20. meta: {
  21. type: 'problem',
  22. docs: {
  23. description: 'disallow irregular whitespace',
  24. category: undefined,
  25. url: 'https://eslint.vuejs.org/rules/no-irregular-whitespace.html'
  26. },
  27. schema: [
  28. {
  29. type: 'object',
  30. properties: {
  31. skipComments: {
  32. type: 'boolean',
  33. default: false
  34. },
  35. skipStrings: {
  36. type: 'boolean',
  37. default: true
  38. },
  39. skipTemplates: {
  40. type: 'boolean',
  41. default: false
  42. },
  43. skipRegExps: {
  44. type: 'boolean',
  45. default: false
  46. },
  47. skipHTMLAttributeValues: {
  48. type: 'boolean',
  49. default: false
  50. },
  51. skipHTMLTextContents: {
  52. type: 'boolean',
  53. default: false
  54. }
  55. },
  56. additionalProperties: false
  57. }
  58. ],
  59. messages: {
  60. disallow: 'Irregular whitespace not allowed.'
  61. }
  62. },
  63. create (context) {
  64. // Module store of error indexes that we have found
  65. let errorIndexes = []
  66. // Lookup the `skipComments` option, which defaults to `false`.
  67. const options = context.options[0] || {}
  68. const skipComments = !!options.skipComments
  69. const skipStrings = options.skipStrings !== false
  70. const skipRegExps = !!options.skipRegExps
  71. const skipTemplates = !!options.skipTemplates
  72. const skipHTMLAttributeValues = !!options.skipHTMLAttributeValues
  73. const skipHTMLTextContents = !!options.skipHTMLTextContents
  74. const sourceCode = context.getSourceCode()
  75. /**
  76. * Removes errors that occur inside a string node
  77. * @param {ASTNode} node to check for matching errors.
  78. * @returns {void}
  79. * @private
  80. */
  81. function removeWhitespaceError (node) {
  82. const [startIndex, endIndex] = node.range
  83. errorIndexes = errorIndexes
  84. .filter(errorIndex => errorIndex < startIndex || endIndex <= errorIndex)
  85. }
  86. /**
  87. * Checks literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  88. * @param {ASTNode} node to check for matching errors.
  89. * @returns {void}
  90. * @private
  91. */
  92. function removeInvalidNodeErrorsInLiteral (node) {
  93. const shouldCheckStrings = skipStrings && (typeof node.value === 'string')
  94. const shouldCheckRegExps = skipRegExps && Boolean(node.regex)
  95. if (shouldCheckStrings || shouldCheckRegExps) {
  96. // If we have irregular characters remove them from the errors list
  97. if (ALL_IRREGULARS.test(node.raw)) {
  98. removeWhitespaceError(node)
  99. }
  100. }
  101. }
  102. /**
  103. * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  104. * @param {ASTNode} node to check for matching errors.
  105. * @returns {void}
  106. * @private
  107. */
  108. function removeInvalidNodeErrorsInTemplateLiteral (node) {
  109. if (ALL_IRREGULARS.test(node.value.raw)) {
  110. removeWhitespaceError(node)
  111. }
  112. }
  113. /**
  114. * Checks HTML attribute value nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  115. * @param {ASTNode} node to check for matching errors.
  116. * @returns {void}
  117. * @private
  118. */
  119. function removeInvalidNodeErrorsInHTMLAttributeValue (node) {
  120. if (ALL_IRREGULARS.test(sourceCode.getText(node))) {
  121. removeWhitespaceError(node)
  122. }
  123. }
  124. /**
  125. * Checks HTML text content nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  126. * @param {ASTNode} node to check for matching errors.
  127. * @returns {void}
  128. * @private
  129. */
  130. function removeInvalidNodeErrorsInHTMLTextContent (node) {
  131. if (ALL_IRREGULARS.test(sourceCode.getText(node))) {
  132. removeWhitespaceError(node)
  133. }
  134. }
  135. /**
  136. * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  137. * @param {ASTNode} node to check for matching errors.
  138. * @returns {void}
  139. * @private
  140. */
  141. function removeInvalidNodeErrorsInComment (node) {
  142. if (ALL_IRREGULARS.test(node.value)) {
  143. removeWhitespaceError(node)
  144. }
  145. }
  146. /**
  147. * Checks the program source for irregular whitespaces and irregular line terminators
  148. * @returns {void}
  149. * @private
  150. */
  151. function checkForIrregularWhitespace () {
  152. const source = sourceCode.getText()
  153. let match
  154. while ((match = IRREGULAR_WHITESPACE.exec(source)) !== null) {
  155. errorIndexes.push(match.index)
  156. }
  157. while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
  158. errorIndexes.push(match.index)
  159. }
  160. }
  161. checkForIrregularWhitespace()
  162. if (!errorIndexes.length) {
  163. return {}
  164. }
  165. const bodyVisitor = utils.defineTemplateBodyVisitor(context,
  166. {
  167. ...(skipHTMLAttributeValues ? { 'VAttribute[directive=false] > VLiteral': removeInvalidNodeErrorsInHTMLAttributeValue } : {}),
  168. ...(skipHTMLTextContents ? { VText: removeInvalidNodeErrorsInHTMLTextContent } : {}),
  169. // inline scripts
  170. Literal: removeInvalidNodeErrorsInLiteral,
  171. ...(skipTemplates ? { TemplateElement: removeInvalidNodeErrorsInTemplateLiteral } : {})
  172. }
  173. )
  174. return {
  175. ...bodyVisitor,
  176. Literal: removeInvalidNodeErrorsInLiteral,
  177. ...(skipTemplates ? { TemplateElement: removeInvalidNodeErrorsInTemplateLiteral } : {}),
  178. 'Program:exit' (node) {
  179. if (bodyVisitor['Program:exit']) {
  180. bodyVisitor['Program:exit'](node)
  181. }
  182. const templateBody = node.templateBody
  183. if (skipComments) {
  184. // First strip errors occurring in comment nodes.
  185. sourceCode.getAllComments().forEach(removeInvalidNodeErrorsInComment)
  186. if (templateBody) {
  187. templateBody.comments.forEach(removeInvalidNodeErrorsInComment)
  188. }
  189. }
  190. // Removes errors that occur outside script and template
  191. const [scriptStart, scriptEnd] = node.range
  192. const [templateStart, templateEnd] = templateBody ? templateBody.range : [0, 0]
  193. errorIndexes = errorIndexes
  194. .filter(errorIndex =>
  195. (scriptStart <= errorIndex && errorIndex < scriptEnd) ||
  196. (templateStart <= errorIndex && errorIndex < templateEnd)
  197. )
  198. // If we have any errors remaining report on them
  199. errorIndexes.forEach(errorIndex => {
  200. context.report({
  201. loc: sourceCode.getLocFromIndex(errorIndex),
  202. messageId: 'disallow'
  203. })
  204. })
  205. }
  206. }
  207. }
  208. }