no-restricted-props.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const regexp = require('../utils/regexp')
  8. /**
  9. * @typedef {import('../utils').ComponentProp} ComponentProp
  10. */
  11. /**
  12. * @typedef {object} ParsedOption
  13. * @property { (name: string) => boolean } test
  14. * @property {string|undefined} [message]
  15. * @property {string|undefined} [suggest]
  16. */
  17. /**
  18. * @param {string} str
  19. * @returns {(str: string) => boolean}
  20. */
  21. function buildMatcher(str) {
  22. if (regexp.isRegExp(str)) {
  23. const re = regexp.toRegExp(str)
  24. return (s) => {
  25. re.lastIndex = 0
  26. return re.test(s)
  27. }
  28. }
  29. return (s) => s === str
  30. }
  31. /**
  32. * @param {string|{name:string, message?: string, suggest?:string}} option
  33. * @returns {ParsedOption}
  34. */
  35. function parseOption(option) {
  36. if (typeof option === 'string') {
  37. const matcher = buildMatcher(option)
  38. return {
  39. test(name) {
  40. return matcher(name)
  41. }
  42. }
  43. }
  44. const parsed = parseOption(option.name)
  45. parsed.message = option.message
  46. parsed.suggest = option.suggest
  47. return parsed
  48. }
  49. module.exports = {
  50. meta: {
  51. type: 'suggestion',
  52. docs: {
  53. description: 'disallow specific props',
  54. categories: undefined,
  55. url: 'https://eslint.vuejs.org/rules/no-restricted-props.html'
  56. },
  57. fixable: null,
  58. hasSuggestions: true,
  59. schema: {
  60. type: 'array',
  61. items: {
  62. oneOf: [
  63. { type: ['string'] },
  64. {
  65. type: 'object',
  66. properties: {
  67. name: { type: 'string' },
  68. message: { type: 'string', minLength: 1 },
  69. suggest: { type: 'string' }
  70. },
  71. required: ['name'],
  72. additionalProperties: false
  73. }
  74. ]
  75. },
  76. uniqueItems: true,
  77. minItems: 0
  78. },
  79. messages: {
  80. // eslint-disable-next-line eslint-plugin/report-message-format
  81. restrictedProp: '{{message}}',
  82. instead: 'Instead, change to `{{suggest}}`.'
  83. }
  84. },
  85. /** @param {RuleContext} context */
  86. create(context) {
  87. /** @type {ParsedOption[]} */
  88. const options = context.options.map(parseOption)
  89. /**
  90. * @param {ComponentProp[]} props
  91. * @param { { [key: string]: Property | undefined } } [withDefaultsProps]
  92. */
  93. function processProps(props, withDefaultsProps) {
  94. for (const prop of props) {
  95. if (!prop.propName) {
  96. continue
  97. }
  98. for (const option of options) {
  99. if (option.test(prop.propName)) {
  100. const message =
  101. option.message ||
  102. `Using \`${prop.propName}\` props is not allowed.`
  103. context.report({
  104. node: prop.type === 'infer-type' ? prop.node : prop.key,
  105. messageId: 'restrictedProp',
  106. data: { message },
  107. suggest:
  108. prop.type === 'infer-type'
  109. ? null
  110. : createSuggest(
  111. prop.key,
  112. option,
  113. withDefaultsProps && withDefaultsProps[prop.propName]
  114. )
  115. })
  116. break
  117. }
  118. }
  119. }
  120. }
  121. return utils.compositingVisitors(
  122. utils.defineScriptSetupVisitor(context, {
  123. onDefinePropsEnter(node, props) {
  124. processProps(props, utils.getWithDefaultsProps(node))
  125. }
  126. }),
  127. utils.defineVueVisitor(context, {
  128. onVueObjectEnter(node) {
  129. processProps(utils.getComponentPropsFromOptions(node))
  130. }
  131. })
  132. )
  133. }
  134. }
  135. /**
  136. * @param {Expression} node
  137. * @param {ParsedOption} option
  138. * @param {Property} [withDefault]
  139. * @returns {Rule.SuggestionReportDescriptor[]}
  140. */
  141. function createSuggest(node, option, withDefault) {
  142. if (!option.suggest) {
  143. return []
  144. }
  145. /** @type {string} */
  146. let replaceText
  147. if (node.type === 'Literal' || node.type === 'TemplateLiteral') {
  148. replaceText = JSON.stringify(option.suggest)
  149. } else if (node.type === 'Identifier') {
  150. replaceText = /^[a-z]\w*$/iu.test(option.suggest)
  151. ? option.suggest
  152. : JSON.stringify(option.suggest)
  153. } else {
  154. return []
  155. }
  156. return [
  157. {
  158. fix(fixer) {
  159. const fixes = [fixer.replaceText(node, replaceText)]
  160. if (withDefault) {
  161. if (withDefault.shorthand) {
  162. fixes.push(
  163. fixer.insertTextBefore(withDefault.value, `${replaceText}:`)
  164. )
  165. } else {
  166. fixes.push(fixer.replaceText(withDefault.key, replaceText))
  167. }
  168. }
  169. return fixes.sort((a, b) => a.range[0] - b.range[0])
  170. },
  171. messageId: 'instead',
  172. data: { suggest: option.suggest }
  173. }
  174. ]
  175. }