no-multiple-objects-in-class.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @author tyankatsu <https://github.com/tyankatsu0105>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const { defineTemplateBodyVisitor } = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. /**
  14. * count ObjectExpression element
  15. * @param {VDirective & {value: VExpressionContainer & {expression: ArrayExpression}}} node
  16. * @return {number}
  17. */
  18. function countObjectExpression(node) {
  19. return node.value.expression.elements.filter(
  20. (element) => element && element.type === 'ObjectExpression'
  21. ).length
  22. }
  23. module.exports = {
  24. meta: {
  25. type: 'suggestion',
  26. docs: {
  27. description: 'disallow to pass multiple objects into array to class',
  28. categories: undefined,
  29. url: 'https://eslint.vuejs.org/rules/no-multiple-objects-in-class.html'
  30. },
  31. fixable: null,
  32. schema: [],
  33. messages: {
  34. unexpected: 'Unexpected multiple objects. Merge objects.'
  35. }
  36. },
  37. /** @param {RuleContext} context */
  38. create(context) {
  39. return defineTemplateBodyVisitor(context, {
  40. /** @param {VDirective & {value: VExpressionContainer & {expression: ArrayExpression}}} node */
  41. 'VAttribute[directive=true][key.argument.name="class"][key.name.name="bind"][value.expression.type="ArrayExpression"]'(
  42. node
  43. ) {
  44. if (countObjectExpression(node) > 1) {
  45. context.report({
  46. node,
  47. loc: node.loc,
  48. messageId: 'unexpected'
  49. })
  50. }
  51. }
  52. })
  53. }
  54. }