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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. type: 'suggestion',
  11. docs: {
  12. description: 'disallow a potential typo in your component property',
  13. categories: undefined,
  14. recommended: false,
  15. url: 'https://eslint.vuejs.org/rules/no-potential-component-option-typo.html'
  16. },
  17. fixable: null,
  18. hasSuggestions: true,
  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. messages: {
  47. potentialTypo: `'{{name}}' may be a typo, which is similar to option [{{option}}].`
  48. }
  49. },
  50. /** @param {RuleContext} context */
  51. create(context) {
  52. const option = context.options[0] || {}
  53. const custom = option.custom || []
  54. /** @type {('all' | 'vue' | 'vue-router' | 'nuxt')[]} */
  55. const presets = option.presets || ['vue']
  56. const threshold = option.threshold || 1
  57. /** @type {Set<string>} */
  58. const candidateOptionSet = new Set(custom)
  59. for (const preset of presets) {
  60. if (preset === 'all') {
  61. for (const opts of Object.values(vueComponentOptions)) {
  62. for (const opt of opts) {
  63. candidateOptionSet.add(opt)
  64. }
  65. }
  66. } else {
  67. for (const opt of vueComponentOptions[preset]) {
  68. candidateOptionSet.add(opt)
  69. }
  70. }
  71. }
  72. const candidateOptionList = [...candidateOptionSet]
  73. if (candidateOptionList.length === 0) {
  74. return {}
  75. }
  76. return utils.executeOnVue(context, (obj) => {
  77. const componentInstanceOptions = obj.properties
  78. .map((p) => {
  79. if (p.type === 'Property') {
  80. const name = utils.getStaticPropertyName(p)
  81. if (name != null) {
  82. return {
  83. name,
  84. key: p.key
  85. }
  86. }
  87. }
  88. return null
  89. })
  90. .filter(utils.isDef)
  91. if (componentInstanceOptions.length === 0) {
  92. return
  93. }
  94. for (const option of componentInstanceOptions) {
  95. const id = option.key
  96. const name = option.name
  97. if (candidateOptionSet.has(name)) {
  98. continue
  99. }
  100. const potentialTypoList = candidateOptionList
  101. .map((o) => ({ option: o, distance: utils.editDistance(o, name) }))
  102. .filter(({ distance }) => distance <= threshold && distance > 0)
  103. .sort((a, b) => a.distance - b.distance)
  104. if (potentialTypoList.length > 0) {
  105. context.report({
  106. node: id,
  107. messageId: 'potentialTypo',
  108. data: {
  109. name,
  110. option: potentialTypoList.map(({ option }) => option).join(',')
  111. },
  112. suggest: potentialTypoList.map(({ option }) => ({
  113. desc: `Replace property '${name}' to '${option}'`,
  114. fix(fixer) {
  115. return fixer.replaceText(id, option)
  116. }
  117. }))
  118. })
  119. }
  120. }
  121. })
  122. }
  123. }