one-component-per-file.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @fileoverview enforce that each component should be in its own file
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const { getVueComponentDefinitionType } = require('../utils')
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. type: 'suggestion',
  14. docs: {
  15. description: 'enforce that each component should be in its own file',
  16. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  17. url: 'https://eslint.vuejs.org/rules/one-component-per-file.html'
  18. },
  19. fixable: null,
  20. schema: [],
  21. messages: {
  22. toManyComponents: 'There is more than one component in this file.'
  23. }
  24. },
  25. /** @param {RuleContext} context */
  26. create(context) {
  27. /** @type {ObjectExpression[]} */
  28. const components = []
  29. return Object.assign(
  30. {},
  31. utils.executeOnVueComponent(context, (node, type) => {
  32. if (type === 'definition') {
  33. const defType = getVueComponentDefinitionType(node)
  34. if (defType === 'mixin') {
  35. return
  36. }
  37. }
  38. components.push(node)
  39. }),
  40. {
  41. 'Program:exit'() {
  42. if (components.length > 1) {
  43. for (const node of components) {
  44. context.report({
  45. node,
  46. messageId: 'toManyComponents'
  47. })
  48. }
  49. }
  50. }
  51. }
  52. )
  53. }
  54. }