Class: Tng::Analyzers::Other

Inherits:
Object
  • Object
show all
Defined in:
lib/tng/analyzers/other.rb

Constant Summary collapse

OTHER_DIRECTORIES =

Directories to scan for “other” files

%w[
  app/jobs
  app/helpers
  lib
  libs
  app/lib
  app/libs
  app/mailers
  app/channels
  app/decorators
  app/presenters
  app/serializers
  app/policies
  app/forms
  app/queries
  app/graphql
  app/graphql/resolvers
  app/graphql/types
  app/graphql/mutations
  app/graphql/loaders
  app/graphql/schemas
].freeze

Class Method Summary collapse

Class Method Details

.build_minitest_test_paths(file_type, file_name) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/tng/analyzers/other.rb', line 208

def self.build_minitest_test_paths(file_type, file_name)
  case file_type
  when "job"
    ["test/jobs/#{file_name}_test.rb"]
  when "helper"
    ["test/helpers/#{file_name}_test.rb"]
  when "lib"
    ["test/lib/#{file_name}_test.rb"]
  when "mailer"
    ["test/mailers/#{file_name}_test.rb"]
  when "channel"
    ["test/channels/#{file_name}_test.rb"]
  when "query"
    ["test/queries/#{file_name}_test.rb"]
  when "decorator"
    ["test/decorators/#{file_name}_test.rb"]
  when "presenter"
    ["test/presenters/#{file_name}_test.rb"]
  when "serializer"
    ["test/serializers/#{file_name}_test.rb"]
  when "policy"
    ["test/policies/#{file_name}_test.rb"]
  when "form"
    ["test/forms/#{file_name}_test.rb"]
  when "resolver"
    ["test/graphql/resolvers/#{file_name}_test.rb"]
  when "type"
    ["test/graphql/types/#{file_name}_test.rb"]
  when "mutation"
    ["test/graphql/mutations/#{file_name}_test.rb"]
  when "loader"
    ["test/graphql/loaders/#{file_name}_test.rb"]
  when "schema"
    ["test/graphql/schemas/#{file_name}_test.rb"]
  when "graphql"
    ["test/graphql/#{file_name}_test.rb"]
  else
    ["test/#{file_type}s/#{file_name}_test.rb"]
  end
end

.build_rspec_test_paths(file_type, file_name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/tng/analyzers/other.rb', line 167

def self.build_rspec_test_paths(file_type, file_name)
  case file_type
  when "job"
    ["spec/jobs/#{file_name}_spec.rb"]
  when "helper"
    ["spec/helpers/#{file_name}_spec.rb"]
  when "lib"
    ["spec/lib/#{file_name}_spec.rb"]
  when "mailer"
    ["spec/mailers/#{file_name}_spec.rb"]
  when "channel"
    ["spec/channels/#{file_name}_spec.rb"]
  when "query"
    ["spec/queries/#{file_name}_spec.rb"]
  when "decorator"
    ["spec/decorators/#{file_name}_spec.rb"]
  when "presenter"
    ["spec/presenters/#{file_name}_spec.rb"]
  when "serializer"
    ["spec/serializers/#{file_name}_spec.rb"]
  when "policy"
    ["spec/policies/#{file_name}_spec.rb"]
  when "form"
    ["spec/forms/#{file_name}_spec.rb"]
  when "resolver"
    ["spec/graphql/resolvers/#{file_name}_spec.rb"]
  when "type"
    ["spec/graphql/types/#{file_name}_spec.rb"]
  when "mutation"
    ["spec/graphql/mutations/#{file_name}_spec.rb"]
  when "loader"
    ["spec/graphql/loaders/#{file_name}_spec.rb"]
  when "schema"
    ["spec/graphql/schemas/#{file_name}_spec.rb"]
  when "graphql"
    ["spec/graphql/#{file_name}_spec.rb"]
  else
    ["spec/#{file_type}s/#{file_name}_spec.rb"]
  end
end

.determine_file_type(file_path) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/tng/analyzers/other.rb', line 126

def self.determine_file_type(file_path)
  case file_path
  when %r{app/jobs}
    "job"
  when %r{app/helpers}
    "helper"
  when %r{(?:app/)?libs?(?:/|$)}
    "lib"
  when %r{app/mailers}
    "mailer"
  when %r{app/channels}
    "channel"
  when %r{app/decorators}
    "decorator"
  when %r{app/presenters}
    "presenter"
  when %r{app/serializers}
    "serializer"
  when %r{app/policies}
    "policy"
  when %r{app/forms}
    "form"
  when %r{app/queries}
    "query"
  when %r{app/graphql/resolvers}
    "resolver"
  when %r{app/graphql/types}
    "type"
  when %r{app/graphql/mutations}
    "mutation"
  when %r{app/graphql/loaders}
    "loader"
  when %r{app/graphql/schemas}
    "schema"
  when %r{app/graphql}
    "graphql"
  else
    "other"
  end
end

.extract_public_method_names(node, methods, current_visibility = :public) ⇒ Object



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
# File 'lib/tng/analyzers/other.rb', line 77

def self.extract_public_method_names(node, methods, current_visibility = :public)
  return unless node.is_a?(Prism::Node)

  case node
  when Prism::DefNode
    # Only add methods that are public
    methods << node.name.to_s if current_visibility == :public
  when Prism::CallNode
    # Handle visibility modifiers (private, protected, public)
    if node.receiver.nil? && node.arguments.nil?
      case node.name
      when :private
        current_visibility = :private
      when :protected
        current_visibility = :protected
      when :public
        current_visibility = :public
      end

    end
  when Prism::SingletonClassNode
    # Methods inside class << self blocks - always public by default
    node.body&.child_nodes&.each do |child|
      extract_public_method_names(child, methods, :public)
    end
  end

  # Recursively process child nodes with current visibility
  node.child_nodes.each do |child|
    extract_public_method_names(child, methods, current_visibility)
  end
end

.files_in_dir(dir = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tng/analyzers/other.rb', line 30

def self.files_in_dir(dir = nil)
  base_dirs = if dir.nil?
                OTHER_DIRECTORIES.select { |d| Dir.exist?(File.join(Dir.pwd, d)) }
              else
                [dir]
              end

  return [] if base_dirs.empty?

  base_dirs.flat_map do |base_dir|
    full_path = File.join(Dir.pwd, base_dir)
    find_other_files(full_path, base_dir)
  end
end

.find_other_files(dir, _base_dir) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tng/analyzers/other.rb', line 110

def self.find_other_files(dir, _base_dir)
  return [] unless Dir.exist?(dir)

  Dir.glob(File.join(dir, "**", "*.rb")).map do |file_path|
    relative_path = file_path.gsub("#{Dir.pwd}/", "")
    file_type = determine_file_type(file_path)

    {
      file: File.basename(file_path),
      path: file_path,
      relative_path: relative_path,
      type: file_type
    }
  end
end

.methods_for_other(other_name, file_path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tng/analyzers/other.rb', line 53

def self.methods_for_other(other_name, file_path)
  raise "other_name is required" if other_name.nil?
  raise "file_path is required" if file_path.nil?

  begin
    # Try to load the class/module if possible
    if File.exist?(file_path)
      source_code = File.read(file_path)
      result = Prism.parse(source_code)

      public_methods = []
      extract_public_method_names(result.value, public_methods, :public)

      # Return the public methods found in the source file
      public_methods.map { |method_name| { name: method_name.to_s } }
    else
      []
    end
  rescue StandardError => e
    puts "❌ Error analyzing file #{other_name}: #{e.message}"
    []
  end
end

.value_for_other(file_path) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/tng/analyzers/other.rb', line 45

def self.value_for_other(file_path)
  raise "file_path is required" if file_path.nil?

  # For now, use the same parsing logic as services
  # We can enhance this later if needed for specific file types
  Tng::Analyzer::Service.parse_service_file(file_path)
end