Skip to content

Commit 0df6103

Browse files
authored
Use the package name to differentiate duplicate files (#69)
1 parent a05d773 commit 0df6103

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

lib/reaper/code_deleter.rb

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def delete_types(types)
1717
types.each do |class_info|
1818
Logger.info "Deleting #{class_info['class_name']}"
1919

20-
type_name = parse_type_name(class_info['class_name'])
20+
type_name_result = parse_type_name(class_info['class_name'])
21+
type_name = type_name_result[:type_name]
22+
package_name = type_name_result[:package_name]
2123
Logger.debug "Parsed type name: #{type_name}"
2224

2325
# Remove line number from path if present
@@ -42,7 +44,7 @@ def delete_types(types)
4244
paths.each do |path|
4345
Logger.debug "Processing path: #{path}"
4446
@profiler.measure('delete_type_from_file') do
45-
delete_type_from_file(path, type_name)
47+
delete_type_from_file(path, type_name, package_name)
4648
end
4749
end
4850

@@ -71,32 +73,43 @@ def delete_types(types)
7173
private
7274

7375
def parse_type_name(type_name)
76+
package_name = nil
77+
7478
# Remove first module prefix for Swift types if present
7579
if @platform == 'ios' && type_name.include?('.')
76-
type_name.split('.')[1..].join('.')
80+
parsed_type_name = type_name.split('.')[1..].join('.')
7781
# For Android, strip package name and just use the class name
7882
elsif @platform == 'android' && type_name.include?('.')
7983
# rubocop:disable Layout/LineLength
8084
# Handle cases like "com.emergetools.hackernews.data.remote.ItemResponse $NullResponse (HackerNewsBaseClient.kt)"
8185
# rubocop:enable Layout/LineLength
8286
has_nested_class = type_name.include?('$')
8387
parts = type_name.split
88+
8489
if parts.length == 0
85-
type_name
90+
parsed_type_name = type_name
8691
elsif has_nested_class && parts.length > 1
87-
base_name = parts[0].split('.').last
92+
full_class_path = parts[0].split('.')
93+
base_name = full_class_path.last
8894
nested_class = parts[1].match(/\$(.+)/).captures.first
89-
"#{base_name}.#{nested_class}"
95+
parsed_type_name = "#{base_name}.#{nested_class}"
96+
# Extract package name (everything except the last part)
97+
package_name = full_class_path[0...-1].join('.') if full_class_path.length > 1
9098
else
91-
parts[0].split('.').last
99+
full_class_path = parts[0].split('.')
100+
parsed_type_name = full_class_path.last
101+
# Extract package name (everything except the last part)
102+
package_name = full_class_path[0...-1].join('.') if full_class_path.length > 1
92103
end
93104
else
94-
type_name
105+
parsed_type_name = type_name
95106
end
107+
108+
{ type_name: parsed_type_name, package_name: package_name }
96109
end
97110

98-
def delete_type_from_file(path, type_name)
99-
full_path = resolve_file_path(path)
111+
def delete_type_from_file(path, type_name, marker = nil)
112+
full_path = resolve_file_path(path, marker)
100113
return unless full_path
101114

102115
Logger.debug "Processing file: #{full_path}"
@@ -221,7 +234,7 @@ def delete_usages_from_file(path, type_name)
221234
end
222235
end
223236

224-
def resolve_file_path(path)
237+
def resolve_file_path(path, marker = nil)
225238
# If path starts with /, treat it as relative to project root
226239
if path.start_with?('/')
227240
path = path[1..] # Remove leading slash
@@ -242,7 +255,21 @@ def resolve_file_path(path)
242255
Logger.warn "Could not find #{path} in project"
243256
return nil
244257
elsif matching_files.length > 1
245-
Logger.warn "Found multiple matches for #{path}: #{matching_files.join(', ')}"
258+
Logger.debug "Found multiple matches for #{path}: #{matching_files.join(', ')}"
259+
260+
# If a marker is provided, use it to select the file
261+
# For Android, this is the package name declaration of the type
262+
if marker
263+
Logger.debug "Using marker #{marker} to select file"
264+
marker_files = matching_files.select { |file| File.read(file).include?(marker) }
265+
if marker_files.length >= 1
266+
Logger.info "Found #{marker_files.length} files with marker #{marker} for #{path}, using first match"
267+
return marker_files.first
268+
else
269+
Logger.warn "No files found with marker #{marker} for #{path}"
270+
end
271+
end
272+
246273
Logger.warn "Using first match: #{matching_files.first}"
247274
end
248275

0 commit comments

Comments
 (0)