Skip to content
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

Fixed false positives inside the ternary operator in no-async-in-computed-properties #962

Merged
merged 2 commits into from Dec 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 19 additions & 33 deletions lib/rules/no-async-in-computed-properties.js
Expand Up @@ -73,7 +73,7 @@ module.exports = {

create (context) {
const forbiddenNodes = []
const allowedScopes = []
let scopeStack = { upper: null, body: null }

const expressionTypes = {
promise: 'asynchronous action',
Expand All @@ -87,65 +87,54 @@ module.exports = {
if (node.async) {
forbiddenNodes.push({
node: node,
type: 'async'
type: 'async',
targetBody: node.body
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}

scopeStack = { upper: scopeStack, body: node.body }
}

function onFunctionExit () {
scopeStack = scopeStack.upper
}
return Object.assign({},
{
FunctionDeclaration: onFunctionEnter,

FunctionExpression: onFunctionEnter,

ArrowFunctionExpression: onFunctionEnter,
':function': onFunctionEnter,
':function:exit': onFunctionExit,

NewExpression (node) {
if (node.callee.name === 'Promise') {
forbiddenNodes.push({
node: node,
type: 'new'
type: 'new',
targetBody: scopeStack.body
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
},

CallExpression (node) {
if (isPromise(node)) {
forbiddenNodes.push({
node: node,
type: 'promise'
type: 'promise',
targetBody: scopeStack.body
})
} else if (isTimedFunction(node)) {
forbiddenNodes.push({
node: node,
type: 'timed'
type: 'timed',
targetBody: scopeStack.body
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
},

AwaitExpression (node) {
forbiddenNodes.push({
node: node,
type: 'await'
type: 'await',
targetBody: scopeStack.body
})
},

'ReturnStatement' (node) {
if (
node.argument &&
(
node.argument.type === 'ObjectExpression' ||
node.argument.type === 'ArrayExpression'
)
) {
allowedScopes.push(node.argument)
}
}
},
utils.executeOnVue(context, (obj) => {
Expand All @@ -157,10 +146,7 @@ module.exports = {
cp.value &&
el.node.loc.start.line >= cp.value.loc.start.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]
)
el.targetBody === cp.value
) {
context.report({
node: el.node,
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-async-in-computed-properties.js
Expand Up @@ -181,6 +181,49 @@ ruleTester.run('no-async-in-computed-properties', rule, {
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return this.bar
? {
baz:() => Promise.resolve(1)
}
: {}
}
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return this.bar ? () => Promise.resolve(1) : null
}
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo() {
return this.bar ? async () => 1 : null
}
}
}
`,
parserOptions
}
],

Expand Down