UNPKG

@arabasta/eslint-plugin-tsoa

Version:
130 lines (129 loc) 4.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRule = void 0; exports.parseJSDoc = parseJSDoc; exports.isTypeEnum = isTypeEnum; exports.hasHttpMethodDecorator = hasHttpMethodDecorator; exports.getFullFunctionName = getFullFunctionName; exports.hasDecoratorWithName = hasDecoratorWithName; exports.hasResponseDecoratorWithStatus = hasResponseDecoratorWithStatus; exports.getAllDecoratorsWithName = getAllDecoratorsWithName; const jsdoccomment_1 = require("@es-joy/jsdoccomment"); const utils_1 = require("@typescript-eslint/utils"); const typescript_1 = __importDefault(require("typescript")); const PLUGIN_DOCS_URL = 'https://github.com/CloudNStoyan/arabasta/blob/main/eslint-plugin-tsoa/docs/rules'; exports.createRule = utils_1.ESLintUtils.RuleCreator((name) => `${PLUGIN_DOCS_URL}/${name}.md`); const HTTP_METHOD_DECORATOR_NAMES = [ 'Options', 'Get', 'Post', 'Put', 'Patch', 'Delete', 'Head', ]; function parseJSDoc(sourceCode, node) { const jsdocNode = (0, jsdoccomment_1.getJSDocComment)(sourceCode, node, { maxLines: 1, minLines: 0, }); if (!jsdocNode) { return { jsdoc: null, jsdocNode: null }; } const jsdoc = (0, jsdoccomment_1.parseComment)(jsdocNode); return { jsdoc, jsdocNode: jsdocNode }; } function isTypeEnum(type) { var _a; return ((_a = type.symbol) === null || _a === void 0 ? void 0 : _a.flags) & typescript_1.default.SymbolFlags.Enum; } function hasHttpMethodDecorator(node) { for (const httpMethodDecoratorName of HTTP_METHOD_DECORATOR_NAMES) { if (hasDecoratorWithName({ node, decoratorName: httpMethodDecoratorName })) { return true; } } return false; } function getFullFunctionName(node) { if (node.type === 'Identifier') { return node.name; } const functionPath = []; let currentNode = node; while (currentNode.type === 'MemberExpression' && currentNode.object) { if ('name' in currentNode.property) { functionPath.push(currentNode.property.name); } currentNode = currentNode.object; } if (currentNode.type === 'Identifier' && currentNode.name) { functionPath.push(currentNode.name); } if (currentNode.type === 'ThisExpression') { functionPath.push('this'); } functionPath.reverse(); const functionName = functionPath.join('.'); return functionName; } function hasDecoratorWithName({ node, decoratorName, }) { var _a; if (!Array.isArray(node === null || node === void 0 ? void 0 : node.decorators)) { return false; } for (const decorator of node.decorators) { if ((decorator === null || decorator === void 0 ? void 0 : decorator.type) !== 'Decorator') { continue; } if (decorator.expression.type === 'Identifier' && decorator.expression.name === decoratorName) { return true; } if (decorator.expression.type === 'CallExpression' && ((_a = decorator.expression.callee) === null || _a === void 0 ? void 0 : _a.type) === 'Identifier' && decorator.expression.callee.name === decoratorName) { return true; } } return false; } function hasResponseDecoratorWithStatus({ status, decorators, }) { for (const decorator of decorators) { if (decorator.expression.type !== 'CallExpression') { continue; } const decoratorArguments = decorator.expression.arguments; if (!Array.isArray(decoratorArguments) || decoratorArguments.length === 0) { continue; } const firstDecoratorArgument = decoratorArguments[0]; if ('value' in firstDecoratorArgument && firstDecoratorArgument.value === status) { return true; } } return false; } function getAllDecoratorsWithName({ node, decoratorName, }) { var _a; if (!Array.isArray(node === null || node === void 0 ? void 0 : node.decorators)) { return []; } const decorators = []; for (const decorator of node.decorators) { if ((decorator === null || decorator === void 0 ? void 0 : decorator.type) !== 'Decorator' || decorator.expression.type !== 'CallExpression' || ((_a = decorator.expression.callee) === null || _a === void 0 ? void 0 : _a.type) !== 'Identifier') { continue; } if (decorator.expression.callee.name !== decoratorName) { continue; } decorators.push(decorator); } return decorators; }