Class: ReactManifest::Scanner

Inherits:
Object
  • Object
show all
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,)]/
JSX_PROP_COMPONENT_PATTERN =
/[A-Za-z_][A-Za-z0-9_]*\s*=\s*\{\s*([A-Z][A-Za-z0-9_]*)\s*\}/
OBJECT_COMPONENT_PATTERN =
/:\s*([A-Z][A-Za-z0-9_]*)\b/
ARRAY_COMPONENT_LIST_PATTERN =
/\[\s*([A-Z][A-Za-z0-9_]*(?:\s*,\s*[A-Z][A-Za-z0-9_]*)*\s*,?)\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

Constructor Details

#initialize(config = ReactManifest.configuration) ⇒ Scanner

Returns a new instance of Scanner.



63
64
65
# File 'lib/react_manifest/scanner.rb', line 63

def initialize(config = ReactManifest.configuration)
  @config = config
end

Instance Method Details

#scan(classification) ⇒ Object



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
# File 'lib/react_manifest/scanner.rb', line 67

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)
      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
  )
end