UNPKG

eslint-plugin-next-route-params

Version:

eslint rule to enforce correct route parameters for Next.js

81 lines (80 loc) 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateGenerateStaticParamsFunction = validateGenerateStaticParamsFunction; const utils_1 = require("@typescript-eslint/utils"); const typeGenerator_1 = require("../../types/typeGenerator"); const validateReturntype_1 = require("./validateReturntype"); const findReferencedType_1 = require("../findReferencedType"); function validateGenerateStaticParamsFunction(functionNode, context) { const returnType = functionNode.returnType; //check if the return type is Promise<T> if (returnType == null) { const arrowToken = context.ruleContext.sourceCode.getFirstToken(functionNode, (token) => token.value === "=>"); if (functionNode.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression && arrowToken != null) { context.ruleContext.report({ loc: functionNode.loc, messageId: "issue:no-returntype", fix: (fixer) => fixer.insertTextBeforeRange(arrowToken.range, `: ${(0, typeGenerator_1.createGenerateStaticParamsReturntypeString)(functionNode.async, context.customContext.params)}`), }); } else { context.ruleContext.report({ loc: functionNode.loc, messageId: "issue:no-returntype", fix: (fixer) => fixer.insertTextBeforeRange(functionNode.body.range, `: ${(0, typeGenerator_1.createGenerateStaticParamsReturntypeString)(functionNode.async, context.customContext.params)}`), }); } return; } if (!("typeAnnotation" in returnType)) { return; } const fullTypeAnnotation = returnType.typeAnnotation; // take the T of Promise<T> if it is a Promise const typeAnnotation = fullTypeAnnotation.type === utils_1.AST_NODE_TYPES.TSTypeReference && fullTypeAnnotation.typeName.type === utils_1.AST_NODE_TYPES.Identifier && fullTypeAnnotation.typeName.name === "Promise" && fullTypeAnnotation.typeArguments?.params[0] ? fullTypeAnnotation.typeArguments.params[0] : fullTypeAnnotation; const isArray = typeAnnotation.type === utils_1.AST_NODE_TYPES.TSArrayType; if (!isArray) { context.ruleContext.report({ loc: functionNode.loc, messageId: "issue:wrong-returntype", fix: (fixer) => fixer.replaceTextRange(returnType.range, `: ${(0, typeGenerator_1.createGenerateStaticParamsReturntypeString)(functionNode.async, context.customContext.params)}`), }); return; } const arrayType = typeAnnotation.elementType; const arrayTypeAnnotation = arrayType.type; switch (arrayTypeAnnotation) { case utils_1.AST_NODE_TYPES.TSTypeLiteral: (0, validateReturntype_1.validateGenerateStaticParamsInnerReturnTypeOfArray)(arrayType, context); return; case utils_1.AST_NODE_TYPES.TSTypeReference: { const type = (0, findReferencedType_1.findReferencedType)(arrayType, context); if (type) { (0, validateReturntype_1.validateGenerateStaticParamsInnerReturnTypeOfArray)(type, context); return; } else { context.ruleContext.report({ loc: functionNode.loc, messageId: "issue:isNoLiteral", fix: (fixer) => fixer.replaceTextRange(returnType.range, (0, typeGenerator_1.createGenerateStaticParamsReturntypeString)(functionNode.async, context.customContext.params)), }); return; } } default: { context.ruleContext.report({ loc: functionNode.loc, messageId: "issue:isNoLiteral", fix: (fixer) => fixer.replaceTextRange(returnType.range, (0, typeGenerator_1.createGenerateStaticParamsReturntypeString)(functionNode.async, context.customContext.params)), }); return; } } }