eslint-plugin-no-multiple-returns
Version:
An ESLint plugin to enforce a single `return` statement per function, encouraging clearer and more maintainable code.
161 lines (160 loc) • 4.39 kB
JavaScript
function l(t) {
return t.callee.type === "Identifier" && t.callee.name === "useEffect";
}
function f(t) {
return t.type === "ArrowFunctionExpression" && t.body.type !== "BlockStatement" ? p(t.body) : t.body && t.body.type === "BlockStatement" ? b(t.body) : !1;
}
function p(t) {
return t.type === "CallExpression" && t.callee.type === "MemberExpression" && t.callee.object.type === "Identifier" && t.callee.object.name === "React" && t.callee.property.type === "Identifier" && t.callee.property.name === "createElement";
}
function b(t) {
for (const s of t.body)
if (s.type === "ReturnStatement" && s.argument && p(s.argument))
return !0;
return !1;
}
function c(t) {
let s = 0;
const r = /* @__PURE__ */ new Set(), o = [t];
for (; o.length > 0; ) {
const a = o.pop();
if (!a || r.has(a))
continue;
if (r.add(a), a.type === "ReturnStatement") {
s += 1;
continue;
}
const e = [];
switch (a.type) {
case "BlockStatement":
e.push("body");
break;
case "IfStatement":
e.push("test", "consequent", "alternate");
break;
case "WhileStatement":
case "DoWhileStatement":
e.push("test", "body");
break;
case "ForStatement":
e.push("init", "test", "update", "body");
break;
case "ForInStatement":
case "ForOfStatement":
e.push("left", "right", "body");
break;
case "SwitchStatement":
e.push("discriminant", "cases");
break;
case "SwitchCase":
e.push("test", "consequent");
break;
case "TryStatement":
e.push("block", "handler", "finalizer");
break;
case "CatchClause":
e.push("param", "body");
break;
case "ExpressionStatement":
e.push("expression");
break;
case "ConditionalExpression":
e.push("test", "consequent", "alternate");
break;
case "LogicalExpression":
case "BinaryExpression":
e.push("left", "right");
break;
case "UnaryExpression":
case "UpdateExpression":
e.push("argument");
break;
case "CallExpression":
case "NewExpression":
a.type === "CallExpression" && l(a) ? e.push("callee") : e.push("callee", "arguments");
break;
case "MemberExpression":
e.push("object", "property");
break;
case "ArrayExpression":
e.push("elements");
break;
case "ObjectExpression":
e.push("properties");
break;
case "Property":
e.push("key", "value");
break;
case "FunctionExpression":
case "ArrowFunctionExpression":
f(a) ? e.push("params") : e.push("params", "body");
break;
case "VariableDeclaration":
e.push("declarations");
break;
case "VariableDeclarator":
e.push("id", "init");
break;
case "AssignmentExpression":
e.push("left", "right");
break;
case "SequenceExpression":
e.push("expressions");
break;
}
for (const u of e) {
const n = a[u];
if (Array.isArray(n))
for (const i of n)
i && typeof i == "object" && i.type && o.push(i);
else n && typeof n == "object" && n.type && o.push(n);
}
}
return s;
}
const m = {
meta: {
type: "suggestion",
docs: {
description: "disallow multiple return statements in a function",
recommended: !1
},
schema: []
// no options
},
create(t) {
return {
FunctionDeclaration(s) {
const r = c(s.body);
r > 1 && t.report({
node: s,
message: `Function '${s.id && s.id.name || "<anonymous>"}' has ${r} return statements.`
});
},
FunctionExpression(s) {
const r = c(s.body);
r > 1 && t.report({
node: s,
message: `Function expression has ${r} return statements.`
});
},
ArrowFunctionExpression(s) {
if (s.body.type === "BlockStatement") {
const r = c(s.body);
r > 1 && t.report({
node: s,
message: `Arrow function has ${r} return statements.`
});
}
}
};
}
}, h = {
rules: {
"no-multiple-returns": m
}
};
export {
h as default
};
//# sourceMappingURL=index.es.js.map