|
| 1 | +/** |
| 2 | + * Copyright (C) 2023 Green Code Initiative |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +"use strict"; |
| 18 | + |
| 19 | +const PAGINATION_KEY_WORDS = ["page", "pagination", "paginated"]; |
| 20 | + |
| 21 | +const isPaginationName = (name) => { |
| 22 | + return PAGINATION_KEY_WORDS.some((keyWord) => |
| 23 | + name.toLowerCase().includes(keyWord) |
| 24 | + ); |
| 25 | +}; |
| 26 | + |
| 27 | +const isPaginated = (objectType) => { |
| 28 | + if (objectType.type === "TSTypeReference") { |
| 29 | + // Pagination object should be an object, for example, it can't be an array |
| 30 | + |
| 31 | + if (isPaginationName(objectType.typeName.name)) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + } else if (objectType.type === "TSTypeLiteral") { |
| 35 | + if ( |
| 36 | + objectType.members != null && |
| 37 | + objectType.members.some( |
| 38 | + (member) => member.key != null && isPaginationName(member.key.name) |
| 39 | + ) |
| 40 | + ) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | +}; |
| 47 | + |
| 48 | +const report = (context, node) => |
| 49 | + context.report({ node, messageId: "PreferReturnCollectionsWithPagination" }); |
| 50 | + |
| 51 | +// Type: RuleModule<"uppercase", ...> |
| 52 | +module.exports = { |
| 53 | + name: "prefer-collections-with-pagination", |
| 54 | + meta: { |
| 55 | + docs: { |
| 56 | + description: "Prefer API collections with pagination.", |
| 57 | + category: "eco-design", |
| 58 | + recommended: "warn", |
| 59 | + }, |
| 60 | + messages: { |
| 61 | + PreferReturnCollectionsWithPagination: |
| 62 | + "Prefer return collections with pagination in HTTP GET", |
| 63 | + }, |
| 64 | + type: "suggestion", |
| 65 | + schema: [], |
| 66 | + }, |
| 67 | + defaultOptions: [], |
| 68 | + create(context) { |
| 69 | + const isValidDecorator = (decorator) => { |
| 70 | + return ( |
| 71 | + decorator.expression.callee.name.toLowerCase() === "get" && |
| 72 | + (decorator.expression.arguments.length === 0 || |
| 73 | + !decorator.expression.arguments[0].value.includes(":")) |
| 74 | + ); |
| 75 | + }; |
| 76 | + |
| 77 | + return { |
| 78 | + Decorator(node) { |
| 79 | + if ( |
| 80 | + isValidDecorator(node) && |
| 81 | + node.parent.parent.parent.type === "ClassDeclaration" && |
| 82 | + node.parent.parent.parent.decorators.find( |
| 83 | + (decorator) => |
| 84 | + decorator.expression.callee.name.toLowerCase() === "controller" |
| 85 | + ) |
| 86 | + ) { |
| 87 | + const getMethod = node.parent; |
| 88 | + const returnType = |
| 89 | + getMethod.value.returnType != null |
| 90 | + ? getMethod.value.returnType.typeAnnotation |
| 91 | + : null; |
| 92 | + |
| 93 | + if (returnType != null) { |
| 94 | + if ( |
| 95 | + returnType.type === "TSTypeReference" && |
| 96 | + returnType.typeName.name === "Promise" |
| 97 | + ) { |
| 98 | + if ( |
| 99 | + returnType.typeParameters.params.length === 1 && |
| 100 | + !isPaginated(returnType.typeParameters.params[0]) |
| 101 | + ) { |
| 102 | + report(context, returnType); |
| 103 | + } |
| 104 | + } else if ( |
| 105 | + returnType.type === "TSArrayType" || |
| 106 | + !isPaginated(returnType) |
| 107 | + ) { |
| 108 | + report(context, returnType); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + }, |
| 113 | + }; |
| 114 | + }, |
| 115 | +}; |
0 commit comments