no-irregular-whitespace.js 7.4 KB

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