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