name-property-casing.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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:
  17. 'enforce specific casing for the name property in Vue components',
  18. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  19. url: 'https://eslint.vuejs.org/rules/name-property-casing.html'
  20. },
  21. deprecated: true,
  22. replacedBy: ['component-definition-name-casing'],
  23. fixable: 'code', // or "code" or "whitespace"
  24. schema: [
  25. {
  26. enum: allowedCaseOptions
  27. }
  28. ]
  29. },
  30. /** @param {RuleContext} context */
  31. create(context) {
  32. const options = context.options[0]
  33. const caseType =
  34. allowedCaseOptions.indexOf(options) !== -1 ? options : 'PascalCase'
  35. // ----------------------------------------------------------------------
  36. // Public
  37. // ----------------------------------------------------------------------
  38. return utils.executeOnVue(context, (obj) => {
  39. const node = utils.findProperty(obj, 'name')
  40. if (!node) return
  41. const valueNode = node.value
  42. if (valueNode.type !== 'Literal') return
  43. if (!casing.getChecker(caseType)(`${valueNode.value}`)) {
  44. const value = casing.getExactConverter(caseType)(`${valueNode.value}`)
  45. context.report({
  46. node: valueNode,
  47. message: 'Property name "{{value}}" is not {{caseType}}.',
  48. data: {
  49. value: `${valueNode.value}`,
  50. caseType
  51. },
  52. fix: (fixer) =>
  53. fixer.replaceText(
  54. valueNode,
  55. context
  56. .getSourceCode()
  57. .getText(valueNode)
  58. .replace(`${valueNode.value}`, value)
  59. )
  60. })
  61. }
  62. })
  63. }
  64. }