|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +const { findVariable } = require('eslint-utils') |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + meta: { |
| 11 | + type: 'suggestion', |
| 12 | + docs: { |
| 13 | + description: 'disallow destructuring of `props` passed to `setup`', |
| 14 | + category: undefined, |
| 15 | + url: 'https://eslint.vuejs.org/rules/no-setup-props-destructure.html' |
| 16 | + }, |
| 17 | + fixable: null, |
| 18 | + schema: [], |
| 19 | + messages: { |
| 20 | + destructuring: 'Destructuring the `props` will cause the value to lose reactivity.', |
| 21 | + getProperty: 'Getting a value from the `props` in root scope of `setup()` will cause the value to lose reactivity.' |
| 22 | + } |
| 23 | + }, |
| 24 | + create (context) { |
| 25 | + const setupFunctions = new Map() |
| 26 | + const forbiddenNodes = new Map() |
| 27 | + |
| 28 | + function addForbiddenNode (property, node, messageId) { |
| 29 | + let list = forbiddenNodes.get(property) |
| 30 | + if (!list) { |
| 31 | + list = [] |
| 32 | + forbiddenNodes.set(property, list) |
| 33 | + } |
| 34 | + list.push({ |
| 35 | + node, |
| 36 | + messageId |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + function verify (left, right, { propsReferenceIds, setupProperty }) { |
| 41 | + if (!right) { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if (left.type === 'ArrayPattern' || left.type === 'ObjectPattern') { |
| 46 | + if (propsReferenceIds.has(right)) { |
| 47 | + addForbiddenNode(setupProperty, left, 'getProperty') |
| 48 | + } |
| 49 | + } else if (left.type === 'Identifier' && right.type === 'MemberExpression') { |
| 50 | + if (propsReferenceIds.has(right.object)) { |
| 51 | + addForbiddenNode(setupProperty, right, 'getProperty') |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let scopeStack = { upper: null, functionNode: null } |
| 57 | + |
| 58 | + return Object.assign( |
| 59 | + { |
| 60 | + 'Property[value.type=/^(Arrow)?FunctionExpression$/]' (node) { |
| 61 | + if (utils.getStaticPropertyName(node) !== 'setup') { |
| 62 | + return |
| 63 | + } |
| 64 | + const param = node.value.params[0] |
| 65 | + if (!param) { |
| 66 | + // no arguments |
| 67 | + return |
| 68 | + } |
| 69 | + if (param.type === 'RestElement') { |
| 70 | + // cannot check |
| 71 | + return |
| 72 | + } |
| 73 | + if (param.type === 'ArrayPattern' || param.type === 'ObjectPattern') { |
| 74 | + addForbiddenNode(node, param, 'destructuring') |
| 75 | + return |
| 76 | + } |
| 77 | + setupFunctions.set(node.value, { |
| 78 | + setupProperty: node, |
| 79 | + propsParam: param, |
| 80 | + propsReferenceIds: new Set() |
| 81 | + }) |
| 82 | + }, |
| 83 | + ':function' (node) { |
| 84 | + scopeStack = { upper: scopeStack, functionNode: node } |
| 85 | + }, |
| 86 | + ':function>*' (node) { |
| 87 | + const setupFunctionData = setupFunctions.get(node.parent) |
| 88 | + if (!setupFunctionData || setupFunctionData.propsParam !== node) { |
| 89 | + return |
| 90 | + } |
| 91 | + const variable = findVariable(context.getScope(), node) |
| 92 | + if (!variable) { |
| 93 | + return |
| 94 | + } |
| 95 | + const { propsReferenceIds } = setupFunctionData |
| 96 | + for (const reference of variable.references) { |
| 97 | + if (!reference.isRead()) { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + propsReferenceIds.add(reference.identifier) |
| 102 | + } |
| 103 | + }, |
| 104 | + 'VariableDeclarator' (node) { |
| 105 | + const setupFunctionData = setupFunctions.get(scopeStack.functionNode) |
| 106 | + if (!setupFunctionData) { |
| 107 | + return |
| 108 | + } |
| 109 | + verify(node.id, node.init, setupFunctionData) |
| 110 | + }, |
| 111 | + 'AssignmentExpression' (node) { |
| 112 | + const setupFunctionData = setupFunctions.get(scopeStack.functionNode) |
| 113 | + if (!setupFunctionData) { |
| 114 | + return |
| 115 | + } |
| 116 | + verify(node.left, node.right, setupFunctionData) |
| 117 | + }, |
| 118 | + ':function:exit' (node) { |
| 119 | + scopeStack = scopeStack.upper |
| 120 | + |
| 121 | + setupFunctions.delete(node) |
| 122 | + } |
| 123 | + }, |
| 124 | + utils.executeOnVue(context, obj => { |
| 125 | + const reportsList = obj.properties |
| 126 | + .map(item => forbiddenNodes.get(item)) |
| 127 | + .filter(reports => !!reports) |
| 128 | + for (const reports of reportsList) { |
| 129 | + for (const report of reports) { |
| 130 | + context.report(report) |
| 131 | + } |
| 132 | + } |
| 133 | + }) |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
0 commit comments