Class: Mbeditor::JsDefinitionService

Inherits:
Object
  • Object
show all
Defined in:
app/services/mbeditor/js_definition_service.rb

Overview

Searches JS/JSX/TS/TSX files in the workspace for definitions of a named JavaScript global (variable, function, class, or window property assignment).

Delegates subprocess execution to CodeSearchService (rg/git grep/grep).

Ranking: top-level declarations (column 0, window.X =, or export) are Sprockets globals and sort before nested/indented matches, so go-to- definition prefers the global over a same-named function buried inside an IIFE. When parent is given (the user clicked Parent.myFunction or a name destructured from Parent), member definitions are resolved first.

Returns an array of hashes:

{ file: String, line: Integer, snippet: String, topLevel: Boolean }

Member results additionally carry member: true.

Constant Summary collapse

MAX_RESULTS =
20
SCAN_CAP =

Matches parsed before ranking. The cap must be applied AFTER sorting or a flood of nested matches could crowd the top-level definition out.

200
IDENT =
/[a-zA-Z_$][a-zA-Z0-9_$]*/

Instance Method Summary collapse

Constructor Details

#initialize(symbol, workspace_root, parent: nil) ⇒ JsDefinitionService

Returns a new instance of JsDefinitionService.



26
27
28
29
30
# File 'app/services/mbeditor/js_definition_service.rb', line 26

def initialize(symbol, workspace_root, parent: nil)
  @symbol         = symbol.to_s
  @workspace_root = workspace_root.to_s.chomp("/")
  @parent         = parent.to_s.empty? ? nil : parent.to_s
end

Instance Method Details

#callObject



32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/mbeditor/js_definition_service.rb', line 32

def call
  return [] if @symbol.empty? || @workspace_root.empty?
  return [] unless File.directory?(@workspace_root)

  if @parent
    member_results = member_definitions
    return member_results unless member_results.empty?
  end

  ranked_plain_results
end