Skip to content

Commit

Permalink
allow composable after own await
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Jun 13, 2023
1 parent 1d0b8cd commit 0fe2d4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/rules/composable-placement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ export default {
)
}

const functionStack: { node: Rule.Node; afterAwait: boolean }[] = []
const functionStack: { node: Rule.Node; firstAwait: Rule.Node | null }[] =
[]

return {
':function'(node: Rule.Node) {
functionStack.push({
node,
afterAwait: false,
firstAwait: null,
})
},

Expand All @@ -157,9 +158,12 @@ export default {
functionStack.pop()
},

AwaitExpression() {
AwaitExpression(node) {
if (functionStack.length > 0) {
functionStack[functionStack.length - 1]!.afterAwait = true
const current = functionStack[functionStack.length - 1]!
if (!current.firstAwait) {
current.firstAwait = node
}
}
},

Expand All @@ -169,10 +173,12 @@ export default {
return
}

const { afterAwait } = functionStack[functionStack.length - 1] ?? {
afterAwait: false,
const { firstAwait } = functionStack[functionStack.length - 1] ?? {
firstAwait: null,
}
if (afterAwait) {

// Forbidden composable after await but allow if the first await is for this CallExpression.
if (firstAwait && firstAwait !== node.parent) {
context.report({
node,
messageId: 'afterAwait',
Expand Down
27 changes: 27 additions & 0 deletions test/composable-placement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ describe('vue-composable/composable-placement', () => {
code: `
import { useFoo } from './foo'
export async function useBar() {
await useFoo()
}
`,
},
{
code: `
import { useFoo } from './foo'
export async function useBar() {
async function baz() {
await fetch()
Expand Down Expand Up @@ -255,6 +264,24 @@ describe('vue-composable/composable-placement', () => {
code: `
import { useFoo } from './foo'
export async function useBar() {
await fetch()
await useFoo()
}
`,
errors: [
{
messageId: 'afterAwait',
data: {
name: 'useFoo',
},
},
],
},
{
code: `
import { useFoo } from './foo'
export function useBar() {
function baz() {
useFoo()
Expand Down

0 comments on commit 0fe2d4a

Please sign in to comment.