no-restricted-props.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. hasSuggestions: true,
  52. type: 'suggestion',
  53. docs: {
  54. description: 'disallow specific props',
  55. categories: undefined,
  56. url: 'https://eslint.vuejs.org/rules/no-restricted-props.html'
  57. },
  58. fixable: null,
  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.key,
  105. messageId: 'restrictedProp',
  106. data: { message },
  107. suggest: createSuggest(
  108. prop.key,
  109. option,
  110. withDefaultsProps && withDefaultsProps[prop.propName]
  111. )
  112. })
  113. break
  114. }
  115. }
  116. }
  117. }
  118. return utils.compositingVisitors(
  119. utils.defineScriptSetupVisitor(context, {
  120. onDefinePropsEnter(node, props) {
  121. processProps(props, utils.getWithDefaultsProps(node))
  122. }
  123. }),
  124. utils.defineVueVisitor(context, {
  125. onVueObjectEnter(node) {
  126. processProps(utils.getComponentPropsFromOptions(node))
  127. }
  128. })
  129. )
  130. }
  131. }
  132. /**
  133. * @param {Expression} node
  134. * @param {ParsedOption} option
  135. * @param {Property} [withDefault]
  136. * @returns {Rule.SuggestionReportDescriptor[]}
  137. */
  138. function createSuggest(node, option, withDefault) {
  139. if (!option.suggest) {
  140. return []
  141. }
  142. /** @type {string} */
  143. let replaceText
  144. if (node.type === 'Literal' || node.type === 'TemplateLiteral') {
  145. replaceText = JSON.stringify(option.suggest)
  146. } else if (node.type === 'Identifier') {
  147. replaceText = /^[a-z]\w*$/iu.exec(option.suggest)
  148. ? option.suggest
  149. : JSON.stringify(option.suggest)
  150. } else {
  151. return []
  152. }
  153. return [
  154. {
  155. fix(fixer) {
  156. const fixes = [fixer.replaceText(node, replaceText)]
  157. if (withDefault) {
  158. if (withDefault.shorthand) {
  159. fixes.push(
  160. fixer.insertTextBefore(withDefault.value, `${replaceText}:`)
  161. )
  162. } else {
  163. fixes.push(fixer.replaceText(withDefault.key, replaceText))
  164. }
  165. }
  166. return fixes.sort((a, b) => a.range[0] - b.range[0])
  167. },
  168. messageId: 'instead',
  169. data: { suggest: option.suggest }
  170. }
  171. ]
  172. }