Class: Json::Merge::FileAnalysis

Inherits:
Object
  • Object
show all
Includes:
Ast::Merge::FileAnalyzable
Defined in:
lib/json/merge/file_analysis.rb

Overview

Analyzes JSON file structure, extracting statements for merging.
This is the main analysis class that prepares JSON content for merging.

Examples:

Basic usage

analysis = FileAnalysis.new(json_source)
analysis.valid? # => true
analysis.statements # => [NodeWrapper, ...]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, signature_generator: nil, parser_path: nil, **options) ⇒ FileAnalysis

Initialize file analysis

Parameters:

  • source (String)

    JSON source code to analyze

  • signature_generator (Proc, nil) (defaults to: nil)

    Custom signature generator

  • parser_path (String, nil) (defaults to: nil)

    Path to tree-sitter-json parser library

  • options (Hash)

    Additional options (forward compatibility - freeze_token, node_typing, etc.)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/json/merge/file_analysis.rb', line 36

def initialize(source, signature_generator: nil, parser_path: nil, **options)
  @source = source
  @lines = source.lines.map(&:chomp)
  @signature_generator = signature_generator
  @parser_path = parser_path || self.class.find_parser_path
  @errors = []
  # **options captures any additional parameters (e.g., freeze_token, node_typing) for forward compatibility

  # Parse the JSON
  DebugLogger.time("FileAnalysis#parse_json") { parse_json }

  @statements = integrate_nodes

  DebugLogger.debug("FileAnalysis initialized", {
    signature_generator: signature_generator ? "custom" : "default",
    statements_count: @statements.size,
    valid: valid?,
  })
end

Instance Attribute Details

#astTreeHaver::Tree? (readonly)

Returns Parsed AST.

Returns:

  • (TreeHaver::Tree, nil)

    Parsed AST



16
17
18
# File 'lib/json/merge/file_analysis.rb', line 16

def ast
  @ast
end

#errorsArray (readonly)

Returns Parse errors if any.

Returns:

  • (Array)

    Parse errors if any



19
20
21
# File 'lib/json/merge/file_analysis.rb', line 19

def errors
  @errors
end

Class Method Details

.find_parser_pathString?

Find the parser library path using TreeHaver::GrammarFinder

Returns:

  • (String, nil)

    Path to the parser library or nil if not found



25
26
27
# File 'lib/json/merge/file_analysis.rb', line 25

def find_parser_path
  TreeHaver::GrammarFinder.new(:json).find_library_path
end

Instance Method Details

#fallthrough_node?(value) ⇒ Boolean

Override to detect tree-sitter nodes for signature generator fallthrough

Parameters:

  • value (Object)

    The value to check

Returns:

  • (Boolean)

    true if this is a fallthrough node



65
66
67
# File 'lib/json/merge/file_analysis.rb', line 65

def fallthrough_node?(value)
  value.is_a?(NodeWrapper) || super
end

#root_nodeNodeWrapper?

Get the root node of the parse tree

Returns:



71
72
73
74
75
# File 'lib/json/merge/file_analysis.rb', line 71

def root_node
  return unless valid?

  NodeWrapper.new(@ast.root_node, lines: @lines, source: @source)
end

#root_objectNodeWrapper?

Get the root object if the JSON document is an object

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/json/merge/file_analysis.rb', line 79

def root_object
  return unless valid?

  root = @ast.root_node
  return unless root

  # JSON root should be a document containing an object or array
  root.each do |child|
    if child.type.to_s == "object"
      return NodeWrapper.new(child, lines: @lines, source: @source)
    end
  end
  nil
end

#root_object_close_lineString?

Get the closing brace line of the root object (the line containing })

Returns:

  • (String, nil)


105
106
107
108
109
110
# File 'lib/json/merge/file_analysis.rb', line 105

def root_object_close_line
  obj = root_object
  return unless obj&.end_line

  line_at(obj.end_line)&.chomp
end

#root_object_open_lineString?

Get the opening brace line of the root object (the line containing {)

Returns:

  • (String, nil)


96
97
98
99
100
101
# File 'lib/json/merge/file_analysis.rb', line 96

def root_object_open_line
  obj = root_object
  return unless obj&.start_line

  line_at(obj.start_line)&.chomp
end

#root_pairsArray<NodeWrapper>

Get key-value pairs from the root object

Returns:



114
115
116
117
118
119
# File 'lib/json/merge/file_analysis.rb', line 114

def root_pairs
  obj = root_object
  return [] unless obj

  obj.pairs
end

#valid?Boolean

Check if parse was successful

Returns:

  • (Boolean)


58
59
60
# File 'lib/json/merge/file_analysis.rb', line 58

def valid?
  @errors.empty? && !@ast.nil?
end