|
| 1 | +/** |
| 2 | + * @fileoverview Check if there are no asynchronous actions inside computed properties. |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +const PROMISE_FUNCTIONS = [ |
| 10 | + 'then', |
| 11 | + 'catch', |
| 12 | + 'finally' |
| 13 | +] |
| 14 | + |
| 15 | +const PROMISE_METHODS = [ |
| 16 | + 'all', |
| 17 | + 'race', |
| 18 | + 'reject', |
| 19 | + 'resolve' |
| 20 | +] |
| 21 | + |
| 22 | +const TIMED_FUNCTIONS = [ |
| 23 | + 'setTimeout', |
| 24 | + 'setInterval', |
| 25 | + 'setImmediate', |
| 26 | + 'requestAnimationFrame' |
| 27 | +] |
| 28 | + |
| 29 | +function isTimedFunction (node) { |
| 30 | + return ( |
| 31 | + node.type === 'CallExpression' && |
| 32 | + node.callee.type === 'Identifier' && |
| 33 | + TIMED_FUNCTIONS.indexOf(node.callee.name) !== -1 |
| 34 | + ) || ( |
| 35 | + node.type === 'CallExpression' && |
| 36 | + node.callee.type === 'MemberExpression' && |
| 37 | + node.callee.object.type === 'Identifier' && |
| 38 | + node.callee.object.name === 'window' && ( |
| 39 | + TIMED_FUNCTIONS.indexOf(node.callee.property.name) !== -1 |
| 40 | + ) |
| 41 | + ) && node.arguments.length |
| 42 | +} |
| 43 | + |
| 44 | +function isPromise (node) { |
| 45 | + if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression') { |
| 46 | + return ( // hello.PROMISE_FUNCTION() |
| 47 | + node.callee.property.type === 'Identifier' && |
| 48 | + PROMISE_FUNCTIONS.indexOf(node.callee.property.name) !== -1 |
| 49 | + ) || ( // Promise.PROMISE_METHOD() |
| 50 | + node.callee.object.type === 'Identifier' && |
| 51 | + node.callee.object.name === 'Promise' && |
| 52 | + PROMISE_METHODS.indexOf(node.callee.property.name) !== -1 |
| 53 | + ) |
| 54 | + } |
| 55 | + return false |
| 56 | +} |
| 57 | + |
| 58 | +function create (context) { |
| 59 | + const forbiddenNodes = [] |
| 60 | + |
| 61 | + const expressionTypes = { |
| 62 | + promise: 'asynchronous action', |
| 63 | + await: 'await operator', |
| 64 | + async: 'async function declaration', |
| 65 | + new: 'Promise object', |
| 66 | + timed: 'timed function' |
| 67 | + } |
| 68 | + |
| 69 | + function onFunctionEnter (node) { |
| 70 | + if (node.async) { |
| 71 | + forbiddenNodes.push({ |
| 72 | + node: node, |
| 73 | + type: 'async' |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return Object.assign({}, |
| 79 | + { |
| 80 | + FunctionDeclaration: onFunctionEnter, |
| 81 | + |
| 82 | + FunctionExpression: onFunctionEnter, |
| 83 | + |
| 84 | + ArrowFunctionExpression: onFunctionEnter, |
| 85 | + |
| 86 | + NewExpression (node) { |
| 87 | + if (node.callee.name === 'Promise') { |
| 88 | + forbiddenNodes.push({ |
| 89 | + node: node, |
| 90 | + type: 'new' |
| 91 | + }) |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + CallExpression (node) { |
| 96 | + if (isPromise(node)) { |
| 97 | + forbiddenNodes.push({ |
| 98 | + node: node, |
| 99 | + type: 'promise' |
| 100 | + }) |
| 101 | + } |
| 102 | + if (isTimedFunction(node)) { |
| 103 | + forbiddenNodes.push({ |
| 104 | + node: node, |
| 105 | + type: 'timed' |
| 106 | + }) |
| 107 | + } |
| 108 | + }, |
| 109 | + |
| 110 | + AwaitExpression (node) { |
| 111 | + forbiddenNodes.push({ |
| 112 | + node: node, |
| 113 | + type: 'await' |
| 114 | + }) |
| 115 | + } |
| 116 | + }, |
| 117 | + utils.executeOnVueComponent(context, (obj) => { |
| 118 | + const computedProperties = utils.getComputedProperties(obj) |
| 119 | + |
| 120 | + computedProperties.forEach(cp => { |
| 121 | + forbiddenNodes.forEach(el => { |
| 122 | + if ( |
| 123 | + cp.value && |
| 124 | + el.node.loc.start.line >= cp.value.loc.start.line && |
| 125 | + el.node.loc.end.line <= cp.value.loc.end.line |
| 126 | + ) { |
| 127 | + context.report({ |
| 128 | + node: el.node, |
| 129 | + message: 'Unexpected {{expressionName}} in "{{propertyName}}" computed property.', |
| 130 | + data: { |
| 131 | + expressionName: expressionTypes[el.type], |
| 132 | + propertyName: cp.key |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | + }) |
| 137 | + }) |
| 138 | + }) |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +// ------------------------------------------------------------------------------ |
| 143 | +// Rule Definition |
| 144 | +// ------------------------------------------------------------------------------ |
| 145 | + |
| 146 | +module.exports = { |
| 147 | + create, |
| 148 | + meta: { |
| 149 | + docs: { |
| 150 | + description: 'Check if there are no asynchronous actions inside computed properties.', |
| 151 | + category: 'Best Practices', |
| 152 | + recommended: false |
| 153 | + }, |
| 154 | + fixable: null, |
| 155 | + schema: [] |
| 156 | + } |
| 157 | +} |
0 commit comments