rbs-merge 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +46 -0
- data/CITATION.cff +20 -0
- data/CODE_OF_CONDUCT.md +134 -0
- data/CONTRIBUTING.md +227 -0
- data/FUNDING.md +74 -0
- data/LICENSE.txt +21 -0
- data/README.md +936 -0
- data/REEK +0 -0
- data/RUBOCOP.md +71 -0
- data/SECURITY.md +21 -0
- data/lib/rbs/merge/conflict_resolver.rb +99 -0
- data/lib/rbs/merge/debug_logger.rb +24 -0
- data/lib/rbs/merge/file_aligner.rb +148 -0
- data/lib/rbs/merge/file_analysis.rb +264 -0
- data/lib/rbs/merge/freeze_node.rb +117 -0
- data/lib/rbs/merge/merge_result.rb +171 -0
- data/lib/rbs/merge/smart_merger.rb +266 -0
- data/lib/rbs/merge/version.rb +12 -0
- data/lib/rbs/merge.rb +82 -0
- data/lib/rbs-merge.rb +4 -0
- data/sig/rbs/merge.rbs +203 -0
- data.tar.gz.sig +0 -0
- metadata +318 -0
- metadata.gz.sig +0 -0
data/REEK
ADDED
|
File without changes
|
data/RUBOCOP.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# RuboCop Usage Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
A tale of two RuboCop plugin gems.
|
|
6
|
+
|
|
7
|
+
### RuboCop Gradual
|
|
8
|
+
|
|
9
|
+
This project uses `rubocop_gradual` instead of vanilla RuboCop for code style checking. The `rubocop_gradual` tool allows for gradual adoption of RuboCop rules by tracking violations in a lock file.
|
|
10
|
+
|
|
11
|
+
### RuboCop LTS
|
|
12
|
+
|
|
13
|
+
This project uses `rubocop-lts` to ensure, on a best-effort basis, compatibility with Ruby >= 1.9.2.
|
|
14
|
+
RuboCop rules are meticulously configured by the `rubocop-lts` family of gems to ensure that a project is compatible with a specific version of Ruby. See: https://rubocop-lts.gitlab.io for more.
|
|
15
|
+
|
|
16
|
+
## Checking RuboCop Violations
|
|
17
|
+
|
|
18
|
+
To check for RuboCop violations in this project, always use:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bundle exec rake rubocop_gradual:check
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Do not use** the standard RuboCop commands like:
|
|
25
|
+
- `bundle exec rubocop`
|
|
26
|
+
- `rubocop`
|
|
27
|
+
|
|
28
|
+
## Understanding the Lock File
|
|
29
|
+
|
|
30
|
+
The `.rubocop_gradual.lock` file tracks all current RuboCop violations in the project. This allows the team to:
|
|
31
|
+
|
|
32
|
+
1. Prevent new violations while gradually fixing existing ones
|
|
33
|
+
2. Track progress on code style improvements
|
|
34
|
+
3. Ensure CI builds don't fail due to pre-existing violations
|
|
35
|
+
|
|
36
|
+
## Common Commands
|
|
37
|
+
|
|
38
|
+
- **Check violations**
|
|
39
|
+
- `bundle exec rake rubocop_gradual`
|
|
40
|
+
- `bundle exec rake rubocop_gradual:check`
|
|
41
|
+
- **(Safe) Autocorrect violations, and update lockfile if no new violations**
|
|
42
|
+
- `bundle exec rake rubocop_gradual:autocorrect`
|
|
43
|
+
- **Force update the lock file (w/o autocorrect) to match violations present in code**
|
|
44
|
+
- `bundle exec rake rubocop_gradual:force_update`
|
|
45
|
+
|
|
46
|
+
## Workflow
|
|
47
|
+
|
|
48
|
+
1. Before submitting a PR, run `bundle exec rake rubocop_gradual:autocorrect`
|
|
49
|
+
a. or just the default `bundle exec rake`, as autocorrection is a pre-requisite of the default task.
|
|
50
|
+
2. If there are new violations, either:
|
|
51
|
+
- Fix them in your code
|
|
52
|
+
- Run `bundle exec rake rubocop_gradual:force_update` to update the lock file (only for violations you can't fix immediately)
|
|
53
|
+
3. Commit the updated `.rubocop_gradual.lock` file along with your changes
|
|
54
|
+
|
|
55
|
+
## Never add inline RuboCop disables
|
|
56
|
+
|
|
57
|
+
Do not add inline `rubocop:disable` / `rubocop:enable` comments anywhere in the codebase (including specs, except when following the few existing `rubocop:disable` patterns for a rule already being disabled elsewhere in the code). We handle exceptions in two supported ways:
|
|
58
|
+
|
|
59
|
+
- Permanent/structural exceptions: prefer adjusting the RuboCop configuration (e.g., in `.rubocop.yml`) to exclude a rule for a path or file pattern when it makes sense project-wide.
|
|
60
|
+
- Temporary exceptions while improving code: record the current violations in `.rubocop_gradual.lock` via the gradual workflow:
|
|
61
|
+
- `bundle exec rake rubocop_gradual:autocorrect` (preferred; will autocorrect what it can and update the lock only if no new violations were introduced)
|
|
62
|
+
- If needed, `bundle exec rake rubocop_gradual:force_update` (as a last resort when you cannot fix the newly reported violations immediately)
|
|
63
|
+
|
|
64
|
+
In general, treat the rules as guidance to follow; fix violations rather than ignore them. For example, RSpec conventions in this project expect `described_class` to be used in specs that target a specific class under test.
|
|
65
|
+
|
|
66
|
+
## Benefits of rubocop_gradual
|
|
67
|
+
|
|
68
|
+
- Allows incremental adoption of code style rules
|
|
69
|
+
- Prevents CI failures due to pre-existing violations
|
|
70
|
+
- Provides a clear record of code style debt
|
|
71
|
+
- Enables focused efforts on improving code quality over time
|
data/SECURITY.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
| Version | Supported |
|
|
6
|
+
|----------|-----------|
|
|
7
|
+
| 1.latest | ✅ |
|
|
8
|
+
|
|
9
|
+
## Security contact information
|
|
10
|
+
|
|
11
|
+
To report a security vulnerability, please use the
|
|
12
|
+
[Tidelift security contact](https://tidelift.com/security).
|
|
13
|
+
Tidelift will coordinate the fix and disclosure.
|
|
14
|
+
|
|
15
|
+
## Additional Support
|
|
16
|
+
|
|
17
|
+
If you are interested in support for versions older than the latest release,
|
|
18
|
+
please consider sponsoring the project / maintainer @ https://liberapay.com/pboling/donate,
|
|
19
|
+
or find other sponsorship links in the [README].
|
|
20
|
+
|
|
21
|
+
[README]: README.md
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rbs
|
|
4
|
+
module Merge
|
|
5
|
+
# Resolves conflicts between template and destination declarations.
|
|
6
|
+
# Determines which version to use when both files have declarations
|
|
7
|
+
# with matching signatures but different content.
|
|
8
|
+
#
|
|
9
|
+
# @example Basic usage
|
|
10
|
+
# resolver = ConflictResolver.new(
|
|
11
|
+
# preference: :destination,
|
|
12
|
+
# template_analysis: template_analysis,
|
|
13
|
+
# dest_analysis: dest_analysis
|
|
14
|
+
# )
|
|
15
|
+
# winner = resolver.resolve(template_decl, dest_decl)
|
|
16
|
+
class ConflictResolver < ::Ast::Merge::ConflictResolverBase
|
|
17
|
+
# Initialize a conflict resolver
|
|
18
|
+
#
|
|
19
|
+
# @param preference [Symbol] Which version wins on conflict (:template or :destination)
|
|
20
|
+
# @param template_analysis [FileAnalysis] Analysis of the template file
|
|
21
|
+
# @param dest_analysis [FileAnalysis] Analysis of the destination file
|
|
22
|
+
def initialize(preference:, template_analysis:, dest_analysis:)
|
|
23
|
+
super(
|
|
24
|
+
strategy: :node,
|
|
25
|
+
preference: preference,
|
|
26
|
+
template_analysis: template_analysis,
|
|
27
|
+
dest_analysis: dest_analysis
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Resolve a conflict between template and destination declarations
|
|
32
|
+
#
|
|
33
|
+
# @param template_decl [Object] Template declaration
|
|
34
|
+
# @param dest_decl [Object] Destination declaration
|
|
35
|
+
# @param template_index [Integer] Index in template statements
|
|
36
|
+
# @param dest_index [Integer] Index in destination statements
|
|
37
|
+
# @return [Hash] Resolution result with :source and :declaration keys
|
|
38
|
+
def resolve(template_decl, dest_decl, template_index:, dest_index:)
|
|
39
|
+
# Freeze blocks always win (they represent protected content)
|
|
40
|
+
if freeze_node?(dest_decl)
|
|
41
|
+
return {source: :destination, declaration: dest_decl, decision: DECISION_FREEZE_BLOCK}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Check if declarations are identical
|
|
45
|
+
if declarations_identical?(template_decl, dest_decl)
|
|
46
|
+
# Prefer destination to minimize diffs
|
|
47
|
+
return {source: :destination, declaration: dest_decl, decision: DECISION_DESTINATION}
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Check if we should recursively merge (for container types)
|
|
51
|
+
if can_recursive_merge?(template_decl, dest_decl)
|
|
52
|
+
return {
|
|
53
|
+
source: :recursive,
|
|
54
|
+
template_declaration: template_decl,
|
|
55
|
+
dest_declaration: dest_decl,
|
|
56
|
+
decision: DECISION_RECURSIVE,
|
|
57
|
+
}
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Apply preference
|
|
61
|
+
case @preference
|
|
62
|
+
when :template
|
|
63
|
+
{source: :template, declaration: template_decl, decision: DECISION_TEMPLATE}
|
|
64
|
+
else # :destination (validated in initialize)
|
|
65
|
+
{source: :destination, declaration: dest_decl, decision: DECISION_DESTINATION}
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Check if two declarations are identical
|
|
70
|
+
# @param decl1 [Object] First declaration
|
|
71
|
+
# @param decl2 [Object] Second declaration
|
|
72
|
+
# @return [Boolean]
|
|
73
|
+
def declarations_identical?(decl1, decl2)
|
|
74
|
+
# Use RBS's built-in equality
|
|
75
|
+
decl1 == decl2
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Check if declarations can be recursively merged
|
|
79
|
+
# @param template_decl [Object] Template declaration
|
|
80
|
+
# @param dest_decl [Object] Destination declaration
|
|
81
|
+
# @return [Boolean]
|
|
82
|
+
def can_recursive_merge?(template_decl, dest_decl)
|
|
83
|
+
# Only container types can be recursively merged
|
|
84
|
+
container_types = [
|
|
85
|
+
RBS::AST::Declarations::Class,
|
|
86
|
+
RBS::AST::Declarations::Module,
|
|
87
|
+
RBS::AST::Declarations::Interface,
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
template_decl.class == dest_decl.class &&
|
|
91
|
+
container_types.any? { |type| template_decl.is_a?(type) } &&
|
|
92
|
+
template_decl.respond_to?(:members) &&
|
|
93
|
+
dest_decl.respond_to?(:members) &&
|
|
94
|
+
template_decl.members.any? &&
|
|
95
|
+
dest_decl.members.any?
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rbs
|
|
4
|
+
module Merge
|
|
5
|
+
# Debug logging utility for RBS merge operations.
|
|
6
|
+
# Extends Ast::Merge::DebugLogger for shared functionality.
|
|
7
|
+
#
|
|
8
|
+
# @example Enable debug logging
|
|
9
|
+
# ENV['RBS_MERGE_DEBUG'] = '1'
|
|
10
|
+
# # ... perform merge operations ...
|
|
11
|
+
#
|
|
12
|
+
# @example Time a block of code
|
|
13
|
+
# result = Rbs::Merge::DebugLogger.time("parsing") { parse_file }
|
|
14
|
+
#
|
|
15
|
+
# @see Ast::Merge::DebugLogger
|
|
16
|
+
module DebugLogger
|
|
17
|
+
extend Ast::Merge::DebugLogger
|
|
18
|
+
|
|
19
|
+
# RBS-specific configuration
|
|
20
|
+
self.env_var_name = "RBS_MERGE_DEBUG"
|
|
21
|
+
self.log_prefix = "[rbs-merge]"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rbs
|
|
4
|
+
module Merge
|
|
5
|
+
# Aligns declarations between template and destination files by matching signatures.
|
|
6
|
+
# Produces alignment information used by SmartMerger to combine files.
|
|
7
|
+
#
|
|
8
|
+
# @example Basic usage
|
|
9
|
+
# aligner = FileAligner.new(template_analysis, dest_analysis)
|
|
10
|
+
# alignment = aligner.align
|
|
11
|
+
# alignment.each do |entry|
|
|
12
|
+
# case entry[:type]
|
|
13
|
+
# when :match
|
|
14
|
+
# # Both files have this declaration
|
|
15
|
+
# when :template_only
|
|
16
|
+
# # Only in template
|
|
17
|
+
# when :dest_only
|
|
18
|
+
# # Only in destination
|
|
19
|
+
# end
|
|
20
|
+
# end
|
|
21
|
+
class FileAligner
|
|
22
|
+
# @return [FileAnalysis] Template file analysis
|
|
23
|
+
attr_reader :template_analysis
|
|
24
|
+
|
|
25
|
+
# @return [FileAnalysis] Destination file analysis
|
|
26
|
+
attr_reader :dest_analysis
|
|
27
|
+
|
|
28
|
+
# Initialize a file aligner
|
|
29
|
+
#
|
|
30
|
+
# @param template_analysis [FileAnalysis] Analysis of the template file
|
|
31
|
+
# @param dest_analysis [FileAnalysis] Analysis of the destination file
|
|
32
|
+
def initialize(template_analysis, dest_analysis)
|
|
33
|
+
@template_analysis = template_analysis
|
|
34
|
+
@dest_analysis = dest_analysis
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Perform alignment between template and destination statements
|
|
38
|
+
#
|
|
39
|
+
# @return [Array<Hash>] Alignment entries with type, indices, and declarations
|
|
40
|
+
def align
|
|
41
|
+
template_statements = @template_analysis.statements
|
|
42
|
+
dest_statements = @dest_analysis.statements
|
|
43
|
+
|
|
44
|
+
# Build signature maps
|
|
45
|
+
template_by_sig = build_signature_map(template_statements, @template_analysis)
|
|
46
|
+
dest_by_sig = build_signature_map(dest_statements, @dest_analysis)
|
|
47
|
+
|
|
48
|
+
# Track which indices have been matched
|
|
49
|
+
matched_template = Set.new
|
|
50
|
+
matched_dest = Set.new
|
|
51
|
+
alignment = []
|
|
52
|
+
|
|
53
|
+
# First pass: find matches by signature
|
|
54
|
+
template_by_sig.each do |sig, template_indices|
|
|
55
|
+
next unless dest_by_sig.key?(sig)
|
|
56
|
+
|
|
57
|
+
dest_indices = dest_by_sig[sig]
|
|
58
|
+
|
|
59
|
+
# Match indices pairwise (first template with first dest, etc.)
|
|
60
|
+
template_indices.zip(dest_indices).each do |t_idx, d_idx|
|
|
61
|
+
next unless t_idx && d_idx
|
|
62
|
+
|
|
63
|
+
alignment << {
|
|
64
|
+
type: :match,
|
|
65
|
+
template_index: t_idx,
|
|
66
|
+
dest_index: d_idx,
|
|
67
|
+
signature: sig,
|
|
68
|
+
template_decl: template_statements[t_idx],
|
|
69
|
+
dest_decl: dest_statements[d_idx],
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
matched_template << t_idx
|
|
73
|
+
matched_dest << d_idx
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Second pass: add template-only entries
|
|
78
|
+
template_statements.each_with_index do |stmt, idx|
|
|
79
|
+
next if matched_template.include?(idx)
|
|
80
|
+
|
|
81
|
+
alignment << {
|
|
82
|
+
type: :template_only,
|
|
83
|
+
template_index: idx,
|
|
84
|
+
dest_index: nil,
|
|
85
|
+
signature: @template_analysis.signature_at(idx),
|
|
86
|
+
template_decl: stmt,
|
|
87
|
+
dest_decl: nil,
|
|
88
|
+
}
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Third pass: add dest-only entries
|
|
92
|
+
dest_statements.each_with_index do |stmt, idx|
|
|
93
|
+
next if matched_dest.include?(idx)
|
|
94
|
+
|
|
95
|
+
alignment << {
|
|
96
|
+
type: :dest_only,
|
|
97
|
+
template_index: nil,
|
|
98
|
+
dest_index: idx,
|
|
99
|
+
signature: @dest_analysis.signature_at(idx),
|
|
100
|
+
template_decl: nil,
|
|
101
|
+
dest_decl: stmt,
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Sort by appearance order (prefer destination order, then template)
|
|
106
|
+
sort_alignment(alignment)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
# Build a map from signatures to statement indices
|
|
112
|
+
# @param statements [Array] Statements to map
|
|
113
|
+
# @param analysis [FileAnalysis] File analysis for signature generation
|
|
114
|
+
# @return [Hash{Array => Array<Integer>}] Signature to indices map
|
|
115
|
+
def build_signature_map(statements, analysis)
|
|
116
|
+
map = Hash.new { |h, k| h[k] = [] }
|
|
117
|
+
|
|
118
|
+
statements.each_with_index do |_stmt, idx|
|
|
119
|
+
sig = analysis.signature_at(idx)
|
|
120
|
+
map[sig] << idx if sig
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
map
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Sort alignment entries for output
|
|
127
|
+
# @param alignment [Array<Hash>] Alignment entries
|
|
128
|
+
# @return [Array<Hash>] Sorted alignment
|
|
129
|
+
def sort_alignment(alignment)
|
|
130
|
+
alignment.sort_by do |entry|
|
|
131
|
+
case entry[:type]
|
|
132
|
+
when :match
|
|
133
|
+
# Sort by destination index for matches
|
|
134
|
+
[0, entry[:dest_index], entry[:template_index]]
|
|
135
|
+
when :dest_only
|
|
136
|
+
# Destination-only items sorted by their index
|
|
137
|
+
[1, entry[:dest_index], 0]
|
|
138
|
+
when :template_only
|
|
139
|
+
# Template-only items at the end, by template index
|
|
140
|
+
[2, entry[:template_index], 0]
|
|
141
|
+
else
|
|
142
|
+
[3, 0, 0]
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rbs
|
|
4
|
+
module Merge
|
|
5
|
+
# File analysis for RBS type signature files.
|
|
6
|
+
# Parses RBS source code and extracts declarations, members, and freeze blocks.
|
|
7
|
+
#
|
|
8
|
+
# This class provides the foundation for intelligent merging by:
|
|
9
|
+
# - Parsing RBS files using the official RBS parser
|
|
10
|
+
# - Extracting top-level declarations (classes, modules, interfaces, type aliases, constants)
|
|
11
|
+
# - Detecting freeze blocks marked with comment directives
|
|
12
|
+
# - Generating signatures for matching declarations between files
|
|
13
|
+
#
|
|
14
|
+
# @example Basic usage
|
|
15
|
+
# analysis = FileAnalysis.new(rbs_source)
|
|
16
|
+
# analysis.statements.each do |stmt|
|
|
17
|
+
# puts stmt.class
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# @example With custom freeze token
|
|
21
|
+
# analysis = FileAnalysis.new(source, freeze_token: "my-merge")
|
|
22
|
+
# # Looks for: # my-merge:freeze / # my-merge:unfreeze
|
|
23
|
+
class FileAnalysis
|
|
24
|
+
include Ast::Merge::FileAnalyzable
|
|
25
|
+
|
|
26
|
+
# Default freeze token for identifying freeze blocks
|
|
27
|
+
# @return [String]
|
|
28
|
+
DEFAULT_FREEZE_TOKEN = "rbs-merge"
|
|
29
|
+
|
|
30
|
+
# @return [RBS::Buffer] The RBS buffer for this file
|
|
31
|
+
attr_reader :buffer
|
|
32
|
+
|
|
33
|
+
# @return [Array<RBS::AST::Directives::Base>] RBS directives (use statements, etc.)
|
|
34
|
+
attr_reader :directives
|
|
35
|
+
|
|
36
|
+
# @return [Array<RBS::AST::Declarations::Base>] Raw declarations from parser
|
|
37
|
+
attr_reader :declarations
|
|
38
|
+
|
|
39
|
+
# Initialize file analysis with RBS parser
|
|
40
|
+
#
|
|
41
|
+
# @param source [String] RBS source code to analyze
|
|
42
|
+
# @param freeze_token [String] Token for freeze block markers (default: "rbs-merge")
|
|
43
|
+
# @param signature_generator [Proc, nil] Custom signature generator
|
|
44
|
+
# @raise [RBS::ParsingError] If the source has syntax errors
|
|
45
|
+
def initialize(source, freeze_token: DEFAULT_FREEZE_TOKEN, signature_generator: nil)
|
|
46
|
+
@source = source
|
|
47
|
+
@lines = source.split("\n", -1)
|
|
48
|
+
@freeze_token = freeze_token
|
|
49
|
+
@signature_generator = signature_generator
|
|
50
|
+
|
|
51
|
+
# Parse the RBS source
|
|
52
|
+
@buffer = RBS::Buffer.new(name: "merge.rbs", content: source)
|
|
53
|
+
@buffer, @directives, @declarations = DebugLogger.time("FileAnalysis#parse") do
|
|
54
|
+
RBS::Parser.parse_signature(@buffer)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Extract and integrate all nodes including freeze blocks
|
|
58
|
+
@statements = extract_and_integrate_all_nodes
|
|
59
|
+
|
|
60
|
+
DebugLogger.debug("FileAnalysis initialized", {
|
|
61
|
+
signature_generator: signature_generator ? "custom" : "default",
|
|
62
|
+
directives_count: @directives.size,
|
|
63
|
+
declarations_count: @declarations.size,
|
|
64
|
+
statements_count: @statements.size,
|
|
65
|
+
freeze_blocks: freeze_blocks.size,
|
|
66
|
+
})
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Check if parse was successful (RBS parser raises on failure, so always true if we get here)
|
|
70
|
+
# @return [Boolean]
|
|
71
|
+
def valid?
|
|
72
|
+
true
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Get all statements (declarations outside freeze blocks + FreezeNodes)
|
|
76
|
+
# @return [Array<RBS::AST::Declarations::Base, FreezeNode>]
|
|
77
|
+
attr_reader :statements
|
|
78
|
+
|
|
79
|
+
# Compute default signature for a node
|
|
80
|
+
# @param node [Object] The declaration or FreezeNode
|
|
81
|
+
# @return [Array, nil] Signature array
|
|
82
|
+
def compute_node_signature(node)
|
|
83
|
+
case node
|
|
84
|
+
when FreezeNode
|
|
85
|
+
node.signature
|
|
86
|
+
when RBS::AST::Declarations::Class
|
|
87
|
+
[:class, node.name.to_s]
|
|
88
|
+
when RBS::AST::Declarations::Module
|
|
89
|
+
[:module, node.name.to_s]
|
|
90
|
+
when RBS::AST::Declarations::Interface
|
|
91
|
+
[:interface, node.name.to_s]
|
|
92
|
+
when RBS::AST::Declarations::TypeAlias
|
|
93
|
+
[:type_alias, node.name.to_s]
|
|
94
|
+
when RBS::AST::Declarations::Constant
|
|
95
|
+
[:constant, node.name.to_s]
|
|
96
|
+
when RBS::AST::Declarations::Global
|
|
97
|
+
[:global, node.name.to_s]
|
|
98
|
+
when RBS::AST::Members::MethodDefinition
|
|
99
|
+
[:method, node.name.to_s, node.kind]
|
|
100
|
+
when RBS::AST::Members::Alias
|
|
101
|
+
[:alias, node.new_name.to_s, node.old_name.to_s]
|
|
102
|
+
when RBS::AST::Members::AttrReader
|
|
103
|
+
[:attr_reader, node.name.to_s]
|
|
104
|
+
when RBS::AST::Members::AttrWriter
|
|
105
|
+
[:attr_writer, node.name.to_s]
|
|
106
|
+
when RBS::AST::Members::AttrAccessor
|
|
107
|
+
[:attr_accessor, node.name.to_s]
|
|
108
|
+
when RBS::AST::Members::Include
|
|
109
|
+
[:include, node.name.to_s]
|
|
110
|
+
when RBS::AST::Members::Extend
|
|
111
|
+
[:extend, node.name.to_s]
|
|
112
|
+
when RBS::AST::Members::Prepend
|
|
113
|
+
[:prepend, node.name.to_s]
|
|
114
|
+
when RBS::AST::Members::InstanceVariable
|
|
115
|
+
[:ivar, node.name.to_s]
|
|
116
|
+
when RBS::AST::Members::ClassInstanceVariable
|
|
117
|
+
[:civar, node.name.to_s]
|
|
118
|
+
when RBS::AST::Members::ClassVariable
|
|
119
|
+
[:cvar, node.name.to_s]
|
|
120
|
+
else
|
|
121
|
+
# Unknown node type - use class name and location as signature
|
|
122
|
+
[:unknown, node.class.name, node.location&.start_line]
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Override to detect RBS nodes for signature generator fallthrough
|
|
127
|
+
# @param value [Object] The value to check
|
|
128
|
+
# @return [Boolean] true if this is a fallthrough node
|
|
129
|
+
def fallthrough_node?(value)
|
|
130
|
+
value.is_a?(RBS::AST::Declarations::Base) ||
|
|
131
|
+
value.is_a?(RBS::AST::Members::Base) ||
|
|
132
|
+
value.is_a?(FreezeNode) ||
|
|
133
|
+
super
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
# Extract all nodes and integrate freeze blocks
|
|
139
|
+
# @return [Array<Object>] Integrated list of declarations and freeze blocks
|
|
140
|
+
def extract_and_integrate_all_nodes
|
|
141
|
+
freeze_markers = find_freeze_markers
|
|
142
|
+
return @declarations.dup if freeze_markers.empty?
|
|
143
|
+
|
|
144
|
+
# Build freeze blocks from markers
|
|
145
|
+
freeze_block_nodes = build_freeze_blocks(freeze_markers)
|
|
146
|
+
return @declarations.dup if freeze_block_nodes.empty?
|
|
147
|
+
|
|
148
|
+
# Integrate declarations with freeze blocks
|
|
149
|
+
integrate_nodes_with_freeze_blocks(@declarations, freeze_block_nodes)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Find all freeze markers in the source
|
|
153
|
+
# @return [Array<Hash>] Array of marker info hashes
|
|
154
|
+
def find_freeze_markers
|
|
155
|
+
markers = []
|
|
156
|
+
# Use shared pattern from Ast::Merge::FreezeNodeBase with our specific token
|
|
157
|
+
pattern = Ast::Merge::FreezeNodeBase.pattern_for(:hash_comment, @freeze_token)
|
|
158
|
+
|
|
159
|
+
@lines.each_with_index do |line, idx|
|
|
160
|
+
line_num = idx + 1
|
|
161
|
+
next unless (match = line.match(pattern))
|
|
162
|
+
|
|
163
|
+
marker_type = match[1]&.downcase # 'freeze' or 'unfreeze'
|
|
164
|
+
if marker_type == "freeze"
|
|
165
|
+
markers << {type: :start, line: line_num, text: line}
|
|
166
|
+
elsif marker_type == "unfreeze"
|
|
167
|
+
markers << {type: :end, line: line_num, text: line}
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
markers
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Build freeze blocks from paired markers
|
|
175
|
+
# @param markers [Array<Hash>] Freeze markers
|
|
176
|
+
# @return [Array<FreezeNode>] Freeze block nodes
|
|
177
|
+
def build_freeze_blocks(markers)
|
|
178
|
+
blocks = []
|
|
179
|
+
stack = []
|
|
180
|
+
|
|
181
|
+
markers.each do |marker|
|
|
182
|
+
case marker[:type]
|
|
183
|
+
when :start
|
|
184
|
+
stack.push(marker)
|
|
185
|
+
when :end
|
|
186
|
+
if stack.any?
|
|
187
|
+
start_marker = stack.pop
|
|
188
|
+
# Find nodes contained in this freeze block
|
|
189
|
+
contained_nodes = find_contained_nodes(start_marker[:line], marker[:line])
|
|
190
|
+
overlapping_nodes = find_overlapping_nodes(start_marker[:line], marker[:line])
|
|
191
|
+
|
|
192
|
+
blocks << FreezeNode.new(
|
|
193
|
+
start_line: start_marker[:line],
|
|
194
|
+
end_line: marker[:line],
|
|
195
|
+
analysis: self,
|
|
196
|
+
nodes: contained_nodes,
|
|
197
|
+
overlapping_nodes: overlapping_nodes,
|
|
198
|
+
start_marker: start_marker[:text],
|
|
199
|
+
end_marker: marker[:text],
|
|
200
|
+
)
|
|
201
|
+
else
|
|
202
|
+
DebugLogger.warning("Unmatched freeze end marker at line #{marker[:line]}")
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
stack.each do |unmatched|
|
|
208
|
+
DebugLogger.warning("Unmatched freeze start marker at line #{unmatched[:line]}")
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
blocks
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Find declarations fully contained within a range
|
|
215
|
+
# @param start_line [Integer] Start line
|
|
216
|
+
# @param end_line [Integer] End line
|
|
217
|
+
# @return [Array<Object>] Contained declarations
|
|
218
|
+
def find_contained_nodes(start_line, end_line)
|
|
219
|
+
@declarations.select do |decl|
|
|
220
|
+
decl.location.start_line >= start_line && decl.location.end_line <= end_line
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Find declarations that overlap with a range
|
|
225
|
+
# @param start_line [Integer] Start line
|
|
226
|
+
# @param end_line [Integer] End line
|
|
227
|
+
# @return [Array<Object>] Overlapping declarations
|
|
228
|
+
def find_overlapping_nodes(start_line, end_line)
|
|
229
|
+
@declarations.select do |decl|
|
|
230
|
+
decl_start = decl.location.start_line
|
|
231
|
+
decl_end = decl.location.end_line
|
|
232
|
+
|
|
233
|
+
# Overlaps if not fully before and not fully after
|
|
234
|
+
!(decl_end < start_line || decl_start > end_line)
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Integrate declarations with freeze blocks, avoiding duplicates
|
|
239
|
+
# @param declarations [Array] Original declarations
|
|
240
|
+
# @param freeze_blocks [Array<FreezeNode>] Freeze blocks
|
|
241
|
+
# @return [Array] Integrated list sorted by line number
|
|
242
|
+
def integrate_nodes_with_freeze_blocks(declarations, freeze_blocks)
|
|
243
|
+
# Track which declarations are inside freeze blocks
|
|
244
|
+
frozen_declarations = freeze_blocks.flat_map(&:nodes).to_set
|
|
245
|
+
|
|
246
|
+
# Filter out frozen declarations and add freeze blocks
|
|
247
|
+
result = declarations.reject { |d| frozen_declarations.include?(d) }
|
|
248
|
+
result.concat(freeze_blocks)
|
|
249
|
+
|
|
250
|
+
# Sort by start line
|
|
251
|
+
result.sort_by do |node|
|
|
252
|
+
if node.is_a?(FreezeNode)
|
|
253
|
+
node.start_line
|
|
254
|
+
elsif node.respond_to?(:comment) && node.comment
|
|
255
|
+
# Include comment line in sorting if present
|
|
256
|
+
[node.comment.location.start_line, node.location.start_line].min
|
|
257
|
+
else
|
|
258
|
+
node.location.start_line
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|