no-deprecated-v-bind-sync.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @author Przemyslaw Falowski (@przemkow)
  3. * @fileoverview Disallow use of deprecated `.sync` modifier on `v-bind` directive (in Vue.js 3.0.0+)
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: 'problem',
  16. docs: {
  17. description:
  18. 'disallow use of deprecated `.sync` modifier on `v-bind` directive (in Vue.js 3.0.0+)',
  19. categories: ['vue3-essential'],
  20. url: 'https://eslint.vuejs.org/rules/no-deprecated-v-bind-sync.html'
  21. },
  22. fixable: 'code',
  23. schema: [],
  24. messages: {
  25. syncModifierIsDeprecated:
  26. "'.sync' modifier on 'v-bind' directive is deprecated. Use 'v-model:propName' instead."
  27. }
  28. },
  29. /** @param {RuleContext} context */
  30. create(context) {
  31. return utils.defineTemplateBodyVisitor(context, {
  32. "VAttribute[directive=true][key.name.name='bind']"(node) {
  33. if (node.key.modifiers.map((mod) => mod.name).includes('sync')) {
  34. context.report({
  35. node,
  36. loc: node.loc,
  37. messageId: 'syncModifierIsDeprecated',
  38. fix(fixer) {
  39. if (node.key.argument == null) {
  40. // is using spread syntax
  41. return null
  42. }
  43. if (node.key.modifiers.length > 1) {
  44. // has multiple modifiers
  45. return null
  46. }
  47. const bindArgument = context
  48. .getSourceCode()
  49. .getText(node.key.argument)
  50. return fixer.replaceText(node.key, `v-model:${bindArgument}`)
  51. }
  52. })
  53. }
  54. }
  55. })
  56. }
  57. }