module Noir::ImportGraph

Overview

Shared file-level import-graph traversal for cross-file route / DTO resolution.

Today the JVM-style flavour is implemented (Java, Kotlin): a file belongs to a package whose dotted path is reflected in its directory layout (src/main/java/com/foo/Bar.javapackage com.foo), and imports map to file paths under the inferred source root. This is the pattern called out as duplication in #1107 for TreeSitterJavaDtoIndex and TreeSitterKotlinDtoIndex.

Caveats / non-goals:

Defined in:

miniparsers/import_graph.cr

Constant Summary

JS_RESOLVE_EXTENSIONS = ["ts", "tsx", "js", "jsx", "mjs", "cjs"]

Class Method Summary

Class Method Detail

def self.related_files(path : String, package_name : String, imports : Indexable(ImportRef), extension : String, &block : String -> ) : Nil #

Yields every file path that should be considered when resolving symbols visible to path:

  1. path itself.
  2. Same-directory siblings ending in .{extension} (same package).
  3. Files reachable through imports, resolved against a source root inferred from package_name.

Wildcard imports expand to every matching file in the imported directory. Each file is yielded at most once. When package_name is empty, the source-root step is skipped (no safe inference possible) — same-directory siblings are still yielded.


[View source]
def self.resolve_relative_import(from_file : String, import_specifier : String, extensions : Array(String) = JS_RESOLVE_EXTENSIONS, boundary : String | Nil = nil) : String | Nil #

[View source]
def self.source_root_for(file_path : String, package_name : String) : String | Nil #

Infer the source root by stripping the package path from the file's directory. src/main/java/com/foo/Bar.java with package com.foo returns src/main/java. Returns nil when the package path doesn't actually trail the file's directory (the source tree is laid out differently and we can't safely resolve imports).


[View source]