Class: ReactManifest::Scanner
- Inherits:
-
Object
- Object
- ReactManifest::Scanner
- Defined in:
- lib/react_manifest/scanner.rb
Overview
Scans JS/JSX (and optionally TS/TSX) files using regex — no AST, no Node.js required.
Returns a Result containing:
-
symbol_index— map of exported symbol name → shared require path -
controller_usages— map of controller name → sorted array of referenced shared files -
warnings— non-fatal issues found during scanning
Phase 1 — builds a symbol index from shared dirs:
"PrimaryButton" => "ux/components/buttons/primary_button"
"useFetch" => "ux/hooks/use_fetch"
"formatDate" => "ux/lib/format_date"
Phase 2 — scans controller files for usage of those symbols
and produces per-controller lists of referenced shared files.
Phase 3 — emits non-fatal warnings. rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: Result
Constant Summary collapse
- DEFINITION_PATTERNS =
Patterns to detect symbol definitions (CommonJS and ES module style)
[ # CommonJS / variable-assignment style /(?:const|let|var)\s+([A-Z][A-Za-z0-9_]*)\s*=/, # const FooBar = /function\s+([A-Z][A-Za-z0-9_]*)\s*\(/, # function FooBar( /class\s+([A-Z][A-Za-z0-9_]*)\s*(?:extends|\{)/, # class FooBar /(?:const|let|var)\s+(use[A-Z][A-Za-z0-9_]*)\s*=/, # const useFoo = (hooks) /function\s+(use[A-Z][A-Za-z0-9_]*)\s*\(/, # function useFoo( /(?:const|let|var)\s+([a-z][A-Za-z0-9_]{2,})\s*=\s*(?:function|\()/, # const formatDate = function/arrow /^function\s+([a-z][A-Za-z0-9_]{2,})\s*\(/, # function formatDate( at line start # ES module style (export default / named exports) /^export\s+default\s+(?:function|class)\s+([A-Z][A-Za-z0-9_]*)/, # export default function Foo /^export\s+default\s+(?:function|class)\s+(use[A-Z][A-Za-z0-9_]*)/, # export default function useFoo /^export\s+(?:const|let|var)\s+([A-Z][A-Za-z0-9_]*)\s*=/, # export const Foo = /^export\s+(?:const|let|var)\s+(use[A-Z][A-Za-z0-9_]*)\s*=/, # export const useFoo = /^export\s+(?:const|let|var)\s+([a-z][A-Za-z0-9_]{2,})\s*=\s*(?:function|\()/, # export const formatDate = /^export\s+function\s+([A-Z][A-Za-z0-9_]*)\s*\(/, # export function Foo( /^export\s+function\s+(use[A-Z][A-Za-z0-9_]*)\s*\(/, # export function useFoo( /^export\s+class\s+([A-Z][A-Za-z0-9_]*)\s*(?:extends|\{)/ # export class Foo ].freeze
- PASCAL_TOKEN_PATTERN =
Patterns to detect usage in controller files. Token-based patterns match any identifier occurrence regardless of syntax context (JSX, constructor, assignment, array, function argument, etc.).
/\b([A-Z][A-Za-z0-9_]*)\b/- HOOK_TOKEN_PATTERN =
/\b(use[A-Z][A-Za-z0-9_]*)\b/- LIB_CALL_PATTERN =
Lib calls matched against known lib symbols to reduce false positives
/\b([a-z][A-Za-z0-9_]{2,})\s*\(/- JS_BUILTINS =
Common JS built-ins to exclude from lib-call matching
%w[ require function return typeof instanceof delete void console document window location history navigator setTimeout setInterval clearTimeout clearInterval parseInt parseFloat isNaN isFinite encodeURI decodeURI fetch Promise Object Array String Number Boolean Math JSON Object Array String Number Boolean Symbol Map Set WeakMap ].freeze
Instance Method Summary collapse
-
#initialize(config = ReactManifest.configuration) ⇒ Scanner
constructor
A new instance of Scanner.
-
#scan(classification) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/PerceivedComplexity.
Constructor Details
#initialize(config = ReactManifest.configuration) ⇒ Scanner
Returns a new instance of Scanner.
62 63 64 |
# File 'lib/react_manifest/scanner.rb', line 62 def initialize(config = ReactManifest.configuration) @config = config end |
Instance Method Details
#scan(classification) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/PerceivedComplexity
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/react_manifest/scanner.rb', line 67 def scan(classification) warnings = [] symbol_index = {} # Phase 1a: index symbols from shared dirs shared_file_paths = {} # file_path => relative_require_path for all shared files classification.shared_dirs.each do |shared_dir| js_files_in(shared_dir[:path]).each do |file_path| relative = relative_require_path(file_path) shared_file_paths[file_path] = relative symbols = extract_definitions(file_path) symbols.each do |sym| if symbol_index.key?(sym) warnings << "Duplicate symbol '#{sym}' in #{relative} (already from #{symbol_index[sym]})" else symbol_index[sym] = relative end end end end # Phase 1b: index symbols from external_roots dirs @config.external_roots.each do |root_path| abs_root = abs_external_root(root_path) js_files_in(abs_root).each do |file_path| relative = relative_require_path(file_path) symbols = extract_definitions(file_path) symbols.each do |sym| symbol_index[sym] ||= relative end end end # Phase 1c: add explicit external_providers (highest precedence — wins on conflict) @config.external_providers.each do |sym, require_path| symbol_index[sym] = require_path end # Phase 1d: build controller (app-dir) symbol index for violation detection controller_symbol_index = {} classification.controller_dirs.each do |ctrl| js_files_in(ctrl[:path]).each do |file_path| extract_definitions(file_path).each do |sym| controller_symbol_index[sym] ||= { file: relative_require_path(file_path), controller: ctrl[:name] } end end end $stdout.puts "[ReactManifest] Shared symbol index: #{symbol_index.size} symbols indexed" if @config.verbose? # Phase 1e: detect shared files that use app-dir (controller) symbols shared_violations = detect_shared_violations(shared_file_paths, controller_symbol_index, warnings) # Phase 2: scan controller dirs for usage controller_usages = {} classification.controller_dirs.each do |ctrl| files = js_files_in(ctrl[:path]) used = Set.new warnings << "Controller dir '#{ctrl[:name]}' has no JS/JSX files" if files.empty? && @config.verbose? files.each do |file_path| validate_naming(file_path, ctrl[:name], warnings) content = read_controller_file(file_path, warnings) next unless content used.merge(extract_used_shared_paths(content, symbol_index)) end controller_usages[ctrl[:name]] = used.to_a.sort end # Phase 3: additional warnings emit_fanout_warnings(controller_usages, warnings) Result.new( symbol_index: symbol_index, controller_usages: controller_usages, warnings: warnings, shared_violations: shared_violations ) end |