one-component-per-file.js 1.3 KB

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