|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef {import('../utils').VueObjectData} VueObjectData |
| 15 | + */ |
| 16 | + |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | +// Rule Definition |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +module.exports = { |
| 22 | + meta: { |
| 23 | + type: 'problem', |
| 24 | + docs: { |
| 25 | + description: 'disallow accessing computed properties in `data`.', |
| 26 | + categories: undefined, |
| 27 | + url: 'https://eslint.vuejs.org/rules/no-computed-properties-in-data.html' |
| 28 | + }, |
| 29 | + fixable: null, |
| 30 | + schema: [], |
| 31 | + messages: { |
| 32 | + cannotBeUsed: |
| 33 | + 'The computed property cannot be used in `data()` because it is before initialization.' |
| 34 | + } |
| 35 | + }, |
| 36 | + /** @param {RuleContext} context */ |
| 37 | + create(context) { |
| 38 | + /** @type {Map<ObjectExpression, {data: FunctionExpression | ArrowFunctionExpression, computedNames:Set<string>}>} */ |
| 39 | + const contextMap = new Map() |
| 40 | + |
| 41 | + /** |
| 42 | + * @typedef {object} ScopeStack |
| 43 | + * @property {ScopeStack | null} upper |
| 44 | + * @property {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node |
| 45 | + */ |
| 46 | + /** @type {ScopeStack | null} */ |
| 47 | + let scopeStack = null |
| 48 | + |
| 49 | + return utils.compositingVisitors( |
| 50 | + { |
| 51 | + /** |
| 52 | + * @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node |
| 53 | + */ |
| 54 | + ':function'(node) { |
| 55 | + scopeStack = { |
| 56 | + upper: scopeStack, |
| 57 | + node |
| 58 | + } |
| 59 | + }, |
| 60 | + ':function:exit'() { |
| 61 | + scopeStack = scopeStack && scopeStack.upper |
| 62 | + } |
| 63 | + }, |
| 64 | + utils.defineVueVisitor(context, { |
| 65 | + onVueObjectEnter(node) { |
| 66 | + const dataProperty = utils.findProperty(node, 'data') |
| 67 | + if ( |
| 68 | + !dataProperty || |
| 69 | + (dataProperty.value.type !== 'FunctionExpression' && |
| 70 | + dataProperty.value.type !== 'ArrowFunctionExpression') |
| 71 | + ) { |
| 72 | + return |
| 73 | + } |
| 74 | + const computedNames = new Set() |
| 75 | + for (const computed of utils.iterateProperties( |
| 76 | + node, |
| 77 | + new Set(['computed']) |
| 78 | + )) { |
| 79 | + computedNames.add(computed.name) |
| 80 | + } |
| 81 | + |
| 82 | + contextMap.set(node, { data: dataProperty.value, computedNames }) |
| 83 | + }, |
| 84 | + /** |
| 85 | + * @param {MemberExpression} node |
| 86 | + * @param {VueObjectData} vueData |
| 87 | + */ |
| 88 | + MemberExpression(node, vueData) { |
| 89 | + if (!scopeStack || !utils.isThis(node.object, context)) { |
| 90 | + return |
| 91 | + } |
| 92 | + const ctx = contextMap.get(vueData.node) |
| 93 | + if (!ctx || ctx.data !== scopeStack.node) { |
| 94 | + return |
| 95 | + } |
| 96 | + const name = utils.getStaticPropertyName(node) |
| 97 | + if (!name || !ctx.computedNames.has(name)) { |
| 98 | + return |
| 99 | + } |
| 100 | + context.report({ |
| 101 | + node, |
| 102 | + messageId: 'cannotBeUsed' |
| 103 | + }) |
| 104 | + } |
| 105 | + }) |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
0 commit comments