prop-name-casing.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @fileoverview Requires specific casing for the Prop name in Vue components
  3. * @author Yu Kimura
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const casing = require('../utils/casing')
  8. const allowedCaseOptions = ['camelCase', 'snake_case']
  9. /**
  10. * @typedef {import('../utils').ComponentProp} ComponentProp
  11. */
  12. /** @param {RuleContext} context */
  13. function create(context) {
  14. const options = context.options[0]
  15. const caseType = allowedCaseOptions.includes(options) ? options : 'camelCase'
  16. const checker = casing.getChecker(caseType)
  17. /**
  18. * @param {ComponentProp[]} props
  19. */
  20. function processProps(props) {
  21. for (const item of props) {
  22. const propName = item.propName
  23. if (propName == null) {
  24. continue
  25. }
  26. if (!checker(propName)) {
  27. context.report({
  28. node: item.node,
  29. messageId: 'invalidCase',
  30. data: {
  31. name: propName,
  32. caseType
  33. }
  34. })
  35. }
  36. }
  37. }
  38. return utils.compositingVisitors(
  39. utils.defineScriptSetupVisitor(context, {
  40. onDefinePropsEnter(_node, props) {
  41. processProps(props)
  42. }
  43. }),
  44. utils.executeOnVue(context, (obj) => {
  45. processProps(utils.getComponentPropsFromOptions(obj))
  46. })
  47. )
  48. }
  49. module.exports = {
  50. meta: {
  51. type: 'suggestion',
  52. docs: {
  53. description:
  54. 'enforce specific casing for the Prop name in Vue components',
  55. categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
  56. url: 'https://eslint.vuejs.org/rules/prop-name-casing.html'
  57. },
  58. fixable: null, // null or "code" or "whitespace"
  59. schema: [
  60. {
  61. enum: allowedCaseOptions
  62. }
  63. ],
  64. messages: {
  65. invalidCase: 'Prop "{{name}}" is not in {{caseType}}.'
  66. }
  67. },
  68. create
  69. }