|
| 1 | +/** |
| 2 | + * @fileoverview disallow using deprecated object declaration on data |
| 3 | + * @author yoyo930021 |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +function isOpenParen (token) { |
| 14 | + return token.type === 'Punctuator' && token.value === '(' |
| 15 | +} |
| 16 | + |
| 17 | +function isCloseParen (token) { |
| 18 | + return token.type === 'Punctuator' && token.value === ')' |
| 19 | +} |
| 20 | + |
| 21 | +function getFirstAndLastTokens (node, sourceCode) { |
| 22 | + let first = sourceCode.getFirstToken(node) |
| 23 | + let last = sourceCode.getLastToken(node) |
| 24 | + |
| 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 | +// ------------------------------------------------------------------------------ |
| 39 | +// Rule Definition |
| 40 | +// ------------------------------------------------------------------------------ |
| 41 | + |
| 42 | +module.exports = { |
| 43 | + meta: { |
| 44 | + type: 'problem', |
| 45 | + docs: { |
| 46 | + description: 'disallow using deprecated object declaration on data', |
| 47 | + categories: ['vue3-essential'], |
| 48 | + url: 'https://eslint.vuejs.org/rules/no-deprecated-data-object-declaration.html' |
| 49 | + }, |
| 50 | + fixable: 'code', |
| 51 | + schema: [], |
| 52 | + messages: { |
| 53 | + objectDeclarationIsDeprecated: "Object declaration on \'data\' property is deprecated. Using function declaration instead." |
| 54 | + } |
| 55 | + }, |
| 56 | + |
| 57 | + create (context) { |
| 58 | + const sourceCode = context.getSourceCode() |
| 59 | + |
| 60 | + return utils.executeOnVue(context, (obj) => { |
| 61 | + obj.properties |
| 62 | + .filter(p => |
| 63 | + p.type === 'Property' && |
| 64 | + p.key.type === 'Identifier' && |
| 65 | + p.key.name === 'data' && |
| 66 | + p.value.type !== 'FunctionExpression' && |
| 67 | + p.value.type !== 'ArrowFunctionExpression' && |
| 68 | + p.value.type !== 'Identifier' |
| 69 | + ) |
| 70 | + .forEach(p => { |
| 71 | + context.report({ |
| 72 | + node: p, |
| 73 | + messageId: 'objectDeclarationIsDeprecated', |
| 74 | + fix (fixer) { |
| 75 | + const tokens = getFirstAndLastTokens(p.value, sourceCode) |
| 76 | + |
| 77 | + return [ |
| 78 | + fixer.insertTextBefore(tokens.first, 'function() {\nreturn '), |
| 79 | + fixer.insertTextAfter(tokens.last, ';\n}') |
| 80 | + ] |
| 81 | + } |
| 82 | + }) |
| 83 | + }) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments