Skip to content

fix(no-async-in-computed-properties): Allow async in nested scopes #745

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
Jan 3, 2019
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
25 changes: 22 additions & 3 deletions lib/rules/no-async-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {

create (context) {
const forbiddenNodes = []
const allowedScopes = []

const expressionTypes = {
promise: 'asynchronous action',
Expand All @@ -88,6 +89,8 @@ module.exports = {
node: node,
type: 'async'
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
}

Expand All @@ -105,6 +108,8 @@ module.exports = {
node: node,
type: 'new'
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
},

Expand All @@ -114,12 +119,13 @@ module.exports = {
node: node,
type: 'promise'
})
}
if (isTimedFunction(node)) {
} else if (isTimedFunction(node)) {
forbiddenNodes.push({
node: node,
type: 'timed'
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
},

Expand All @@ -128,6 +134,15 @@ module.exports = {
node: node,
type: 'await'
})
},

'ReturnStatement' (node) {
if (
node.argument.type === 'ObjectExpression' ||
node.argument.type === 'ArrayExpression'
) {
allowedScopes.push(node.argument)
}
}
},
utils.executeOnVue(context, (obj) => {
Expand All @@ -138,7 +153,11 @@ module.exports = {
if (
cp.value &&
el.node.loc.start.line >= cp.value.loc.start.line &&
el.node.loc.end.line <= cp.value.loc.end.line
el.node.loc.end.line <= cp.value.loc.end.line &&
!allowedScopes.some(scope =>
scope.range[0] < el.node.range[0] &&
scope.range[1] > el.node.range[1]
)
) {
context.report({
node: el.node,
Expand Down
97 changes: 97 additions & 0 deletions tests/lib/rules/no-async-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,103 @@ ruleTester.run('no-async-in-computed-properties', rule, {
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return {
async bar() {
const data = await baz(this.a)
return data
}
}
}
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
const a = 'test'
return [
async () => {
const baz = await bar(a)
return baz
},
'b',
{}
]
}
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return function () {
return async () => await bar()
}
},
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return new Promise.resolve()
},
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return new Bar(async () => await baz())
},
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return someFunc.doSomething({
async bar() {
return await baz()
}
})
},
}
}
`,
parserOptions
}
],

Expand Down