@vanta-inc/eslint-plugin-vanta
Version:
Custom ESLint rules for Vanta
55 lines (54 loc) • 2.12 kB
JavaScript
;
/**
* @fileoverview Ensure that all mutations return a unique type
*/
Object.defineProperty(exports, "__esModule", { value: true });
var rule = {
meta: {
type: "suggestion",
docs: {
// @ts-ignore
description: "All mutations must return a unique type",
category: "Operations",
url: "https://github.com/VantaInc/eslint-plugin-vanta/blob/main/docs/rules/mutations-payloads-unique.md",
},
},
create: function (context) {
var validatePayload = function (node) {
var _a;
var typeNames = new Set();
(_a = node.rawNode().definitions) === null || _a === void 0 ? void 0 : _a.forEach(function (definition) {
var _a;
if (definition.kind === "ObjectTypeDefinition" ||
definition.kind === "ObjectTypeExtension") {
if (definition.name.value !== "Mutation") {
return;
}
(_a = definition.fields) === null || _a === void 0 ? void 0 : _a.forEach(function (field) {
if (field.type.kind === "ListType" ||
field.type.kind === "NonNullType") {
return;
}
var payloadTypeName = field.type.name.value;
if (typeNames.has(payloadTypeName)) {
context.report({
node: node,
message: "Mutation payload types must be unique; reused type {{type}}",
data: {
type: payloadTypeName,
},
});
return;
}
typeNames.add(payloadTypeName);
});
}
});
};
return {
Document: validatePayload,
};
},
};
module.exports = rule;
exports.default = rule;