class AssetPipeline::DependencyAnalyzer

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:

Defined in:

asset_pipeline/dependency_analyzer.cr

Constant 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

Instance Method Summary

Constructor Detail

def self.new(javascript_content : String) #

[View source]

Class Method Detail

def self.bulk_analyze(javascript_blocks : Array(String)) : Array(Hash(Symbol, Array(String))) #

Bulk analysis for multiple JavaScript blocks (optimized for performance)


[View source]
def self.cache_stats : Hash(String, Int32) #

Performance monitoring and cache management methods Returns cache statistics for monitoring performance


[View source]
def self.clear_caches #

Clears all performance caches (useful for testing or memory management)


[View source]
def self.warm_cache(common_patterns : Array(String)) #

Pre-warm cache with common patterns (useful for production deployments)


[View source]

Instance Method Detail

def analyze_code_complexity #

Analyzes the code complexity to determine if it needs modularization


[View source]
def analyze_dependencies : Hash(Symbol, Array(String)) #

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

[View source]
def detect_external_dependencies : Array(String) #

Detects external library dependencies from common usage patterns


[View source]
def detect_local_dependencies : Array(String) #

Detects local module dependencies from usage patterns


[View source]
def extract_existing_imports : Array(String) #

Analyzes import statements that are already present in the code


[View source]
def generate_import_suggestions(external_deps : Array(String), local_deps : Array(String)) : Array(String) #

Generates import statement suggestions based on detected dependencies


[View source]
def uses_module_syntax? : Bool #

Checks if the code uses any module syntax


[View source]