name-property-casing.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // deprecated: true, // TODO Change with major version.
  21. // replacedBy: ['component-definition-name-casing'], // TODO Change with major version.
  22. fixable: 'code', // or "code" or "whitespace"
  23. schema: [
  24. {
  25. enum: allowedCaseOptions
  26. }
  27. ]
  28. },
  29. create (context) {
  30. const options = context.options[0]
  31. const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'PascalCase'
  32. // ----------------------------------------------------------------------
  33. // Public
  34. // ----------------------------------------------------------------------
  35. return utils.executeOnVue(context, (obj) => {
  36. const node = obj.properties
  37. .find(item => (
  38. item.type === 'Property' &&
  39. item.key.name === 'name' &&
  40. item.value.type === 'Literal'
  41. ))
  42. if (!node) return
  43. const value = casing.getConverter(caseType)(node.value.value)
  44. if (value !== node.value.value) {
  45. context.report({
  46. node: node.value,
  47. message: 'Property name "{{value}}" is not {{caseType}}.',
  48. data: {
  49. value: node.value.value,
  50. caseType: caseType
  51. },
  52. fix: fixer => fixer.replaceText(node.value, node.value.raw.replace(node.value.value, value))
  53. })
  54. }
  55. })
  56. }
  57. }