UNPKG

@arabasta/eslint-plugin-tsoa

Version:
66 lines (65 loc) 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@typescript-eslint/utils"); const utils_2 = require("../utils"); exports.default = (0, utils_2.createRule)({ name: 'no-jsdoc-example-for-complex-property-type', meta: { messages: { complexExamplesAreForbidden: "Using JSDoc '@example' on a complex property type is forbidden", }, type: 'problem', docs: { description: "ban complex interface property types from having JSDoc's `@example` declaration", recommended: true, requiresTypeChecking: true, }, schema: [ { type: 'object', additionalProperties: false, properties: { allowEnums: { type: 'boolean', description: "Whether to allow JSDoc's '@example' declaration for enum properties on interfaces.", }, }, }, ], }, defaultOptions: [ { allowEnums: true, }, ], create: (context, [{ allowEnums }]) => { const services = utils_1.ESLintUtils.getParserServices(context); const sourceCode = context.sourceCode || context.getSourceCode(); return { TSPropertySignature(node) { if (!node.typeAnnotation || node.typeAnnotation.typeAnnotation.type !== 'TSTypeReference') { return; } const propertyType = services.getTypeAtLocation(node); const { jsdoc } = (0, utils_2.parseJSDoc)(sourceCode, node); if (!jsdoc) { return; } const targetTagName = 'example'; const propertyExamples = jsdoc.tags.filter(({ tag }) => { return tag === targetTagName; }); if (allowEnums && (0, utils_2.isTypeEnum)(propertyType)) { return; } if (propertyExamples.length > 0) { context.report({ node, messageId: 'complexExamplesAreForbidden', }); } }, }; }, });