name-property-casing.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @fileoverview Requires specific casing for the name property in Vue components
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const casing = require('../utils/casing')
  8. const allowedCaseOptions = ['PascalCase', 'kebab-case']
  9. // ------------------------------------------------------------------------------
  10. // Rule Definition
  11. // ------------------------------------------------------------------------------
  12. module.exports = {
  13. meta: {
  14. type: 'suggestion',
  15. docs: {
  16. description: 'enforce specific casing for the name property in Vue components',
  17. category: 'strongly-recommended',
  18. url: 'https://eslint.vuejs.org/rules/name-property-casing.html'
  19. },
  20. fixable: 'code', // or "code" or "whitespace"
  21. schema: [
  22. {
  23. enum: allowedCaseOptions
  24. }
  25. ]
  26. },
  27. create (context) {
  28. const options = context.options[0]
  29. const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'PascalCase'
  30. // ----------------------------------------------------------------------
  31. // Public
  32. // ----------------------------------------------------------------------
  33. return utils.executeOnVue(context, (obj) => {
  34. const node = obj.properties
  35. .find(item => (
  36. item.type === 'Property' &&
  37. item.key.name === 'name' &&
  38. item.value.type === 'Literal'
  39. ))
  40. if (!node) return
  41. const value = casing.getConverter(caseType)(node.value.value)
  42. if (value !== node.value.value) {
  43. context.report({
  44. node: node.value,
  45. message: 'Property name "{{value}}" is not {{caseType}}.',
  46. data: {
  47. value: node.value.value,
  48. caseType: caseType
  49. },
  50. fix: fixer => fixer.replaceText(node.value, node.value.raw.replace(node.value.value, value))
  51. })
  52. }
  53. })
  54. }
  55. }