no-restricted-class.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @fileoverview Forbid certain classes from being used
  3. * @author Tao Bojlen
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const regexp = require('../utils/regexp')
  8. /**
  9. * Report a forbidden class
  10. * @param {string} className
  11. * @param {*} node
  12. * @param {RuleContext} context
  13. * @param {Set<string>} forbiddenClasses
  14. * @param {Array<RegExp>} forbiddenClassesRegexps
  15. */
  16. const reportForbiddenClass = (
  17. className,
  18. node,
  19. context,
  20. forbiddenClasses,
  21. forbiddenClassesRegexps
  22. ) => {
  23. if (
  24. forbiddenClasses.has(className) ||
  25. forbiddenClassesRegexps.some((re) => re.test(className))
  26. ) {
  27. const loc = node.value ? node.value.loc : node.loc
  28. context.report({
  29. node,
  30. loc,
  31. messageId: 'forbiddenClass',
  32. data: {
  33. class: className
  34. }
  35. })
  36. }
  37. }
  38. /**
  39. * @param {Expression} node
  40. * @param {boolean} [textOnly]
  41. * @returns {IterableIterator<{ className:string, reportNode: ESNode }>}
  42. */
  43. function* extractClassNames(node, textOnly) {
  44. if (node.type === 'Literal') {
  45. yield* `${node.value}`
  46. .split(/\s+/)
  47. .map((className) => ({ className, reportNode: node }))
  48. return
  49. }
  50. if (node.type === 'TemplateLiteral') {
  51. for (const templateElement of node.quasis) {
  52. yield* templateElement.value.cooked
  53. .split(/\s+/)
  54. .map((className) => ({ className, reportNode: templateElement }))
  55. }
  56. for (const expr of node.expressions) {
  57. yield* extractClassNames(expr, true)
  58. }
  59. return
  60. }
  61. if (node.type === 'BinaryExpression') {
  62. if (node.operator !== '+') {
  63. return
  64. }
  65. yield* extractClassNames(node.left, true)
  66. yield* extractClassNames(node.right, true)
  67. return
  68. }
  69. if (textOnly) {
  70. return
  71. }
  72. if (node.type === 'ObjectExpression') {
  73. for (const prop of node.properties) {
  74. if (prop.type !== 'Property') {
  75. continue
  76. }
  77. const classNames = utils.getStaticPropertyName(prop)
  78. if (!classNames) {
  79. continue
  80. }
  81. yield* classNames
  82. .split(/\s+/)
  83. .map((className) => ({ className, reportNode: prop.key }))
  84. }
  85. return
  86. }
  87. if (node.type === 'ArrayExpression') {
  88. for (const element of node.elements) {
  89. if (element == null) {
  90. continue
  91. }
  92. if (element.type === 'SpreadElement') {
  93. continue
  94. }
  95. yield* extractClassNames(element)
  96. }
  97. return
  98. }
  99. }
  100. module.exports = {
  101. meta: {
  102. type: 'problem',
  103. docs: {
  104. description: 'disallow specific classes in Vue components',
  105. url: 'https://eslint.vuejs.org/rules/no-restricted-class.html',
  106. categories: undefined
  107. },
  108. fixable: null,
  109. schema: {
  110. type: 'array',
  111. items: {
  112. type: 'string'
  113. }
  114. },
  115. messages: {
  116. forbiddenClass: "'{{class}}' class is not allowed."
  117. }
  118. },
  119. /** @param {RuleContext} context */
  120. create(context) {
  121. const forbiddenClasses = new Set(context.options || [])
  122. const forbiddenClassesRegexps = (context.options || [])
  123. .filter((cl) => regexp.isRegExp(cl))
  124. .map((cl) => regexp.toRegExp(cl))
  125. return utils.defineTemplateBodyVisitor(context, {
  126. /**
  127. * @param {VAttribute & { value: VLiteral } } node
  128. */
  129. 'VAttribute[directive=false][key.name="class"][value!=null]'(node) {
  130. for (const className of node.value.value.split(/\s+/)) {
  131. reportForbiddenClass(
  132. className,
  133. node,
  134. context,
  135. forbiddenClasses,
  136. forbiddenClassesRegexps
  137. )
  138. }
  139. },
  140. /** @param {VExpressionContainer} node */
  141. "VAttribute[directive=true][key.name.name='bind'][key.argument.name='class'] > VExpressionContainer.value"(
  142. node
  143. ) {
  144. if (!node.expression) {
  145. return
  146. }
  147. for (const { className, reportNode } of extractClassNames(
  148. /** @type {Expression} */ (node.expression)
  149. )) {
  150. reportForbiddenClass(
  151. className,
  152. reportNode,
  153. context,
  154. forbiddenClasses,
  155. forbiddenClassesRegexps
  156. )
  157. }
  158. }
  159. })
  160. }
  161. }