Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 2x 2x 13x 16x 2x 5x 5x 5x 4x 8x 2x 8x 8x 8x 8x 5x 5x 5x 8x 3x | import type { Rule } from "eslint";
type CallExpression = Extract<Rule.Node, { type: "CallExpression" }>;
type Properties = Extract<Rule.Node, { type: "ObjectExpression" }>["properties"];
const REQUIRED_KEYS = ["tags", "summary"];
const findProperty = (properties: Properties, name: string) =>
properties.find(
(property) =>
property.type === "Property" &&
((property.key.type === "Identifier" && property.key.name === name) ||
(property.key.type === "Literal" && property.key.value === name)),
);
const describesOpenApi = ({ arguments: [meta] }: CallExpression): boolean => {
Iif (meta?.type !== "ObjectExpression") return false;
const openApi = findProperty(meta.properties, "openAPI");
if (openApi?.type !== "Property" || openApi.value.type !== "ObjectExpression") return false;
const { properties } = openApi.value;
return REQUIRED_KEYS.every((key) => findProperty(properties, key));
};
const requireRouteMeta: Rule.RuleModule = {
meta: {
type: "problem",
docs: {
description:
"Require a top-level `defineRouteMeta` with `openAPI.tags` and `summary`, below the handler.",
},
schema: [],
messages: {
missing:
"Describe this route with a top-level `defineRouteMeta({ openAPI: { … } })`, so its OpenAPI entry has a tag and a summary.",
incomplete:
"Give `defineRouteMeta` an `openAPI` object literal with `tags` and `summary`.",
beforeHandler:
"Move `defineRouteMeta` below the `export default` handler, so the route reads handler first.",
},
},
create: (context) => {
let found = false;
let handlerSeen = false;
return {
"Program > ExportDefaultDeclaration": () => {
handlerSeen = true;
},
"Program > ExpressionStatement > CallExpression[callee.name='defineRouteMeta']": (
node: CallExpression,
) => {
found = true;
if (!describesOpenApi(node)) context.report({ node, messageId: "incomplete" });
if (!handlerSeen) context.report({ node, messageId: "beforeHandler" });
},
"Program:exit": (node) => {
if (!found) {
context.report({ node, loc: { line: 1, column: 0 }, messageId: "missing" });
}
},
};
},
};
export default requireRouteMeta;
|