no-irregular-whitespace.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. extensionRule: true,
  20. coreRuleUrl: 'https://eslint.org/docs/rules/no-irregular-whitespace'
  21. },
  22. schema: [
  23. {
  24. type: 'object',
  25. properties: {
  26. skipComments: {
  27. type: 'boolean',
  28. default: false
  29. },
  30. skipStrings: {
  31. type: 'boolean',
  32. default: true
  33. },
  34. skipTemplates: {
  35. type: 'boolean',
  36. default: false
  37. },
  38. skipRegExps: {
  39. type: 'boolean',
  40. default: false
  41. },
  42. skipHTMLAttributeValues: {
  43. type: 'boolean',
  44. default: false
  45. },
  46. skipHTMLTextContents: {
  47. type: 'boolean',
  48. default: false
  49. }
  50. },
  51. additionalProperties: false
  52. }
  53. ],
  54. messages: {
  55. disallow: 'Irregular whitespace not allowed.'
  56. }
  57. },
  58. /**
  59. * @param {RuleContext} context - The rule context.
  60. * @returns {RuleListener} AST event handlers.
  61. */
  62. create(context) {
  63. // Module store of error indexes that we have found
  64. /** @type {number[]} */
  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 | Token} 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.filter(
  84. (errorIndex) => errorIndex < startIndex || endIndex <= errorIndex
  85. )
  86. }
  87. /**
  88. * Checks literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  89. * @param {Literal} node to check for matching errors.
  90. * @returns {void}
  91. * @private
  92. */
  93. function removeInvalidNodeErrorsInLiteral(node) {
  94. const shouldCheckStrings = skipStrings && typeof node.value === 'string'
  95. const shouldCheckRegExps = skipRegExps && Boolean(node.regex)
  96. // If we have irregular characters, remove them from the errors list
  97. if (
  98. (shouldCheckStrings || shouldCheckRegExps) &&
  99. ALL_IRREGULARS.test(sourceCode.getText(node))
  100. ) {
  101. removeWhitespaceError(node)
  102. }
  103. }
  104. /**
  105. * Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  106. * @param {TemplateElement} node to check for matching errors.
  107. * @returns {void}
  108. * @private
  109. */
  110. function removeInvalidNodeErrorsInTemplateLiteral(node) {
  111. if (ALL_IRREGULARS.test(node.value.raw)) {
  112. removeWhitespaceError(node)
  113. }
  114. }
  115. /**
  116. * Checks HTML attribute value nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  117. * @param {VLiteral} node to check for matching errors.
  118. * @returns {void}
  119. * @private
  120. */
  121. function removeInvalidNodeErrorsInHTMLAttributeValue(node) {
  122. if (ALL_IRREGULARS.test(sourceCode.getText(node))) {
  123. removeWhitespaceError(node)
  124. }
  125. }
  126. /**
  127. * Checks HTML text content nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  128. * @param {VText} node to check for matching errors.
  129. * @returns {void}
  130. * @private
  131. */
  132. function removeInvalidNodeErrorsInHTMLTextContent(node) {
  133. if (ALL_IRREGULARS.test(sourceCode.getText(node))) {
  134. removeWhitespaceError(node)
  135. }
  136. }
  137. /**
  138. * Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
  139. * @param {Comment | HTMLComment | HTMLBogusComment} node to check for matching errors.
  140. * @returns {void}
  141. * @private
  142. */
  143. function removeInvalidNodeErrorsInComment(node) {
  144. if (ALL_IRREGULARS.test(node.value)) {
  145. removeWhitespaceError(node)
  146. }
  147. }
  148. /**
  149. * Checks the program source for irregular whitespaces and irregular line terminators
  150. * @returns {void}
  151. * @private
  152. */
  153. function checkForIrregularWhitespace() {
  154. const source = sourceCode.getText()
  155. let match
  156. while ((match = IRREGULAR_WHITESPACE.exec(source)) !== null) {
  157. errorIndexes.push(match.index)
  158. }
  159. while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
  160. errorIndexes.push(match.index)
  161. }
  162. }
  163. checkForIrregularWhitespace()
  164. if (errorIndexes.length === 0) {
  165. return {}
  166. }
  167. const bodyVisitor = utils.defineTemplateBodyVisitor(context, {
  168. ...(skipHTMLAttributeValues
  169. ? {
  170. 'VAttribute[directive=false] > VLiteral':
  171. removeInvalidNodeErrorsInHTMLAttributeValue
  172. }
  173. : {}),
  174. ...(skipHTMLTextContents
  175. ? { VText: removeInvalidNodeErrorsInHTMLTextContent }
  176. : {}),
  177. // inline scripts
  178. Literal: removeInvalidNodeErrorsInLiteral,
  179. ...(skipTemplates
  180. ? { TemplateElement: removeInvalidNodeErrorsInTemplateLiteral }
  181. : {})
  182. })
  183. return {
  184. ...bodyVisitor,
  185. Literal: removeInvalidNodeErrorsInLiteral,
  186. ...(skipTemplates
  187. ? { TemplateElement: removeInvalidNodeErrorsInTemplateLiteral }
  188. : {}),
  189. 'Program:exit'(node) {
  190. if (bodyVisitor['Program:exit']) {
  191. bodyVisitor['Program:exit'](node)
  192. }
  193. const templateBody = node.templateBody
  194. if (skipComments) {
  195. // First strip errors occurring in comment nodes.
  196. for (const node of sourceCode.getAllComments()) {
  197. removeInvalidNodeErrorsInComment(node)
  198. }
  199. if (templateBody) {
  200. for (const node of templateBody.comments) {
  201. removeInvalidNodeErrorsInComment(node)
  202. }
  203. }
  204. }
  205. // Removes errors that occur outside script and template
  206. const [scriptStart, scriptEnd] = node.range
  207. const [templateStart, templateEnd] = templateBody
  208. ? templateBody.range
  209. : [0, 0]
  210. errorIndexes = errorIndexes.filter(
  211. (errorIndex) =>
  212. (scriptStart <= errorIndex && errorIndex < scriptEnd) ||
  213. (templateStart <= errorIndex && errorIndex < templateEnd)
  214. )
  215. // If we have any errors remaining, report on them
  216. for (const errorIndex of errorIndexes) {
  217. context.report({
  218. loc: sourceCode.getLocFromIndex(errorIndex),
  219. messageId: 'disallow'
  220. })
  221. }
  222. }
  223. }
  224. }
  225. }