one-component-per-file.js 1.3 KB

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