prop-name-casing.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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').ComponentArrayProp} ComponentArrayProp
  11. * @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp
  12. * @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp
  13. */
  14. // ------------------------------------------------------------------------------
  15. // Rule Definition
  16. // ------------------------------------------------------------------------------
  17. /** @param {RuleContext} context */
  18. function create(context) {
  19. const options = context.options[0]
  20. const caseType =
  21. allowedCaseOptions.indexOf(options) !== -1 ? options : 'camelCase'
  22. const checker = casing.getChecker(caseType)
  23. // ----------------------------------------------------------------------
  24. // Public
  25. // ----------------------------------------------------------------------
  26. /**
  27. * @param {(ComponentArrayProp | ComponentObjectProp | ComponentTypeProp)[]} props
  28. */
  29. function processProps(props) {
  30. for (const item of props) {
  31. const propName = item.propName
  32. if (propName == null) {
  33. continue
  34. }
  35. if (!checker(propName)) {
  36. context.report({
  37. node: item.node,
  38. message: 'Prop "{{name}}" is not in {{caseType}}.',
  39. data: {
  40. name: propName,
  41. caseType
  42. }
  43. })
  44. }
  45. }
  46. }
  47. return utils.compositingVisitors(
  48. utils.defineScriptSetupVisitor(context, {
  49. onDefinePropsEnter(_node, props) {
  50. processProps(props)
  51. }
  52. }),
  53. utils.executeOnVue(context, (obj) => {
  54. processProps(utils.getComponentProps(obj))
  55. })
  56. )
  57. }
  58. // ------------------------------------------------------------------------------
  59. // Rule Definition
  60. // ------------------------------------------------------------------------------
  61. module.exports = {
  62. meta: {
  63. type: 'suggestion',
  64. docs: {
  65. description:
  66. 'enforce specific casing for the Prop name in Vue components',
  67. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  68. url: 'https://eslint.vuejs.org/rules/prop-name-casing.html'
  69. },
  70. fixable: null, // null or "code" or "whitespace"
  71. schema: [
  72. {
  73. enum: allowedCaseOptions
  74. }
  75. ]
  76. },
  77. create
  78. }