class Analyzer::Swift::Hummingbird

Defined in:

analyzer/analyzers/swift/hummingbird.cr

Constant Summary

DSL_BODY_RE = /\bsome\s+RouterMiddleware\b/

var body: some RouterMiddleware<Context> / func routes() -> some RouterMiddleware<Context> — the controller body that hosts the DSL.

DSL_BUILDER_RE = /(?<![.\w])RouterBuilder\s*[(<]/
DSL_GROUP_RE = /(?<![.\w])RouteGroup\s*\(/
DSL_VERB_RE = /(?<![.\w])(Get|Post|Put|Patch|Delete|Head)\s*\(/

The capitalized result-builder primitives exposed by the HummingbirdRouter module. Unlike the receiver-based closure API (router.get("x") { ... }), routes are declared structurally:

let router = RouterBuilder(context: Context.self) {
    Get("/health") { _, _ in .ok }   // GET /health
    UserController(...)               // splices its `body` here
    RouteGroup("api") {
        Post("login", handler: self.login) // POST /api/login
    }
}

struct UserController: RouterController {
    var body: some RouterMiddleware<Context> {
        RouteGroup("user") {
            Put(handler: self.create)          // PUT  /user
            Post("signup", handler: self.signup) // POST /user/signup
        }
    }
}

A route primitive is only honored when it sits directly inside a route-emitting scope — a RouterBuilder { } block, a RouteGroup { } block, or a var/func ... some RouterMiddleware { } controller body — never inside a handler closure. That gating keeps look-alike PascalCase constructors (a Post(title:) model, a Get-named type) out of the endpoint list.

FUNCTION_SIGNATURE_PATTERN = /\bfunc\s+([A-Za-z_]\w*)\s*\(/
GROUP_ASSIGN_PATTERN = /\b(?:let|var)\s+([A-Za-z_]\w*)\s*=\s*([A-Za-z_]\w*)\.group\s*\(/

let group = router.group("path") / var api = router.group("v1")

GROUP_CLOSURE_PATTERN = /([A-Za-z_]\w*)\.group\s*\(/

router.group("admin") { admin in ... } — closure-scoped group.

HTTP_METHODS = ["get", "post", "put", "patch", "delete", "head"] of ::String

Route-registration methods exposed by Hummingbird's RouterMethods protocol. They all return Self, which is what makes the fluent builder chain (group.get(...).post(...)) possible.

LOOKAHEAD_LIMIT = 20

Maximum number of lines to look ahead for function parameters

ROUTE_BODY_LOOKAHEAD_LIMIT = 5
ROUTE_COLLECTION_ASSIGN_PATTERN = /\b(?:let|var)\s+([A-Za-z_]\w*)\s*=\s*([A-Za-z_]\w*\.)?RouteCollection\s*[(<]/

let routes = RouteCollection(context: ...) — a mountable route group.

ROUTER_ASSIGN_PATTERN = /\b(?:let|var)\s+([A-Za-z_]\w*)\s*=\s*Router\s*[(<]/

let router = Router() — establishes a root router-like receiver.

ROUTER_PARAM_PATTERN = /([A-Za-z_]\w*)\s*:\s*(?:some\s+|any\s+|inout\s+)*(?:Router|RouterGroup|RouterMethods)\b/

A function parameter typed as a router/router-group/router-methods.

TYPE_DECL_PATTERN = /\b(?:struct|class|extension|actor|enum)\s+([A-Za-z_]\w*)/

Type declarations whose body can host a RouteCollection handler.

Instance Method Summary

Instance methods inherited from class Analyzer::Swift::SwiftEngine

analyze analyze, analyze_file(path : String) : Array(Endpoint) analyze_file

Class methods inherited from class Analyzer::Swift::SwiftEngine

swift_test_path?(path : String) : Bool swift_test_path?, swift_vendor_path?(path : String) : Bool swift_vendor_path?

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 #

Project-wide pre-pass: resolve RouteCollection prefixes before the per-file scan so controller bodies in one file can pick up the prefix declared at a call site in another.


[View source]
def analyze_file(path : String) : Array(Endpoint) #

[View source]
def extract_function_params(lines : Array(String), start_index : Int32, endpoint : Endpoint) #

Extract parameters from an inline closure body.


[View source]
def extract_path_params(route : String, endpoint : Endpoint) #

Extract path parameters from the route pattern (e.g., :id, :userID)


[View source]
def parse_route_path(route_args : String) : String #

Parse route path from route arguments. The path is the first quoted string; {id}-style segments are normalized to :id.


[View source]