class
AssetPipeline::DependencyAnalyzer
- AssetPipeline::DependencyAnalyzer
- Reference
- Object
Overview
The DependencyAnalyzer analyzes JavaScript code to detect dependencies that should be imported.
It can identify references to external libraries, local modules, and common JavaScript patterns
that indicate missing imports.
Performance optimizations:
- Caches dependency analysis results based on JavaScript content hash
- Memoizes expensive regex operations
- Caches import suggestions to avoid regeneration
Defined in:
asset_pipeline/dependency_analyzer.crConstant Summary
-
EXTERNAL_LIBRARY_PATTERNS =
{/\$\s*\(/ => "jquery", /jQuery[\.\s]*[\(\w]/ => "jQuery", /_\.(map|filter|forEach|reduce|find|includes|isEmpty|isArray|get|set|has|clone)/ => "lodash", /lodash\.(map|filter|forEach|reduce|find|includes|isEmpty|isArray|get|set|has|clone)/ => "lodash", /moment\s*\(/ => "moment", /moment\.(now|utc|unix)/ => "moment", /new\s+Chart\s*\(/ => "chartjs", /Chart\.(register|defaults)/ => "chartjs", /Alpine\.(start|data|store)/ => "alpinejs", /x-data\s*=/ => "alpinejs", /Vue\.(createApp|ref|reactive|computed)/ => "vue", /createApp\s*\(/ => "vue", /React\.(useState|useEffect|createElement)/ => "react", /ReactDOM\.render/ => "react-dom", /axios\.(get|post|put|delete)/ => "axios", /fetch\s*\(/ => "unfetch"} -
Common external library patterns and their typical import names
-
LOCAL_MODULE_PATTERNS =
[/new\s+([A-Z][a-zA-Z0-9_]*)\s*\(/, /([A-Z][a-zA-Z0-9_]*)\.[a-zA-Z_][a-zA-Z0-9_]*\s*\(/, /([A-Z][a-zA-Z0-9_]*)\.[A-Z_][A-Z0-9_]*\s*[=\(]/] -
Patterns that indicate local module references
Constructors
Class Method Summary
-
.bulk_analyze(javascript_blocks : Array(String)) : Array(Hash(Symbol, Array(String)))
Bulk analysis for multiple JavaScript blocks (optimized for performance)
-
.cache_stats : Hash(String, Int32)
Performance monitoring and cache management methods Returns cache statistics for monitoring performance
-
.clear_caches
Clears all performance caches (useful for testing or memory management)
-
.warm_cache(common_patterns : Array(String))
Pre-warm cache with common patterns (useful for production deployments)
Instance Method Summary
-
#analyze_code_complexity
Analyzes the code complexity to determine if it needs modularization
-
#analyze_dependencies : Hash(Symbol, Array(String))
Analyzes the JavaScript content and returns detected dependencies
-
#detect_external_dependencies : Array(String)
Detects external library dependencies from common usage patterns
-
#detect_local_dependencies : Array(String)
Detects local module dependencies from usage patterns
-
#extract_existing_imports : Array(String)
Analyzes import statements that are already present in the code
-
#generate_import_suggestions(external_deps : Array(String), local_deps : Array(String)) : Array(String)
Generates import statement suggestions based on detected dependencies
-
#uses_module_syntax? : Bool
Checks if the code uses any module syntax
Constructor Detail
Class Method Detail
Bulk analysis for multiple JavaScript blocks (optimized for performance)
Performance monitoring and cache management methods Returns cache statistics for monitoring performance
Pre-warm cache with common patterns (useful for production deployments)
Instance Method Detail
Analyzes the JavaScript content and returns detected dependencies
Returns a hash with:
- :external - Array of external library names detected
- :local - Array of local module names detected
- :suggestions - Array of import suggestions
Detects external library dependencies from common usage patterns
Detects local module dependencies from usage patterns
Analyzes import statements that are already present in the code
Generates import statement suggestions based on detected dependencies