prop-name-casing.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // Rule Definition
  11. // ------------------------------------------------------------------------------
  12. function create (context) {
  13. const options = context.options[0]
  14. const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'camelCase'
  15. const converter = casing.getConverter(caseType)
  16. // ----------------------------------------------------------------------
  17. // Public
  18. // ----------------------------------------------------------------------
  19. return utils.executeOnVue(context, (obj) => {
  20. const props = utils.getComponentProps(obj)
  21. .filter(prop => prop.key && (prop.key.type === 'Literal' || (prop.key.type === 'Identifier' && !prop.node.computed)))
  22. for (const item of props) {
  23. const propName = item.key.type === 'Literal' ? item.key.value : item.key.name
  24. if (typeof propName !== 'string') {
  25. // (boolean | null | number | RegExp) Literal
  26. continue
  27. }
  28. const convertedName = converter(propName)
  29. if (convertedName !== propName) {
  30. context.report({
  31. node: item.node,
  32. message: 'Prop "{{name}}" is not in {{caseType}}.',
  33. data: {
  34. name: propName,
  35. caseType: caseType
  36. }
  37. })
  38. }
  39. }
  40. })
  41. }
  42. // ------------------------------------------------------------------------------
  43. // Rule Definition
  44. // ------------------------------------------------------------------------------
  45. module.exports = {
  46. meta: {
  47. type: 'suggestion',
  48. docs: {
  49. description: 'enforce specific casing for the Prop name in Vue components',
  50. category: 'strongly-recommended',
  51. url: 'https://eslint.vuejs.org/rules/prop-name-casing.html'
  52. },
  53. fixable: null, // null or "code" or "whitespace"
  54. schema: [
  55. {
  56. enum: allowedCaseOptions
  57. }
  58. ]
  59. },
  60. create
  61. }