Skip to content

Commit eefb98f

Browse files
authored
fix(no-async-in-computed-properties): Allow async in nested scopes (#745)
1 parent d550054 commit eefb98f

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

lib/rules/no-async-in-computed-properties.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = {
7373

7474
create (context) {
7575
const forbiddenNodes = []
76+
const allowedScopes = []
7677

7778
const expressionTypes = {
7879
promise: 'asynchronous action',
@@ -88,6 +89,8 @@ module.exports = {
8889
node: node,
8990
type: 'async'
9091
})
92+
} else if (node.parent.type === 'ReturnStatement') {
93+
allowedScopes.push(node)
9194
}
9295
}
9396

@@ -105,6 +108,8 @@ module.exports = {
105108
node: node,
106109
type: 'new'
107110
})
111+
} else if (node.parent.type === 'ReturnStatement') {
112+
allowedScopes.push(node)
108113
}
109114
},
110115

@@ -114,12 +119,13 @@ module.exports = {
114119
node: node,
115120
type: 'promise'
116121
})
117-
}
118-
if (isTimedFunction(node)) {
122+
} else if (isTimedFunction(node)) {
119123
forbiddenNodes.push({
120124
node: node,
121125
type: 'timed'
122126
})
127+
} else if (node.parent.type === 'ReturnStatement') {
128+
allowedScopes.push(node)
123129
}
124130
},
125131

@@ -128,6 +134,15 @@ module.exports = {
128134
node: node,
129135
type: 'await'
130136
})
137+
},
138+
139+
'ReturnStatement' (node) {
140+
if (
141+
node.argument.type === 'ObjectExpression' ||
142+
node.argument.type === 'ArrayExpression'
143+
) {
144+
allowedScopes.push(node.argument)
145+
}
131146
}
132147
},
133148
utils.executeOnVue(context, (obj) => {
@@ -138,7 +153,11 @@ module.exports = {
138153
if (
139154
cp.value &&
140155
el.node.loc.start.line >= cp.value.loc.start.line &&
141-
el.node.loc.end.line <= cp.value.loc.end.line
156+
el.node.loc.end.line <= cp.value.loc.end.line &&
157+
!allowedScopes.some(scope =>
158+
scope.range[0] < el.node.range[0] &&
159+
scope.range[1] > el.node.range[1]
160+
)
142161
) {
143162
context.report({
144163
node: el.node,

tests/lib/rules/no-async-in-computed-properties.js

+97
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,103 @@ ruleTester.run('no-async-in-computed-properties', rule, {
7171
}
7272
`,
7373
parserOptions
74+
},
75+
{
76+
filename: 'test.vue',
77+
code: `
78+
export default {
79+
computed: {
80+
foo() {
81+
return {
82+
async bar() {
83+
const data = await baz(this.a)
84+
return data
85+
}
86+
}
87+
}
88+
}
89+
}
90+
`,
91+
parserOptions
92+
},
93+
{
94+
filename: 'test.vue',
95+
code: `
96+
export default {
97+
computed: {
98+
foo() {
99+
const a = 'test'
100+
return [
101+
async () => {
102+
const baz = await bar(a)
103+
return baz
104+
},
105+
'b',
106+
{}
107+
]
108+
}
109+
}
110+
}
111+
`,
112+
parserOptions
113+
},
114+
{
115+
filename: 'test.vue',
116+
code: `
117+
export default {
118+
computed: {
119+
foo() {
120+
return function () {
121+
return async () => await bar()
122+
}
123+
},
124+
}
125+
}
126+
`,
127+
parserOptions
128+
},
129+
{
130+
filename: 'test.vue',
131+
code: `
132+
export default {
133+
computed: {
134+
foo() {
135+
return new Promise.resolve()
136+
},
137+
}
138+
}
139+
`,
140+
parserOptions
141+
},
142+
{
143+
filename: 'test.vue',
144+
code: `
145+
export default {
146+
computed: {
147+
foo() {
148+
return new Bar(async () => await baz())
149+
},
150+
}
151+
}
152+
`,
153+
parserOptions
154+
},
155+
{
156+
filename: 'test.vue',
157+
code: `
158+
export default {
159+
computed: {
160+
foo() {
161+
return someFunc.doSomething({
162+
async bar() {
163+
return await baz()
164+
}
165+
})
166+
},
167+
}
168+
}
169+
`,
170+
parserOptions
74171
}
75172
],
76173

0 commit comments

Comments
 (0)