valid-v-bind-sync.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @fileoverview enforce valid `.sync` modifier on `v-bind` directives
  3. * @author Yosuke Ota
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Helpers
  12. // ------------------------------------------------------------------------------
  13. /**
  14. * Check whether the given node is valid or not.
  15. * @param {ASTNode} node The element node to check.
  16. * @returns {boolean} `true` if the node is valid.
  17. */
  18. function isValidElement (node) {
  19. if (
  20. (!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
  21. utils.isHtmlWellKnownElementName(node.rawName) ||
  22. utils.isSvgWellKnownElementName(node.rawName)
  23. ) {
  24. // non Vue-component
  25. return false
  26. }
  27. return true
  28. }
  29. /**
  30. * Check whether the given node can be LHS.
  31. * @param {ASTNode} node The node to check.
  32. * @returns {boolean} `true` if the node can be LHS.
  33. */
  34. function isLhs (node) {
  35. return Boolean(node) && (
  36. node.type === 'Identifier' ||
  37. node.type === 'MemberExpression'
  38. )
  39. }
  40. // ------------------------------------------------------------------------------
  41. // Rule Definition
  42. // ------------------------------------------------------------------------------
  43. module.exports = {
  44. meta: {
  45. type: 'problem',
  46. docs: {
  47. description: 'enforce valid `.sync` modifier on `v-bind` directives',
  48. category: undefined,
  49. // TODO Change with major version.
  50. // category: 'essential',
  51. url: 'https://eslint.vuejs.org/rules/valid-v-bind-sync.html'
  52. },
  53. fixable: null,
  54. schema: [],
  55. messages: {
  56. unexpectedInvalidElement: "'.sync' modifiers aren't supported on <{{name}}> non Vue-components.",
  57. unexpectedNonLhsExpression: "'.sync' modifiers require the attribute value which is valid as LHS.",
  58. unexpectedUpdateIterationVariable: "'.sync' modifiers cannot update the iteration variable '{{varName}}' itself."
  59. }
  60. },
  61. create (context) {
  62. return utils.defineTemplateBodyVisitor(context, {
  63. "VAttribute[directive=true][key.name.name='bind']" (node) {
  64. if (!node.key.modifiers.map(mod => mod.name).includes('sync')) {
  65. return
  66. }
  67. const element = node.parent.parent
  68. const name = element.name
  69. if (!isValidElement(element)) {
  70. context.report({
  71. node,
  72. loc: node.loc,
  73. messageId: 'unexpectedInvalidElement',
  74. data: { name }
  75. })
  76. }
  77. if (node.value) {
  78. if (!isLhs(node.value.expression)) {
  79. context.report({
  80. node,
  81. loc: node.loc,
  82. messageId: 'unexpectedNonLhsExpression'
  83. })
  84. }
  85. for (const reference of node.value.references) {
  86. const id = reference.id
  87. if (id.parent.type !== 'VExpressionContainer') {
  88. continue
  89. }
  90. const variable = reference.variable
  91. if (variable) {
  92. context.report({
  93. node,
  94. loc: node.loc,
  95. messageId: 'unexpectedUpdateIterationVariable',
  96. data: { varName: id.name }
  97. })
  98. }
  99. }
  100. }
  101. }
  102. })
  103. }
  104. }