no-deprecated-data-object-declaration.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @fileoverview disallow using deprecated object declaration on data
  3. * @author yoyo930021
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. /** @param {Token} token */
  11. function isOpenParen(token) {
  12. return token.type === 'Punctuator' && token.value === '('
  13. }
  14. /** @param {Token} token */
  15. function isCloseParen(token) {
  16. return token.type === 'Punctuator' && token.value === ')'
  17. }
  18. /**
  19. * @param {Expression} node
  20. * @param {SourceCode} sourceCode
  21. */
  22. function getFirstAndLastTokens(node, sourceCode) {
  23. let first = sourceCode.getFirstToken(node)
  24. let last = sourceCode.getLastToken(node)
  25. // If the value enclosed by parentheses, update the 'first' and 'last' by the parentheses.
  26. while (true) {
  27. const prev = sourceCode.getTokenBefore(first)
  28. const next = sourceCode.getTokenAfter(last)
  29. if (isOpenParen(prev) && isCloseParen(next)) {
  30. first = prev
  31. last = next
  32. } else {
  33. return { first, last }
  34. }
  35. }
  36. }
  37. // ------------------------------------------------------------------------------
  38. // Rule Definition
  39. // ------------------------------------------------------------------------------
  40. module.exports = {
  41. meta: {
  42. type: 'problem',
  43. docs: {
  44. description:
  45. 'disallow using deprecated object declaration on data (in Vue.js 3.0.0+)',
  46. categories: ['vue3-essential'],
  47. url:
  48. 'https://eslint.vuejs.org/rules/no-deprecated-data-object-declaration.html'
  49. },
  50. fixable: 'code',
  51. schema: [],
  52. messages: {
  53. objectDeclarationIsDeprecated:
  54. "Object declaration on 'data' property is deprecated. Using function declaration instead."
  55. }
  56. },
  57. /** @param {RuleContext} context */
  58. create(context) {
  59. const sourceCode = context.getSourceCode()
  60. return utils.executeOnVue(context, (obj) => {
  61. const invalidData = utils.findProperty(
  62. obj,
  63. 'data',
  64. (p) =>
  65. p.value.type !== 'FunctionExpression' &&
  66. p.value.type !== 'ArrowFunctionExpression' &&
  67. p.value.type !== 'Identifier'
  68. )
  69. if (invalidData) {
  70. context.report({
  71. node: invalidData,
  72. messageId: 'objectDeclarationIsDeprecated',
  73. fix(fixer) {
  74. const tokens = getFirstAndLastTokens(invalidData.value, sourceCode)
  75. return [
  76. fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
  77. fixer.insertTextAfter(tokens.last, ';\n}')
  78. ]
  79. }
  80. })
  81. }
  82. })
  83. }
  84. }