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.
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
- JSX_ELEMENT_PATTERN =
Patterns to detect usage in controller files
%r{<([A-Z][A-Za-z0-9_]*)[\s/>]}- REACT_CREATE_PATTERN =
/React\.createElement\(\s*([A-Z][A-Za-z0-9_]*)[\s,)]/- HOOK_CALL_PATTERN =
/\b(use[A-Z][A-Za-z0-9_]*)\s*\(/- 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
Constructor Details
#initialize(config = ReactManifest.configuration) ⇒ Scanner
Returns a new instance of Scanner.
60 61 62 |
# File 'lib/react_manifest/scanner.rb', line 60 def initialize(config = ReactManifest.configuration) @config = config end |
Instance Method Details
#scan(classification) ⇒ Object
64 65 66 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 |
# File 'lib/react_manifest/scanner.rb', line 64 def scan(classification) warnings = [] symbol_index = {} # Phase 1: index symbols from shared dirs classification.shared_dirs.each do |shared_dir| js_files_in(shared_dir[:path]).each do |file_path| relative = relative_require_path(file_path) 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 $stdout.puts "[ReactManifest] Shared symbol index: #{symbol_index.size} symbols indexed" if @config.verbose? # 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) begin content = File.read(file_path, encoding: "utf-8") rescue Errno::ENOENT, Errno::EACCES => e warnings << "Skipping #{file_path}: #{e.}" next rescue Encoding::InvalidByteSequenceError warnings << "Skipping #{file_path}: not valid UTF-8" next end # JSX element usage: <PrimaryButton (JSX tag syntax) content.scan(JSX_ELEMENT_PATTERN) do |match| sym = match[0] used << symbol_index[sym] if symbol_index.key?(sym) end # React.createElement(PrimaryButton, ...) (non-JSX style) content.scan(REACT_CREATE_PATTERN) do |match| sym = match[0] used << symbol_index[sym] if symbol_index.key?(sym) end # Hook calls: useFetch( content.scan(HOOK_CALL_PATTERN) do |match| sym = match[0] used << symbol_index[sym] if symbol_index.key?(sym) end # Lib function calls: formatDate( — filtered against lib symbol index content.scan(LIB_CALL_PATTERN) do |match| sym = match[0] next if JS_BUILTINS.include?(sym) used << symbol_index[sym] if symbol_index.key?(sym) end 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 ) end |