Skip to content

Add no-async-in-computed-properties rule #72

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
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
60 changes: 60 additions & 0 deletions docs/rules/no-async-in-computed-properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Check if there are no asynchronous actions inside computed properties (no-async-in-computed-properties)

Computed properties should be synchronous. Asynchronous actions inside them may not work as expected and can lead to an unexpected behaviour, that's why you should avoid them.
If you need async computed properties you might want to consider using additional plugin [vue-async-computed]

## :book: Rule Details

This rule is aimed at preventing asynchronous methods from being called in computed properties.

:-1: Examples of **incorrect** code for this rule:

```js
export default {
computed: {
pro () {
return Promise.all([new Promise((resolve, reject) => {})])
},
foo: async function () {
return await someFunc()
},
bar () {
return fetch(url).then(response => {})
},
tim () {
setTimeout(() => { }, 0)
},
inter () {
setInterval(() => { }, 0)
},
anim () {
requestAnimationFrame(() => {})
}
}
}
```

:+1: Examples of **correct** code for this rule:

```js
export default {
computed: {
foo () {
var bar = 0
try {
bar = bar / this.a
} catch (e) {
return 0
} finally {
return bar
}
}
}
}
```

## :wrench: Options

Nothing.

[vue-async-computed]: https://github.com/foxbenjaminfox/vue-async-computed
157 changes: 157 additions & 0 deletions lib/rules/no-async-in-computed-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* @fileoverview Check if there are no asynchronous actions inside computed properties.
* @author Armano
*/
'use strict'

const utils = require('../utils')

const PROMISE_FUNCTIONS = [
'then',
'catch',
'finally'
]

const PROMISE_METHODS = [
'all',
'race',
'reject',
'resolve'
]

const TIMED_FUNCTIONS = [
'setTimeout',
'setInterval',
'setImmediate',
'requestAnimationFrame'
]

function isTimedFunction (node) {
return (
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
TIMED_FUNCTIONS.indexOf(node.callee.name) !== -1
) || (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'window' && (
TIMED_FUNCTIONS.indexOf(node.callee.property.name) !== -1
)
) && node.arguments.length
}

function isPromise (node) {
if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression') {
return ( // hello.PROMISE_FUNCTION()
node.callee.property.type === 'Identifier' &&
PROMISE_FUNCTIONS.indexOf(node.callee.property.name) !== -1
) || ( // Promise.PROMISE_METHOD()
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'Promise' &&
PROMISE_METHODS.indexOf(node.callee.property.name) !== -1
)
}
return false
}

function create (context) {
const forbiddenNodes = []

const expressionTypes = {
promise: 'asynchronous action',
await: 'await operator',
async: 'async function declaration',
new: 'Promise object',
timed: 'timed function'
}

function onFunctionEnter (node) {
if (node.async) {
forbiddenNodes.push({
node: node,
type: 'async'
})
}
}

return Object.assign({},
{
FunctionDeclaration: onFunctionEnter,

FunctionExpression: onFunctionEnter,

ArrowFunctionExpression: onFunctionEnter,

NewExpression (node) {
if (node.callee.name === 'Promise') {
forbiddenNodes.push({
node: node,
type: 'new'
})
}
},

CallExpression (node) {
if (isPromise(node)) {
forbiddenNodes.push({
node: node,
type: 'promise'
})
}
if (isTimedFunction(node)) {
forbiddenNodes.push({
node: node,
type: 'timed'
})
}
},

AwaitExpression (node) {
forbiddenNodes.push({
node: node,
type: 'await'
})
}
},
utils.executeOnVueComponent(context, (obj) => {
const computedProperties = utils.getComputedProperties(obj)

computedProperties.forEach(cp => {
forbiddenNodes.forEach(el => {
if (
cp.value &&
el.node.loc.start.line >= cp.value.loc.start.line &&
el.node.loc.end.line <= cp.value.loc.end.line
) {
context.report({
node: el.node,
message: 'Unexpected {{expressionName}} in "{{propertyName}}" computed property.',
data: {
expressionName: expressionTypes[el.type],
propertyName: cp.key
}
})
}
})
})
})
)
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
create,
meta: {
docs: {
description: 'Check if there are no asynchronous actions inside computed properties.',
category: 'Best Practices',
recommended: false
},
fixable: null,
schema: []
}
}
1 change: 1 addition & 0 deletions lib/rules/no-side-effects-in-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function create (context) {
computedProperties.forEach(cp => {
forbiddenNodes.forEach(node => {
if (
cp.value &&
node.loc.start.line >= cp.value.loc.start.line &&
node.loc.end.line <= cp.value.loc.end.line
) {
Expand Down
Loading