eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
94 lines (93 loc) • 4.09 kB
JavaScript
const require_index = require("../utils/index.js");
let _eslint_community_eslint_utils = require("@eslint-community/eslint-utils");
// lib/rules/valid-next-tick.ts
/**
* @fileoverview enforce valid `nextTick` function calls
* @author Flo Edelmann
* @copyright 2021 Flo Edelmann. All rights reserved.
* See LICENSE file in root directory for full license.
*/
function getVueNextTickNode(identifier, context) {
if (identifier.parent.type === "MemberExpression" && (identifier.name === "$nextTick" && require_index.default.isThis(identifier.parent.object, context) || identifier.name === "nextTick" && identifier.parent.object.type === "Identifier" && identifier.parent.object.name === "Vue")) return identifier.parent;
const variable = (0, _eslint_community_eslint_utils.findVariable)(require_index.default.getScope(context, identifier), identifier);
if (variable != null && variable.defs.length === 1) {
const def = variable.defs[0];
if (def.type === "ImportBinding" && def.node.type === "ImportSpecifier" && def.node.imported.type === "Identifier" && def.node.imported.name === "nextTick" && def.node.parent.type === "ImportDeclaration" && def.node.parent.source.value === "vue") return identifier;
}
}
function isAwaitedPromise(callExpression) {
switch (callExpression.parent.type) {
case "AwaitExpression": return true;
case "ReturnStatement": return true;
case "ArrowFunctionExpression": return callExpression.parent.body === callExpression;
case "MemberExpression": return callExpression.parent.property.type === "Identifier" && callExpression.parent.property.name === "then";
case "VariableDeclarator":
case "AssignmentExpression": return true;
case "ArrayExpression": return callExpression.parent.parent.type === "CallExpression" && callExpression.parent.parent.callee.type === "MemberExpression" && callExpression.parent.parent.callee.object.type === "Identifier" && callExpression.parent.parent.callee.object.name === "Promise" && callExpression.parent.parent.callee.property.type === "Identifier";
}
return false;
}
var valid_next_tick_default = {
meta: {
type: "problem",
docs: {
description: "enforce valid `nextTick` function calls",
categories: ["vue3-essential", "vue2-essential"],
url: "https://eslint.vuejs.org/rules/valid-next-tick.html"
},
fixable: "code",
hasSuggestions: true,
schema: [],
messages: {
shouldBeFunction: "`nextTick` is a function.",
missingCallbackOrAwait: "Await the Promise returned by `nextTick` or pass a callback function.",
addAwait: "Add missing `await` statement.",
tooManyParameters: "`nextTick` expects zero or one parameters.",
eitherAwaitOrCallback: "Either await the Promise or pass a callback function to `nextTick`."
}
},
create(context) {
return require_index.default.defineVueVisitor(context, { Identifier(node) {
const nextTickNode = getVueNextTickNode(node, context);
if (!nextTickNode || !nextTickNode.parent) return;
let parentNode = nextTickNode.parent;
if (parentNode.type === "ConditionalExpression") parentNode = parentNode.parent;
if (parentNode.type === "CallExpression" && parentNode.callee !== nextTickNode || parentNode.type === "VariableDeclarator" || parentNode.type === "AssignmentExpression") return;
if (parentNode.type !== "CallExpression") {
context.report({
node,
messageId: "shouldBeFunction",
fix(fixer) {
return fixer.insertTextAfter(node, "()");
}
});
return;
}
if (parentNode.arguments.length === 0) {
if (!isAwaitedPromise(parentNode)) context.report({
node,
messageId: "missingCallbackOrAwait",
suggest: [{
messageId: "addAwait",
fix(fixer) {
return fixer.insertTextBefore(parentNode, "await ");
}
}]
});
return;
}
if (parentNode.arguments.length > 1) {
context.report({
node,
messageId: "tooManyParameters"
});
return;
}
if (isAwaitedPromise(parentNode)) context.report({
node,
messageId: "eitherAwaitOrCallback"
});
} });
}
};
exports.default = valid_next_tick_default;