Skip to content

Commit

Permalink
Fixed false positives inside the ternary operator in `no-async-in-com…
Browse files Browse the repository at this point in the history
…puted-properties` (#962)

* Fixed false positives inside the ternary operator in `no-async-in-computed-properties`

* Add testcase
  • Loading branch information
ota-meshi committed Dec 26, 2019
1 parent e8f130c commit b412783
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
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

0 comments on commit b412783

Please sign in to comment.