|
| 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 | +function isMaybeUsedStopHandle (node) { |
| 10 | + const parent = node.parent |
| 11 | + if (parent) { |
| 12 | + if (parent.type === 'VariableDeclarator') { |
| 13 | + // var foo = watch() |
| 14 | + return true |
| 15 | + } |
| 16 | + if (parent.type === 'AssignmentExpression') { |
| 17 | + // foo = watch() |
| 18 | + return true |
| 19 | + } |
| 20 | + if (parent.type === 'CallExpression') { |
| 21 | + // foo(watch()) |
| 22 | + return true |
| 23 | + } |
| 24 | + if (parent.type === 'Property') { |
| 25 | + // {foo: watch()} |
| 26 | + return true |
| 27 | + } |
| 28 | + if (parent.type === 'ArrayExpression') { |
| 29 | + // [watch()] |
| 30 | + return true |
| 31 | + } |
| 32 | + } |
| 33 | + return false |
| 34 | +} |
| 35 | + |
| 36 | +module.exports = { |
| 37 | + meta: { |
| 38 | + type: 'suggestion', |
| 39 | + docs: { |
| 40 | + description: 'disallow asynchronously registered `watch`', |
| 41 | + categories: ['vue3-essential'], |
| 42 | + url: 'https://eslint.vuejs.org/rules/no-watch-after-await.html' |
| 43 | + }, |
| 44 | + fixable: null, |
| 45 | + schema: [], |
| 46 | + messages: { |
| 47 | + forbidden: 'The `watch` after `await` expression are forbidden.' |
| 48 | + } |
| 49 | + }, |
| 50 | + create (context) { |
| 51 | + const watchCallNodes = new Set() |
| 52 | + const setupFunctions = new Map() |
| 53 | + const forbiddenNodes = new Map() |
| 54 | + |
| 55 | + function addForbiddenNode (property, node) { |
| 56 | + let list = forbiddenNodes.get(property) |
| 57 | + if (!list) { |
| 58 | + list = [] |
| 59 | + forbiddenNodes.set(property, list) |
| 60 | + } |
| 61 | + list.push(node) |
| 62 | + } |
| 63 | + |
| 64 | + let scopeStack = { upper: null, functionNode: null } |
| 65 | + |
| 66 | + return Object.assign( |
| 67 | + { |
| 68 | + 'Program' () { |
| 69 | + const tracker = new ReferenceTracker(context.getScope()) |
| 70 | + const traceMap = { |
| 71 | + vue: { |
| 72 | + [ReferenceTracker.ESM]: true, |
| 73 | + watch: { |
| 74 | + [ReferenceTracker.CALL]: true |
| 75 | + }, |
| 76 | + watchEffect: { |
| 77 | + [ReferenceTracker.CALL]: true |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (const { node } of tracker.iterateEsmReferences(traceMap)) { |
| 83 | + watchCallNodes.add(node) |
| 84 | + } |
| 85 | + }, |
| 86 | + 'Property[value.type=/^(Arrow)?FunctionExpression$/]' (node) { |
| 87 | + if (utils.getStaticPropertyName(node) !== 'setup') { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + setupFunctions.set(node.value, { |
| 92 | + setupProperty: node, |
| 93 | + afterAwait: false |
| 94 | + }) |
| 95 | + }, |
| 96 | + ':function' (node) { |
| 97 | + scopeStack = { upper: scopeStack, functionNode: node } |
| 98 | + }, |
| 99 | + 'AwaitExpression' () { |
| 100 | + const setupFunctionData = setupFunctions.get(scopeStack.functionNode) |
| 101 | + if (!setupFunctionData) { |
| 102 | + return |
| 103 | + } |
| 104 | + setupFunctionData.afterAwait = true |
| 105 | + }, |
| 106 | + 'CallExpression' (node) { |
| 107 | + const setupFunctionData = setupFunctions.get(scopeStack.functionNode) |
| 108 | + if (!setupFunctionData || !setupFunctionData.afterAwait) { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) { |
| 113 | + addForbiddenNode(setupFunctionData.setupProperty, node) |
| 114 | + } |
| 115 | + }, |
| 116 | + ':function:exit' (node) { |
| 117 | + scopeStack = scopeStack.upper |
| 118 | + |
| 119 | + setupFunctions.delete(node) |
| 120 | + } |
| 121 | + }, |
| 122 | + utils.executeOnVue(context, obj => { |
| 123 | + const reportsList = obj.properties |
| 124 | + .map(item => forbiddenNodes.get(item)) |
| 125 | + .filter(reports => !!reports) |
| 126 | + for (const reports of reportsList) { |
| 127 | + for (const node of reports) { |
| 128 | + context.report({ |
| 129 | + node, |
| 130 | + messageId: 'forbidden' |
| 131 | + }) |
| 132 | + } |
| 133 | + } |
| 134 | + }) |
| 135 | + ) |
| 136 | + } |
| 137 | +} |
0 commit comments