class Analyzer::Dart::DartFrog

Overview

Dart Frog is a filesystem-routed framework. Routes live under routes/ and the URL is derived from the directory layout:

routes/index.dart → / routes/about.dart → /about routes/users/index.dart → /users routes/users/[id].dart → /users/{id} routes/users/[id]/posts.dart → /users/{id}/posts

Each route file exports an onRequest(RequestContext, ...) handler. Method dispatch happens inside that handler — typically a switch on context.request.method against HttpMethod.<verb> constants. We surface the verbs we can see referenced in the file and fall back to the standard set when the file looks like a catch-all (no explicit HttpMethod.* references).

_middleware.dart and other underscore-prefixed Dart files are framework plumbing — not user-facing routes — and skipped.

Defined in:

analyzer/analyzers/dart/dart_frog.cr

Constant Summary

CASE_METHOD_REGEX = /\bcase\s+HttpMethod\.([a-z]+)\s*:/

Matches case HttpMethod.<verb>: clauses inside a method switch.

CLAUSE_BOUNDARY_REGEX = /\}|\bcase\b|\bdefault\b/

The handler body for a case clause runs until the next clause or the closing brace of the switch — whichever comes first. We bound at the earliest of a }, a case, or a default keyword so the last enumerated method-case can't sweep in a trailing default: arm (whose 405/methodNotAllowed would wrongly tar the case).

FALLBACK_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH"]
HANDLER_REFERENCE_REGEX = /\A[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*\z/

A bare (possibly dotted) handler reference assigned to onRequest.

HTTP_METHOD_MAP = {"get" => "GET", "post" => "POST", "put" => "PUT", "delete" => "DELETE", "patch" => "PATCH", "head" => "HEAD", "options" => "OPTIONS"}
HTTP_METHOD_PATTERNS = HTTP_METHOD_MAP.map do |dart_name, verb| {verb, /HttpMethod\.#{dart_name}\b/} end

Crystal recompiles an interpolated regex literal on every evaluation (a full PCRE2 JIT compile) — precompile the fixed verb probes once at load time instead of per handler file.

METHOD_NOT_ALLOWED_REGEX = /methodNotAllowed|MethodNotAllowed|statusCode\s*:\s*405|\(\s*405\b/

A clause body that rejects the verb: methodNotAllowed, MethodNotAllowedResponse(), or a literal 405 status code passed positionally (Response(405)) or by name (statusCode: 405). We deliberately avoid a bare 405 so an unrelated {'code': 405} in a real handler body can't be mistaken for a rejection.

ON_REQUEST_HANDLER_REGEX = /\bonRequest\s*[(=]/

Every Dart Frog route file exports an onRequest handler — either a function (Response onRequest(...), Future<Response> onRequest(...)) or the assignment form (Handler onRequest = ...). A .dart file under a routes/ directory that lacks it is not a server route. This matters for full-stack monorepos, where a Flutter client commonly keeps its UI navigation under lib/.../routes/ — those widget files must not be reported as HTTP endpoints.

Instance Method Summary

Instance methods inherited from class Analyzer

analyze analyze, base_path : String base_path, base_paths : Array(String) base_paths, callees_needed? : Bool callees_needed?, logger : NoirLogger logger, parallel_analyze(files : Array(String), &block : String -> Nil) parallel_analyze, read_file_content(path : String) : String read_file_content, result : Array(Endpoint) result, url : String url

Constructor methods inherited from class Analyzer

new(options : Hash(String, YAML::Any)) new

Macros inherited from class Analyzer

define_getter_methods(names) define_getter_methods

Instance methods inherited from module FileHelper

all_files : Array(String) all_files, get_files_by_extension(extension : String) : Array(String) get_files_by_extension, get_files_by_prefix(prefix : String) : Array(String) get_files_by_prefix, get_files_by_prefix_and_extension(prefix : String, extension : String) : Array(String) get_files_by_prefix_and_extension, get_public_dir_files(base_path : String, folder : String) : Array(String) get_public_dir_files, get_public_files(base_path : String, anchors : Array(String) = ["shard.yml", "Gemfile"]) : Array(String) get_public_files

Instance Method Detail

def analyze #

[View source]