no-potential-component-option-typo.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @fileoverview detect if there is a potential typo in your component property
  3. * @author IWANABETHATGUY
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const vueComponentOptions = require('../utils/vue-component-options.json')
  8. module.exports = {
  9. meta: {
  10. hasSuggestions: true,
  11. type: 'suggestion',
  12. docs: {
  13. description: 'disallow a potential typo in your component property',
  14. categories: undefined,
  15. recommended: false,
  16. url: 'https://eslint.vuejs.org/rules/no-potential-component-option-typo.html'
  17. },
  18. fixable: null,
  19. schema: [
  20. {
  21. type: 'object',
  22. properties: {
  23. presets: {
  24. type: 'array',
  25. items: {
  26. type: 'string',
  27. enum: ['all', 'vue', 'vue-router', 'nuxt']
  28. },
  29. uniqueItems: true,
  30. minItems: 0
  31. },
  32. custom: {
  33. type: 'array',
  34. minItems: 0,
  35. items: { type: 'string' },
  36. uniqueItems: true
  37. },
  38. threshold: {
  39. type: 'number',
  40. minimum: 1
  41. }
  42. },
  43. additionalProperties: false
  44. }
  45. ]
  46. },
  47. /** @param {RuleContext} context */
  48. create(context) {
  49. const option = context.options[0] || {}
  50. const custom = option.custom || []
  51. /** @type {('all' | 'vue' | 'vue-router' | 'nuxt')[]} */
  52. const presets = option.presets || ['vue']
  53. const threshold = option.threshold || 1
  54. /** @type {Set<string>} */
  55. const candidateOptionSet = new Set(custom)
  56. for (const preset of presets) {
  57. if (preset === 'all') {
  58. for (const opts of Object.values(vueComponentOptions)) {
  59. for (const opt of opts) {
  60. candidateOptionSet.add(opt)
  61. }
  62. }
  63. } else {
  64. for (const opt of vueComponentOptions[preset]) {
  65. candidateOptionSet.add(opt)
  66. }
  67. }
  68. }
  69. const candidateOptionList = [...candidateOptionSet]
  70. if (candidateOptionList.length === 0) {
  71. return {}
  72. }
  73. return utils.executeOnVue(context, (obj) => {
  74. const componentInstanceOptions = obj.properties
  75. .map((p) => {
  76. if (p.type === 'Property') {
  77. const name = utils.getStaticPropertyName(p)
  78. if (name != null) {
  79. return {
  80. name,
  81. key: p.key
  82. }
  83. }
  84. }
  85. return null
  86. })
  87. .filter(utils.isDef)
  88. if (componentInstanceOptions.length === 0) {
  89. return
  90. }
  91. for (const option of componentInstanceOptions) {
  92. const id = option.key
  93. const name = option.name
  94. if (candidateOptionSet.has(name)) {
  95. continue
  96. }
  97. const potentialTypoList = candidateOptionList
  98. .map((o) => ({ option: o, distance: utils.editDistance(o, name) }))
  99. .filter(({ distance }) => distance <= threshold && distance > 0)
  100. .sort((a, b) => a.distance - b.distance)
  101. if (potentialTypoList.length > 0) {
  102. context.report({
  103. node: id,
  104. message: `'{{name}}' may be a typo, which is similar to option [{{option}}].`,
  105. data: {
  106. name,
  107. option: potentialTypoList.map(({ option }) => option).join(',')
  108. },
  109. suggest: potentialTypoList.map(({ option }) => ({
  110. desc: `Replace property '${name}' to '${option}'`,
  111. fix(fixer) {
  112. return fixer.replaceText(id, option)
  113. }
  114. }))
  115. })
  116. }
  117. }
  118. })
  119. }
  120. }