|
| 1 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +import 'package:analyzer/dart/element/type.dart'; |
| 3 | +import 'package:analyzer/error/listener.dart'; |
| 4 | +import 'package:collection/collection.dart'; |
| 5 | +import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 6 | +import 'package:dart_frog_lint/src/types.dart'; |
| 7 | + |
| 8 | +/// {@template dart_frog_lint.request} |
| 9 | +/// The definition of `dart_frog_entrypoint` lints. |
| 10 | +/// {@endtemplate} |
| 11 | +class DartFrogEntrypoint extends DartLintRule { |
| 12 | + /// {@macro dart_frog_lint.request} |
| 13 | + const DartFrogEntrypoint() |
| 14 | + : super( |
| 15 | + code: const LintCode( |
| 16 | + name: 'dart_frog_entrypoint', |
| 17 | + problemMessage: 'Main files should define a valid "run" function.', |
| 18 | + ), |
| 19 | + ); |
| 20 | + |
| 21 | + @override |
| 22 | + List<String> get filesToAnalyze => ['main.dart']; |
| 23 | + |
| 24 | + @override |
| 25 | + void run( |
| 26 | + CustomLintResolver resolver, |
| 27 | + ErrorReporter reporter, |
| 28 | + CustomLintContext context, |
| 29 | + ) { |
| 30 | + context.registry.addCompilationUnit((node) { |
| 31 | + // Search for a function declaration with the name "run" |
| 32 | + final run = |
| 33 | + node.declarations.whereType<FunctionDeclaration>().firstWhereOrNull( |
| 34 | + (declaration) => declaration.name.lexeme == 'run', |
| 35 | + ); |
| 36 | + |
| 37 | + if (run == null) { |
| 38 | + // No function declaration found with the name "run" |
| 39 | + reporter.reportErrorForNode(code, node.directives.firstOrNull ?? node); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (run.functionExpression.parameters?.parameters.length != 3) { |
| 44 | + // Only one parameter is allowed |
| 45 | + reporter.reportErrorForNode(code, run); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + final handlerType = run.functionExpression.parameters?.parameters |
| 50 | + .firstOrNull?.declaredElement?.type; |
| 51 | + final ipType = run.functionExpression.parameters?.parameters |
| 52 | + .elementAtOrNull(1) |
| 53 | + ?.declaredElement |
| 54 | + ?.type; |
| 55 | + final portType = run.functionExpression.parameters?.parameters |
| 56 | + .elementAtOrNull(2) |
| 57 | + ?.declaredElement |
| 58 | + ?.type; |
| 59 | + |
| 60 | + if (handlerType == null || !isHandler(handlerType)) { |
| 61 | + // The parameter is not a Handler |
| 62 | + reporter.reportErrorForNode(code, run); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (ipType == null || !internetAddressTypeChecker.isExactlyType(ipType)) { |
| 67 | + // The parameter is not a Handler |
| 68 | + reporter.reportErrorForNode(code, run); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (portType?.isDartCoreInt != true) { |
| 73 | + // The parameter is not a Handler |
| 74 | + reporter.reportErrorForNode(code, run); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + final returnType = run.returnType?.type; |
| 79 | + if (returnType == null || |
| 80 | + !returnType.isDartAsyncFuture || |
| 81 | + !httpServerTypeChecker.isExactlyType( |
| 82 | + (returnType as InterfaceType).typeArguments.single, |
| 83 | + )) { |
| 84 | + // The parameter is not a Handler |
| 85 | + reporter.reportErrorForNode(code, run); |
| 86 | + return; |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments