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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: 'https://eslint.vuejs.org/rules/no-deprecated-data-object-declaration.html'
  48. },
  49. fixable: 'code',
  50. schema: [],
  51. messages: {
  52. objectDeclarationIsDeprecated:
  53. "Object declaration on 'data' property is deprecated. Using function declaration instead."
  54. }
  55. },
  56. /** @param {RuleContext} context */
  57. create(context) {
  58. const sourceCode = context.getSourceCode()
  59. return utils.executeOnVue(context, (obj) => {
  60. const invalidData = utils.findProperty(
  61. obj,
  62. 'data',
  63. (p) =>
  64. p.value.type !== 'FunctionExpression' &&
  65. p.value.type !== 'ArrowFunctionExpression' &&
  66. p.value.type !== 'Identifier'
  67. )
  68. if (invalidData) {
  69. context.report({
  70. node: invalidData,
  71. messageId: 'objectDeclarationIsDeprecated',
  72. fix(fixer) {
  73. const tokens = getFirstAndLastTokens(invalidData.value, sourceCode)
  74. return [
  75. fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
  76. fixer.insertTextAfter(tokens.last, ';\n}')
  77. ]
  78. }
  79. })
  80. }
  81. })
  82. }
  83. }