eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
204 lines (203 loc) • 7.36 kB
JavaScript
const require_index = require("../utils/index.js");
let _eslint_community_eslint_utils = require("@eslint-community/eslint-utils");
// lib/rules/no-async-in-computed-properties.ts
/**
* @fileoverview Check if there are no asynchronous actions inside computed properties.
* @author Armano
*/
const PROMISE_FUNCTIONS = /* @__PURE__ */ new Set([
"then",
"catch",
"finally"
]);
const PROMISE_METHODS = /* @__PURE__ */ new Set([
"all",
"allSettled",
"any",
"race",
"reject",
"resolve",
"try",
"withResolvers"
]);
const TIMED_FUNCTIONS = /* @__PURE__ */ new Set([
"setTimeout",
"setInterval",
"setImmediate",
"requestAnimationFrame"
]);
function isTimedFunction(node) {
const callee = require_index.default.skipChainExpression(node.callee);
return (callee.type === "Identifier" && TIMED_FUNCTIONS.has(callee.name) || callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "window" && TIMED_FUNCTIONS.has(require_index.default.getStaticPropertyName(callee) || "")) && node.arguments.length > 0;
}
function skipWrapper(node) {
while (node && node.expression) node = node.expression;
return node;
}
/**
* Resolve a single step of the member expression chain walk.
*/
function resolveRootObjectStep(current) {
switch (current.type) {
case "MemberExpression": return {
done: false,
next: skipWrapper(current.object)
};
case "CallExpression": {
const calleeExpr = skipWrapper(current.callee);
if (calleeExpr.type === "MemberExpression") return {
done: false,
next: skipWrapper(calleeExpr.object)
};
return {
done: true,
name: calleeExpr.type === "Identifier" ? calleeExpr.name : null
};
}
case "Identifier": return {
done: true,
name: current.name
};
default: return {
done: true,
name: null
};
}
}
/**
* Get the root object name from a member expression chain
*/
function getRootObjectName(memberExpr) {
let current = skipWrapper(memberExpr.object);
while (current) {
const step = resolveRootObjectStep(current);
if (step.done) return step.name;
current = step.next;
}
return null;
}
function isPromiseMethod(name, callee) {
return PROMISE_FUNCTIONS.has(name) || callee.object.type === "Identifier" && callee.object.name === "Promise" && PROMISE_METHODS.has(name);
}
function isPromise(node, ignoredObjectNames) {
const callee = require_index.default.skipChainExpression(node.callee);
if (callee.type === "MemberExpression") {
const name = require_index.default.getStaticPropertyName(callee);
if (!name || !isPromiseMethod(name, callee)) return false;
const rootObjectName = getRootObjectName(callee);
return !rootObjectName || !ignoredObjectNames.has(rootObjectName);
}
return false;
}
function isNextTick(node, context) {
const callee = require_index.default.skipChainExpression(node.callee);
if (callee.type === "MemberExpression") {
const name = require_index.default.getStaticPropertyName(callee);
return require_index.default.isThis(callee.object, context) && name === "$nextTick" || callee.object.type === "Identifier" && callee.object.name === "Vue" && name === "nextTick";
}
return false;
}
var no_async_in_computed_properties_default = {
meta: {
type: "problem",
docs: {
description: "disallow asynchronous actions in computed properties",
categories: ["vue3-essential", "vue2-essential"],
url: "https://eslint.vuejs.org/rules/no-async-in-computed-properties.html"
},
fixable: null,
schema: [{
type: "object",
properties: { ignoredObjectNames: {
type: "array",
items: { type: "string" },
uniqueItems: true
} },
additionalProperties: false
}],
messages: {
unexpectedInFunction: "Unexpected {{expressionName}} in computed function.",
unexpectedInProperty: "Unexpected {{expressionName}} in \"{{propertyName}}\" computed property."
}
},
create(context) {
const options = context.options[0] || {};
const ignoredObjectNames = new Set(options.ignoredObjectNames || []);
const computedPropertiesMap = /* @__PURE__ */ new Map();
const computedFunctionNodes = [];
let scopeStack = null;
const expressionTypes = {
promise: "asynchronous action",
nextTick: "asynchronous action",
await: "await operator",
async: "async function declaration",
new: "Promise object",
timed: "timed function"
};
function onFunctionEnter(node, info) {
if (node.async) verify(node, node.body, "async", info ? computedPropertiesMap.get(info.node) : null);
scopeStack = {
upper: scopeStack,
body: node.body
};
}
function onFunctionExit() {
scopeStack &&= scopeStack.upper;
}
function verify(node, targetBody, type, computedProperties) {
const properties = computedProperties || [];
for (const cp of properties) if (cp.value && node.loc.start.line >= cp.value.loc.start.line && node.loc.end.line <= cp.value.loc.end.line && targetBody === cp.value) {
context.report({
node,
messageId: "unexpectedInProperty",
data: {
expressionName: expressionTypes[type],
propertyName: cp.key || "unknown"
}
});
return;
}
for (const cf of computedFunctionNodes) if (node.loc.start.line >= cf.body.loc.start.line && node.loc.end.line <= cf.body.loc.end.line && targetBody === cf.body) {
context.report({
node,
messageId: "unexpectedInFunction",
data: { expressionName: expressionTypes[type] }
});
return;
}
}
const nodeVisitor = {
":function": onFunctionEnter,
":function:exit": onFunctionExit,
NewExpression(node, info) {
if (!scopeStack) return;
if (node.callee.type === "Identifier" && node.callee.name === "Promise") verify(node, scopeStack.body, "new", info ? computedPropertiesMap.get(info.node) : null);
},
CallExpression(node, info) {
if (!scopeStack) return;
if (isPromise(node, ignoredObjectNames)) verify(node, scopeStack.body, "promise", info ? computedPropertiesMap.get(info.node) : null);
else if (isTimedFunction(node)) verify(node, scopeStack.body, "timed", info ? computedPropertiesMap.get(info.node) : null);
else if (isNextTick(node, context)) verify(node, scopeStack.body, "nextTick", info ? computedPropertiesMap.get(info.node) : null);
},
AwaitExpression(node, info) {
if (!scopeStack) return;
verify(node, scopeStack.body, "await", info ? computedPropertiesMap.get(info.node) : null);
}
};
return require_index.default.compositingVisitors({ Program(program) {
const tracker = new _eslint_community_eslint_utils.ReferenceTracker(require_index.default.getScope(context, program));
const computedRefs = require_index.default.iterateReferencesTraceMap(tracker, { computed: { [_eslint_community_eslint_utils.ReferenceTracker.CALL]: true } });
for (const { node } of computedRefs) {
if (node.type !== "CallExpression") continue;
const getter = require_index.default.getGetterBodyFromComputedFunction(node);
if (getter) computedFunctionNodes.push(getter);
}
} }, require_index.default.isScriptSetup(context) ? require_index.default.defineScriptSetupVisitor(context, nodeVisitor) : require_index.default.defineVueVisitor(context, {
onVueObjectEnter(node) {
computedPropertiesMap.set(node, require_index.default.getComputedProperties(node));
},
...nodeVisitor
}));
}
};
exports.default = no_async_in_computed_properties_default;