|
| 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 { findVariable } = require('eslint-utils') |
| 12 | +const utils = require('../utils') |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Helpers |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +/** |
| 19 | + * Get the callee member node from the given CallExpression |
| 20 | + * @param {CallExpression} node CallExpression |
| 21 | + */ |
| 22 | +function getCalleeMemberNode(node) { |
| 23 | + const callee = utils.skipChainExpression(node.callee) |
| 24 | + |
| 25 | + if (callee.type === 'MemberExpression') { |
| 26 | + const name = utils.getStaticPropertyName(callee) |
| 27 | + if (name) { |
| 28 | + return { name, member: callee } |
| 29 | + } |
| 30 | + } |
| 31 | + return null |
| 32 | +} |
| 33 | + |
| 34 | +// ------------------------------------------------------------------------------ |
| 35 | +// Rule Definition |
| 36 | +// ------------------------------------------------------------------------------ |
| 37 | + |
| 38 | +module.exports = { |
| 39 | + meta: { |
| 40 | + type: 'problem', |
| 41 | + docs: { |
| 42 | + description: 'disallow asynchronously registered `expose`', |
| 43 | + categories: undefined, |
| 44 | + // categories: ['vue3-essential'], TODO Change with the major version |
| 45 | + url: 'https://eslint.vuejs.org/rules/no-expose-after-await.html' |
| 46 | + }, |
| 47 | + fixable: null, |
| 48 | + schema: [], |
| 49 | + messages: { |
| 50 | + forbidden: 'The `expose` after `await` expression are forbidden.' |
| 51 | + } |
| 52 | + }, |
| 53 | + /** @param {RuleContext} context */ |
| 54 | + create(context) { |
| 55 | + /** |
| 56 | + * @typedef {object} SetupScopeData |
| 57 | + * @property {boolean} afterAwait |
| 58 | + * @property {[number,number]} range |
| 59 | + * @property {Set<Identifier>} exposeReferenceIds |
| 60 | + * @property {Set<Identifier>} contextReferenceIds |
| 61 | + */ |
| 62 | + /** |
| 63 | + * @typedef {object} ScopeStack |
| 64 | + * @property {ScopeStack | null} upper |
| 65 | + * @property {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} scopeNode |
| 66 | + */ |
| 67 | + /** @type {Map<FunctionDeclaration | FunctionExpression | ArrowFunctionExpression, SetupScopeData>} */ |
| 68 | + const setupScopes = new Map() |
| 69 | + |
| 70 | + /** @type {ScopeStack | null} */ |
| 71 | + let scopeStack = null |
| 72 | + |
| 73 | + return utils.defineVueVisitor(context, { |
| 74 | + onSetupFunctionEnter(node) { |
| 75 | + const contextParam = node.params[1] |
| 76 | + if (!contextParam) { |
| 77 | + // no arguments |
| 78 | + return |
| 79 | + } |
| 80 | + if (contextParam.type === 'RestElement') { |
| 81 | + // cannot check |
| 82 | + return |
| 83 | + } |
| 84 | + if (contextParam.type === 'ArrayPattern') { |
| 85 | + // cannot check |
| 86 | + return |
| 87 | + } |
| 88 | + /** @type {Set<Identifier>} */ |
| 89 | + const contextReferenceIds = new Set() |
| 90 | + /** @type {Set<Identifier>} */ |
| 91 | + const exposeReferenceIds = new Set() |
| 92 | + if (contextParam.type === 'ObjectPattern') { |
| 93 | + const exposeProperty = utils.findAssignmentProperty( |
| 94 | + contextParam, |
| 95 | + 'expose' |
| 96 | + ) |
| 97 | + if (!exposeProperty) { |
| 98 | + return |
| 99 | + } |
| 100 | + const exposeParam = exposeProperty.value |
| 101 | + // `setup(props, {emit})` |
| 102 | + const variable = |
| 103 | + exposeParam.type === 'Identifier' |
| 104 | + ? findVariable(context.getScope(), exposeParam) |
| 105 | + : null |
| 106 | + if (!variable) { |
| 107 | + return |
| 108 | + } |
| 109 | + for (const reference of variable.references) { |
| 110 | + if (!reference.isRead()) { |
| 111 | + continue |
| 112 | + } |
| 113 | + exposeReferenceIds.add(reference.identifier) |
| 114 | + } |
| 115 | + } else if (contextParam.type === 'Identifier') { |
| 116 | + // `setup(props, context)` |
| 117 | + const variable = findVariable(context.getScope(), contextParam) |
| 118 | + if (!variable) { |
| 119 | + return |
| 120 | + } |
| 121 | + for (const reference of variable.references) { |
| 122 | + if (!reference.isRead()) { |
| 123 | + continue |
| 124 | + } |
| 125 | + contextReferenceIds.add(reference.identifier) |
| 126 | + } |
| 127 | + } |
| 128 | + setupScopes.set(node, { |
| 129 | + afterAwait: false, |
| 130 | + range: node.range, |
| 131 | + exposeReferenceIds, |
| 132 | + contextReferenceIds |
| 133 | + }) |
| 134 | + }, |
| 135 | + /** |
| 136 | + * @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node |
| 137 | + */ |
| 138 | + ':function'(node) { |
| 139 | + scopeStack = { |
| 140 | + upper: scopeStack, |
| 141 | + scopeNode: node |
| 142 | + } |
| 143 | + }, |
| 144 | + ':function:exit'() { |
| 145 | + scopeStack = scopeStack && scopeStack.upper |
| 146 | + }, |
| 147 | + /** @param {AwaitExpression} node */ |
| 148 | + AwaitExpression(node) { |
| 149 | + if (!scopeStack) { |
| 150 | + return |
| 151 | + } |
| 152 | + const setupScope = setupScopes.get(scopeStack.scopeNode) |
| 153 | + if (!setupScope || !utils.inRange(setupScope.range, node)) { |
| 154 | + return |
| 155 | + } |
| 156 | + setupScope.afterAwait = true |
| 157 | + }, |
| 158 | + /** @param {CallExpression} node */ |
| 159 | + CallExpression(node) { |
| 160 | + if (!scopeStack) { |
| 161 | + return |
| 162 | + } |
| 163 | + const setupScope = setupScopes.get(scopeStack.scopeNode) |
| 164 | + if ( |
| 165 | + !setupScope || |
| 166 | + !setupScope.afterAwait || |
| 167 | + !utils.inRange(setupScope.range, node) |
| 168 | + ) { |
| 169 | + return |
| 170 | + } |
| 171 | + const { contextReferenceIds, exposeReferenceIds } = setupScope |
| 172 | + if ( |
| 173 | + node.callee.type === 'Identifier' && |
| 174 | + exposeReferenceIds.has(node.callee) |
| 175 | + ) { |
| 176 | + // setup(props,{expose}) {expose()} |
| 177 | + context.report({ |
| 178 | + node, |
| 179 | + messageId: 'forbidden' |
| 180 | + }) |
| 181 | + } else { |
| 182 | + const expose = getCalleeMemberNode(node) |
| 183 | + if ( |
| 184 | + expose && |
| 185 | + expose.name === 'expose' && |
| 186 | + expose.member.object.type === 'Identifier' && |
| 187 | + contextReferenceIds.has(expose.member.object) |
| 188 | + ) { |
| 189 | + // setup(props,context) {context.emit()} |
| 190 | + context.report({ |
| 191 | + node, |
| 192 | + messageId: 'forbidden' |
| 193 | + }) |
| 194 | + } |
| 195 | + } |
| 196 | + }, |
| 197 | + onSetupFunctionExit(node) { |
| 198 | + setupScopes.delete(node) |
| 199 | + } |
| 200 | + }) |
| 201 | + } |
| 202 | +} |
0 commit comments