|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +const { ReferenceTracker } = require('eslint-utils') |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + meta: { |
| 11 | + type: 'suggestion', |
| 12 | + docs: { |
| 13 | + description: 'disallow asynchronously registered `watch`', |
| 14 | + category: undefined, |
| 15 | + url: 'https://eslint.vuejs.org/rules/no-watch-after-await.html' |
| 16 | + }, |
| 17 | + fixable: null, |
| 18 | + schema: [], |
| 19 | + messages: { |
| 20 | + forbidden: 'The `watch` after `await` expression are forbidden.' |
| 21 | + } |
| 22 | + }, |
| 23 | + create (context) { |
| 24 | + const watchCallNodes = new Set() |
| 25 | + const setupFunctions = new Map() |
| 26 | + const forbiddenNodes = new Map() |
| 27 | + |
| 28 | + function addForbiddenNode (property, node) { |
| 29 | + let list = forbiddenNodes.get(property) |
| 30 | + if (!list) { |
| 31 | + list = [] |
| 32 | + forbiddenNodes.set(property, list) |
| 33 | + } |
| 34 | + list.push(node) |
| 35 | + } |
| 36 | + |
| 37 | + let scopeStack = { upper: null, functionNode: null } |
| 38 | + |
| 39 | + return Object.assign( |
| 40 | + { |
| 41 | + 'Program' () { |
| 42 | + const tracker = new ReferenceTracker(context.getScope()) |
| 43 | + const traceMap = { |
| 44 | + vue: { |
| 45 | + [ReferenceTracker.ESM]: true, |
| 46 | + watch: { |
| 47 | + [ReferenceTracker.CALL]: true |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + for (const { node } of tracker.iterateEsmReferences(traceMap)) { |
| 53 | + watchCallNodes.add(node) |
| 54 | + } |
| 55 | + }, |
| 56 | + 'Property[value.type=/^(Arrow)?FunctionExpression$/]' (node) { |
| 57 | + if (utils.getStaticPropertyName(node) !== 'setup') { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + setupFunctions.set(node.value, { |
| 62 | + setupProperty: node, |
| 63 | + afterAwait: false |
| 64 | + }) |
| 65 | + }, |
| 66 | + ':function' (node) { |
| 67 | + scopeStack = { upper: scopeStack, functionNode: node } |
| 68 | + }, |
| 69 | + 'AwaitExpression' () { |
| 70 | + const setupFunctionData = setupFunctions.get(scopeStack.functionNode) |
| 71 | + if (!setupFunctionData) { |
| 72 | + return |
| 73 | + } |
| 74 | + setupFunctionData.afterAwait = true |
| 75 | + }, |
| 76 | + 'CallExpression' (node) { |
| 77 | + const setupFunctionData = setupFunctions.get(scopeStack.functionNode) |
| 78 | + if (!setupFunctionData || !setupFunctionData.afterAwait) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if (watchCallNodes.has(node)) { |
| 83 | + addForbiddenNode(setupFunctionData.setupProperty, node) |
| 84 | + } |
| 85 | + }, |
| 86 | + ':function:exit' (node) { |
| 87 | + scopeStack = scopeStack.upper |
| 88 | + |
| 89 | + setupFunctions.delete(node) |
| 90 | + } |
| 91 | + }, |
| 92 | + utils.executeOnVue(context, obj => { |
| 93 | + const reportsList = obj.properties |
| 94 | + .map(item => forbiddenNodes.get(item)) |
| 95 | + .filter(reports => !!reports) |
| 96 | + for (const reports of reportsList) { |
| 97 | + for (const node of reports) { |
| 98 | + context.report({ |
| 99 | + node, |
| 100 | + messageId: 'forbidden' |
| 101 | + }) |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
0 commit comments