prop-name-casing.js 2.2 KB

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