Skip to content

Update vue/no-watch-after-await rule to support <script setup> #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 58 additions & 25 deletions lib/rules/no-watch-after-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,31 @@ module.exports = {
/** @param {RuleContext} context */
create(context) {
const watchCallNodes = new Set()
/** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration, { setupProperty: Property, afterAwait: boolean }>} */
const setupFunctions = new Map()
/**
* @typedef {object} SetupScopeData
* @property {boolean} afterAwait
* @property {[number,number]} range
*/
/** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program, SetupScopeData>} */
const setupScopes = new Map()

/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} functionNode
* @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program} scopeNode
*/
/** @type {ScopeStack | null} */
let scopeStack = null

return Object.assign(
return utils.compositingVisitors(
{
Program() {
/** @param {Program} node */
Program(node) {
scopeStack = {
upper: scopeStack,
scopeNode: node
}

const tracker = new ReferenceTracker(context.getScope())
const traceMap = {
vue: {
Expand All @@ -87,39 +98,39 @@ module.exports = {
for (const { node } of tracker.iterateEsmReferences(traceMap)) {
watchCallNodes.add(node)
}
}
},
utils.defineVueVisitor(context, {
},
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
':function'(node) {
scopeStack = {
upper: scopeStack,
functionNode: node
scopeNode: node
}
},
onSetupFunctionEnter(node) {
setupFunctions.set(node, {
setupProperty: node.parent,
afterAwait: false
})
':function:exit'() {
scopeStack = scopeStack && scopeStack.upper
},
AwaitExpression() {
/** @param {AwaitExpression} node */
AwaitExpression(node) {
if (!scopeStack) {
return
}
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData) {
const setupScope = setupScopes.get(scopeStack.scopeNode)
if (!setupScope || !utils.inRange(setupScope.range, node)) {
return
}
setupFunctionData.afterAwait = true
setupScope.afterAwait = true
},
/** @param {CallExpression} node */
CallExpression(node) {
if (!scopeStack) {
return
}
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData || !setupFunctionData.afterAwait) {
const setupScope = setupScopes.get(scopeStack.scopeNode)
if (
!setupScope ||
!setupScope.afterAwait ||
!utils.inRange(setupScope.range, node)
) {
return
}

Expand All @@ -129,12 +140,34 @@ module.exports = {
messageId: 'forbidden'
})
}
}
},
(() => {
const scriptSetup = utils.getScriptSetupElement(context)
if (!scriptSetup) {
return {}
}
return {
/**
* @param {Program} node
*/
Program(node) {
setupScopes.set(node, {
afterAwait: false,
range: scriptSetup.range
})
}
}
})(),
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node) {
setupScopes.set(node, {
afterAwait: false,
range: node.range
})
},
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
':function:exit'(node) {
scopeStack = scopeStack && scopeStack.upper

setupFunctions.delete(node)
onSetupFunctionExit(node) {
setupScopes.delete(node)
}
})
)
Expand Down
57 changes: 57 additions & 0 deletions tests/lib/rules/no-watch-after-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,43 @@ tester.run('no-watch-after-await', rule, {
}
</script>
`
},
{
filename: 'test.vue',
code: `
<script setup>
import {watchEffect} from 'vue'
watchEffect(() => { /* ... */ })
await doSomething()
</script>
`,
parserOptions: { ecmaVersion: 2022 }
},
{
filename: 'test.vue',
code: `
<script setup>
await doSomething()
</script>
<script>
import {watchEffect} from 'vue'
watchEffect(() => { /* ... */ }) // not error
</script>
`,
parserOptions: { ecmaVersion: 2022 }
},
{
filename: 'test.vue',
code: `
<script setup>
</script>
<script>
import {watchEffect} from 'vue'
await doSomething()
watchEffect(() => { /* ... */ }) // not error
</script>
`,
parserOptions: { ecmaVersion: 2022 }
}
],
invalid: [
Expand Down Expand Up @@ -199,6 +236,26 @@ tester.run('no-watch-after-await', rule, {
line: 12
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
import {watch} from 'vue'
watch(foo, () => { /* ... */ })

await doSomething()

watch(foo, () => { /* ... */ })
</script>
`,
parserOptions: { ecmaVersion: 2022 },
errors: [
{
message: 'The `watch` after `await` expression are forbidden.',
line: 8
}
]
}
]
})