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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. hasSuggestions: true,
  14. type: 'suggestion',
  15. docs: {
  16. description: 'disallow a potential typo in your component property',
  17. categories: undefined,
  18. recommended: false,
  19. url: 'https://eslint.vuejs.org/rules/no-potential-component-option-typo.html'
  20. },
  21. fixable: null,
  22. schema: [
  23. {
  24. type: 'object',
  25. properties: {
  26. presets: {
  27. type: 'array',
  28. items: {
  29. type: 'string',
  30. enum: ['all', 'vue', 'vue-router', 'nuxt']
  31. },
  32. uniqueItems: true,
  33. minItems: 0
  34. },
  35. custom: {
  36. type: 'array',
  37. minItems: 0,
  38. items: { type: 'string' },
  39. uniqueItems: true
  40. },
  41. threshold: {
  42. type: 'number',
  43. minimum: 1
  44. }
  45. },
  46. additionalProperties: false
  47. }
  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) {
  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) {
  92. return
  93. }
  94. componentInstanceOptions.forEach((option) => {
  95. const id = option.key
  96. const name = option.name
  97. if (candidateOptionSet.has(name)) {
  98. return
  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) {
  105. context.report({
  106. node: id,
  107. message: `'{{name}}' may be a typo, which is similar to option [{{option}}].`,
  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. }