@vanta-inc/eslint-plugin-vanta
Version:
Custom ESLint rules for Vanta
65 lines (64 loc) • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var rule = {
meta: {
type: "suggestion",
docs: {
// @ts-ignore
description: "Payloads are unions of a success type and one or more error types",
category: "Operations",
url: "https://github.com/VantaInc/eslint-plugin-vanta/blob/main/docs/rules/payloads-are-unions.md",
},
},
create: function (context) {
var validateDefinitionPayload = function (node) {
var _a, _b, _c, _d;
if (node.name.value.endsWith("Payload")) {
if (node.type !== "UnionTypeDefinition") {
context.report({
node: node,
message: "Types that end with 'Payload' must be unions",
});
return;
}
var successTypes = (_b = (_a = node.types) === null || _a === void 0 ? void 0 : _a.filter(function (type) { return type.name.value.endsWith("Success"); })) !== null && _b !== void 0 ? _b : [];
if (successTypes.length !== 1) {
context.report({
node: node,
message: "Payloads must include exactly one 'Success' type, found {{count}}",
data: {
count: "" + successTypes.length,
},
});
}
if (!((_c = node.types) === null || _c === void 0 ? void 0 : _c.some(function (type) { return type.name.value === "BaseUserError"; }))) {
context.report({
node: node,
message: "Payloads must include BaseUserError",
});
}
if (!((_d = node.types) === null || _d === void 0 ? void 0 : _d.every(function (type) {
return type.name.value.endsWith("Success") ||
type.name.value.endsWith("Error");
}))) {
context.report({
node: node,
message: "Payloads should be unions of only Success and Error types",
});
}
}
};
// lint all kinds that fall under TypeDefinitionNode
// sadly TypeDefinitionNode: validateDefinitionPayload doesn't work
return {
ScalarTypeDefinition: validateDefinitionPayload,
ObjectTypeDefinition: validateDefinitionPayload,
InterfaceTypeDefinition: validateDefinitionPayload,
UnionTypeDefinition: validateDefinitionPayload,
EnumTypeDefinition: validateDefinitionPayload,
InputObjectTypeDefinition: validateDefinitionPayload,
};
},
};
module.exports = rule;
exports.default = rule;