module Analyzer::Javascript::JsModuleResolver

Overview

Resolves a JS/TS require('./x') / import ... from './x' specifier to a file on disk.

Extracted from RouterMountScanner so RouteHelperScanner resolves specifiers identically: the mount pass decides which file a prefix belongs to and the helper pass decides which file defines a helper, and the two have to name the same file for a helper's routes to end up under the right prefix.

root anchors the @/ and ~/ aliases — the scan base the importing file belongs to. Bare specifiers (express, @hapi/hapi) resolve to nil; a node_modules walk is deliberately out of scope.

Defined in:

analyzer/analyzers/javascript/express/js_module_resolver.cr

Constant Summary

JS_TO_TS_EXT = {".js" => [".ts", ".tsx"], ".jsx" => [".tsx"], ".mjs" => [".mts"], ".cjs" => [".cts"]}

TypeScript ESM (moduleResolution: NodeNext) imports a sibling .ts source through a .js-family specifier. Map each JS extension to the TS source extension(s) it can stand in for.

Class Method Summary

Class Method Detail

def self.resolve(from_file : String, require_path : String, root : String) : String | Nil #

[View source]