|
| 1 | +/** |
| 2 | + * @fileoverview enforce Promise or callback style in `nextTick` |
| 3 | + * @author Flo Edelmann |
| 4 | + * @copyright 2020 Flo Edelmann. All rights reserved. |
| 5 | + * See LICENSE file in root directory for full license. |
| 6 | + */ |
| 7 | +'use strict' |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Requirements |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +const utils = require('../utils') |
| 14 | +const { findVariable } = require('eslint-utils') |
| 15 | + |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | +// Helpers |
| 18 | +// ------------------------------------------------------------------------------ |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {Identifier} identifier |
| 22 | + * @param {RuleContext} context |
| 23 | + * @returns {CallExpression|undefined} |
| 24 | + */ |
| 25 | +function getVueNextTickCallExpression(identifier, context) { |
| 26 | + // Instance API: this.$nextTick() |
| 27 | + if ( |
| 28 | + identifier.name === '$nextTick' && |
| 29 | + identifier.parent.type === 'MemberExpression' && |
| 30 | + utils.isThis(identifier.parent.object, context) && |
| 31 | + identifier.parent.parent.type === 'CallExpression' && |
| 32 | + identifier.parent.parent.callee === identifier.parent |
| 33 | + ) { |
| 34 | + return identifier.parent.parent |
| 35 | + } |
| 36 | + |
| 37 | + // Vue 2 Global API: Vue.nextTick() |
| 38 | + if ( |
| 39 | + identifier.name === 'nextTick' && |
| 40 | + identifier.parent.type === 'MemberExpression' && |
| 41 | + identifier.parent.object.type === 'Identifier' && |
| 42 | + identifier.parent.object.name === 'Vue' && |
| 43 | + identifier.parent.parent.type === 'CallExpression' && |
| 44 | + identifier.parent.parent.callee === identifier.parent |
| 45 | + ) { |
| 46 | + return identifier.parent.parent |
| 47 | + } |
| 48 | + |
| 49 | + // Vue 3 Global API: import { nextTick as nt } from 'vue'; nt() |
| 50 | + if ( |
| 51 | + identifier.parent.type === 'CallExpression' && |
| 52 | + identifier.parent.callee === identifier |
| 53 | + ) { |
| 54 | + const variable = findVariable(context.getScope(), identifier) |
| 55 | + |
| 56 | + if (variable != null && variable.defs.length === 1) { |
| 57 | + const def = variable.defs[0] |
| 58 | + if ( |
| 59 | + def.type === 'ImportBinding' && |
| 60 | + def.node.type === 'ImportSpecifier' && |
| 61 | + def.node.imported.type === 'Identifier' && |
| 62 | + def.node.imported.name === 'nextTick' && |
| 63 | + def.node.parent.type === 'ImportDeclaration' && |
| 64 | + def.node.parent.source.value === 'vue' |
| 65 | + ) { |
| 66 | + return identifier.parent |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return undefined |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @param {CallExpression} callExpression |
| 76 | + * @returns {boolean} |
| 77 | + */ |
| 78 | +function isAwaitedPromise(callExpression) { |
| 79 | + return ( |
| 80 | + callExpression.parent.type === 'AwaitExpression' || |
| 81 | + (callExpression.parent.type === 'MemberExpression' && |
| 82 | + callExpression.parent.property.type === 'Identifier' && |
| 83 | + callExpression.parent.property.name === 'then') |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +// ------------------------------------------------------------------------------ |
| 88 | +// Rule Definition |
| 89 | +// ------------------------------------------------------------------------------ |
| 90 | + |
| 91 | +module.exports = { |
| 92 | + meta: { |
| 93 | + type: 'suggestion', |
| 94 | + docs: { |
| 95 | + description: 'enforce Promise or callback style in `nextTick`', |
| 96 | + categories: undefined, |
| 97 | + url: 'https://eslint.vuejs.org/rules/next-tick-style.html' |
| 98 | + }, |
| 99 | + fixable: 'code', |
| 100 | + schema: [{ enum: ['promise', 'callback'] }] |
| 101 | + }, |
| 102 | + /** @param {RuleContext} context */ |
| 103 | + create(context) { |
| 104 | + const preferredStyle = |
| 105 | + /** @type {string|undefined} */ (context.options[0]) || 'promise' |
| 106 | + |
| 107 | + return utils.defineVueVisitor(context, { |
| 108 | + /** @param {Identifier} node */ |
| 109 | + Identifier(node) { |
| 110 | + const callExpression = getVueNextTickCallExpression(node, context) |
| 111 | + if (!callExpression) { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + if (preferredStyle === 'callback') { |
| 116 | + if ( |
| 117 | + callExpression.arguments.length !== 1 || |
| 118 | + isAwaitedPromise(callExpression) |
| 119 | + ) { |
| 120 | + context.report({ |
| 121 | + node, |
| 122 | + message: |
| 123 | + 'Pass a callback function to `nextTick` instead of using the returned Promise.' |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + if ( |
| 131 | + callExpression.arguments.length !== 0 || |
| 132 | + !isAwaitedPromise(callExpression) |
| 133 | + ) { |
| 134 | + context.report({ |
| 135 | + node, |
| 136 | + message: |
| 137 | + 'Use the Promise returned by `nextTick` instead of passing a callback function.', |
| 138 | + fix(fixer) { |
| 139 | + return fixer.insertTextAfter(node, '().then') |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments