@arabasta/eslint-plugin-tsoa
Version:
ESLint plugin for tsoa rules
49 lines (48 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_js_1 = require("../utils.js");
exports.default = (0, utils_js_1.createRule)({
name: 'require-example-decorator',
meta: {
messages: {
missingRequiredDecorators: "The '@Example' decorator is required when the method returns an array",
},
type: 'problem',
docs: {
description: 'require the `@Example` decorator on methods that return an array',
recommended: true,
},
schema: [],
},
defaultOptions: [],
create(context) {
function getMethodDefinitionReturnType(methodDefinitionNode) {
var _a, _b;
if (!((_b = (_a = methodDefinitionNode === null || methodDefinitionNode === void 0 ? void 0 : methodDefinitionNode.value) === null || _a === void 0 ? void 0 : _a.returnType) === null || _b === void 0 ? void 0 : _b.typeAnnotation)) {
return null;
}
const typeAnnotation = methodDefinitionNode.value.returnType.typeAnnotation;
if (typeAnnotation.type === 'TSTypeReference' &&
'name' in typeAnnotation.typeName &&
typeAnnotation.typeName.name === 'Promise' &&
typeAnnotation.typeArguments) {
return typeAnnotation.typeArguments.params[0];
}
return typeAnnotation;
}
return {
MethodDefinition(node) {
const returnType = getMethodDefinitionReturnType(node);
if (returnType &&
returnType.type === 'TSArrayType' &&
(0, utils_js_1.hasHttpMethodDecorator)(node) &&
!(0, utils_js_1.hasDecoratorWithName)({ node, decoratorName: 'Example' })) {
context.report({
node,
messageId: 'missingRequiredDecorators',
});
}
},
};
},
});