no-reserved-props.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 casing = require('../utils/casing')
  8. /**
  9. * @typedef {import('../utils').ComponentProp} ComponentProp
  10. */
  11. const RESERVED = {
  12. 3: ['key', 'ref'],
  13. 2: ['key', 'ref', 'is', 'slot', 'slot-scope', 'slotScope', 'class', 'style']
  14. }
  15. module.exports = {
  16. meta: {
  17. type: 'problem',
  18. docs: {
  19. description: 'disallow reserved names in props',
  20. categories: ['vue3-essential', 'vue2-essential'],
  21. url: 'https://eslint.vuejs.org/rules/no-reserved-props.html',
  22. defaultOptions: {
  23. vue2: [{ vueVersion: 2 }]
  24. }
  25. },
  26. fixable: null,
  27. schema: [
  28. {
  29. type: 'object',
  30. properties: {
  31. vueVersion: {
  32. enum: [2, 3]
  33. }
  34. },
  35. additionalProperties: false
  36. }
  37. ],
  38. messages: {
  39. reserved:
  40. "'{{propName}}' is a reserved attribute and cannot be used as props."
  41. }
  42. },
  43. /** @param {RuleContext} context */
  44. create(context) {
  45. const options = context.options[0] || {}
  46. /** @type {2|3} */
  47. const vueVersion = options.vueVersion || 3
  48. const reserved = new Set(RESERVED[vueVersion])
  49. /**
  50. * @param {ComponentProp[]} props
  51. */
  52. function processProps(props) {
  53. for (const prop of props) {
  54. if (prop.propName != null && reserved.has(prop.propName)) {
  55. context.report({
  56. node: prop.node,
  57. messageId: `reserved`,
  58. data: {
  59. propName: casing.kebabCase(prop.propName)
  60. }
  61. })
  62. }
  63. }
  64. }
  65. return utils.compositingVisitors(
  66. utils.defineScriptSetupVisitor(context, {
  67. onDefinePropsEnter(_node, props) {
  68. processProps(props)
  69. }
  70. }),
  71. utils.executeOnVue(context, (obj) => {
  72. processProps(utils.getComponentPropsFromOptions(obj))
  73. })
  74. )
  75. }
  76. }