zammad-copyright.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. /**
  3. * @fileoverview Enforce presence of Zammad copyright header
  4. * @author Martin Gruner
  5. */
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /**
  10. * @type {import('eslint').Rule.RuleModule}
  11. */
  12. module.exports = {
  13. meta: {
  14. type: 'problem',
  15. docs: {
  16. description: 'Enforce presence of Zammad copyright header',
  17. category: 'Layout & Formatting',
  18. recommended: true,
  19. url: null,
  20. },
  21. fixable: 'code',
  22. schema: [],
  23. },
  24. create(context) {
  25. const year = new Date().getYear() + 1900
  26. let expectedComment = `// Copyright (C) 2012-${year} Zammad Foundation, https://zammad-foundation.org/`
  27. let findComment = '// Copyright'
  28. if (context.getFilename().endsWith('.vue')) {
  29. expectedComment = `<!-- Copyright (C) 2012-${year} Zammad Foundation, https://zammad-foundation.org/ -->`
  30. findComment = '<!-- Copyright'
  31. }
  32. return {
  33. Program(node) {
  34. const firstLine = context.getSourceCode().lines[0]
  35. if (!firstLine.length) return
  36. if (firstLine === expectedComment) return
  37. if (firstLine.startsWith(findComment)) {
  38. const range = [0, firstLine.length]
  39. context.report({
  40. loc: node.loc,
  41. message: 'Wrong Zammad copyright header.',
  42. fix(fixer) {
  43. return fixer.replaceTextRange(range, expectedComment)
  44. },
  45. })
  46. return
  47. }
  48. context.report({
  49. loc: node.loc,
  50. message: 'Missing Zammad copyright header.',
  51. fix(fixer) {
  52. return fixer.insertTextBeforeRange([0, 0], `${expectedComment}\n\n`)
  53. },
  54. })
  55. },
  56. }
  57. },
  58. }