Skip to content

Commit

Permalink
Run Rubocop across the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
dgutov committed Mar 27, 2024
1 parent 5788fe4 commit ef3134a
Show file tree
Hide file tree
Showing 32 changed files with 488 additions and 440 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'rspec', '~> 3.4.0'
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rake'
require 'rspec/core/rake_task'

Expand Down
8 changes: 5 additions & 3 deletions lib/robe.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'robe/sash'
require 'robe/server'

Expand All @@ -10,7 +12,7 @@ def start(port = 0, host = '127.0.0.1')

@server = Server.new(Sash.new, host, port)

['INT', 'TERM'].each do |signal|
%w[INT TERM].each do |signal|
trap(signal) { stop }
end

Expand All @@ -27,7 +29,7 @@ def start(port = 0, host = '127.0.0.1')

server_thread.abort_on_exception = true

@server && @server.start
@server&.start
end

@server.wait_for_it
Expand All @@ -36,7 +38,7 @@ def start(port = 0, host = '127.0.0.1')
end

def stop
@server && @server.shutdown
@server&.shutdown
@server = nil
end

Expand Down
26 changes: 9 additions & 17 deletions lib/robe/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
# frozen_string_literal: true

class Module
unless method_defined?(:__name__)
alias_method :__name__, :name
end
alias __name__ name unless method_defined?(:__name__)

unless method_defined?(:__singleton_class__)
alias_method :__singleton_class__, :singleton_class
end
alias __singleton_class__ singleton_class unless method_defined?(:__singleton_class__)

unless method_defined?(:__include__?)
alias_method :__include__?, :include?
end
alias __include__? include? unless method_defined?(:__include__?)

unless method_defined?(:__instance_methods__)
alias_method :__instance_methods__, :instance_methods
end
alias __instance_methods__ instance_methods unless method_defined?(:__instance_methods__)

unless method_defined?(:__public_instance_methods__)
alias_method :__public_instance_methods__, :public_instance_methods
end
alias __public_instance_methods__ public_instance_methods unless method_defined?(:__public_instance_methods__)

unless method_defined?(:__protected_instance_methods__)
alias_method :__protected_instance_methods__, :protected_instance_methods
alias __protected_instance_methods__ protected_instance_methods
end

unless method_defined?(:__private_instance_methods__)
alias_method :__private_instance_methods__, :private_instance_methods
alias __private_instance_methods__ private_instance_methods
end
end
2 changes: 2 additions & 0 deletions lib/robe/jvisor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'robe/visor'

module Robe
Expand Down
35 changes: 17 additions & 18 deletions lib/robe/sash.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'robe/sash/doc_for'
require 'robe/sash/const_locations'
require 'robe/type_space'
Expand Down Expand Up @@ -30,10 +32,10 @@ def targets(obj)
module_methods = obj.methods.map { |m| method_spec(obj.method(m)) }
instance_methods = (obj.__instance_methods__ +
obj.__private_instance_methods__(false))
.map { |m| method_spec(obj.instance_method(m)) }
.map { |m| method_spec(obj.instance_method(m)) }
[name_cache[obj]] + module_methods + instance_methods
else
self.targets(obj.class.to_s)
targets(obj.class.to_s)
end
end

Expand All @@ -42,14 +44,13 @@ def find_method(mod, inst, sym)
end

def find_method_owner(mod, inst, sym)
begin
find_method(mod, inst, sym).owner
rescue NameError
end
find_method(mod, inst, sym).owner
rescue NameError
end

def method_spec(method)
owner, inst = method.owner, nil
owner = method.owner
inst = nil
if owner.singleton_class?
# FIXME: Use https://docs.ruby-lang.org/en/3.2/Class.html#method-i-attached_object
name = owner.to_s[/Class:([A-Z][^\(> ]*)/, 1] # defined in an eigenclass
Expand Down Expand Up @@ -97,28 +98,26 @@ def method_targets(name, target, mod, instance, superc, conservative)
end

if targets.empty? && (target || !conservative) && !special_method
unless target
scanner.scan_methods(Kernel, :__private_instance_methods__)
end
scanner.scan_methods(Kernel, :__private_instance_methods__) unless target
scanner.check_private = false
scanner.scan(visor.each_object(Module), true, true)
targets = scanner.candidates
end

if !target
unless target
scanner.scan_methods(Object, :__instance_methods__)
scanner.scan_methods(Object, :__private_instance_methods__)
end

targets.map { |method| method_spec(method) }
.sort_by { |(mname)| mname ? mname.scan(/::/).length : 99 }
.sort_by { |(mname)| mname ? mname.scan(/::/).length : 99 }
end

def filter_targets!(space, targets, instance, sym)
owner = find_method_owner(space.target_type, instance, sym)
if owner
targets.reject! do |method|
!(method.owner <= owner) &&
method.owner > owner &&
targets.find { |other| other.owner < method.owner }
end
end
Expand All @@ -130,7 +129,7 @@ def complete_method(prefix, target, mod, instance)

space.scan_with(scanner)

if !target
unless target
scanner.scan_methods(Object, :__instance_methods__)
scanner.scan_methods(Object, :__private_instance_methods__)
end
Expand All @@ -144,7 +143,7 @@ def complete_method(prefix, target, mod, instance)
end

def complete_const(prefix, mod)
colons = prefix.rindex("::")
colons = prefix.rindex('::')
tail = colons ? prefix[colons + 2..-1] : prefix
if !colons
path = [Object]
Expand Down Expand Up @@ -194,8 +193,8 @@ def ping
{pong: true}
end

def call(path, body)
_, endpoint, *args = path.split("/").map { |s| s == "-" ? nil : s }
def call(path, _body)
_, endpoint, *args = path.split('/').map { |s| s == '-' ? nil : s }
value = public_send(endpoint.to_sym, *args)

# Yajl 1.4.1 encoder is weirdly slow on our data.
Expand All @@ -209,7 +208,7 @@ def call(path, body)
private

def pick_visor
if RUBY_ENGINE == "jruby"
if RUBY_ENGINE == 'jruby'
JVisor.new
else
Visor.new
Expand Down
22 changes: 13 additions & 9 deletions lib/robe/sash/const_locations.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'robe/core_ext'
require 'rubygems'

Expand All @@ -20,16 +22,16 @@ def all(name, mod)
obj.private_instance_methods(false)).map { |m| obj.instance_method(m) }

methods.each do |m|
if (loc = m.source_location)
path = loc[0]
next unless (loc = m.source_location)

path = loc[0]

# Kernel.instance_method(:warn).source_location[0], Ruby 3
# or Sinatra::Base.sessions
next if path.start_with?('<internal:', '(eval)')
# Kernel.instance_method(:warn).source_location[0], Ruby 3
# or Sinatra::Base.sessions
next if path.start_with?('<internal:', '(eval)')

locations[path] ||= 0
locations[path] += 1
end
locations[path] ||= 0
locations[path] += 1
end
end

Expand Down Expand Up @@ -65,7 +67,7 @@ def full_scan(obj)
output, _stderr, status = Open3.capture3(command, stdin_data: files.join("\0"))

if status.exitstatus == 127
puts "Install GNU grep and xargs for faster full scans"
puts 'Install GNU grep and xargs for faster full scans'
files.select { |f| File.read(f).match(re) }
else
output.split("\0")
Expand All @@ -74,13 +76,15 @@ def full_scan(obj)

def filter_locations_by_module(files, obj)
return files if obj.nil? || obj.name.nil?

re = definition_re(obj)
files.select { |file| File.read(file).match(re) }
end

def resolve_name(name, mod)
obj = visor.resolve_context(name, mod)
return [obj.name, obj] if obj.is_a?(Module)

matches = /^(?:(.*)::)?([^:]*)/.match(name)
mod_part = matches[1]
base_name = matches[2]
Expand Down
41 changes: 21 additions & 20 deletions lib/robe/sash/doc_for.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require 'pry'
require 'ostruct'

begin
require 'pry-doc' if RUBY_ENGINE == "ruby"
require 'pry-doc' if RUBY_ENGINE == 'ruby'
rescue LoadError
# Whatever, it's optional.
end
Expand All @@ -23,7 +25,8 @@ def format
end

def visibility
owner, name = @method.owner, @method.name
owner = @method.owner
name = @method.name
if owner.__public_instance_methods__(false).include?(name)
:public
elsif owner.__protected_instance_methods__(false).include?(name)
Expand All @@ -34,25 +37,23 @@ def visibility
end

def self.method_struct(method)
begin
info = Pry::Method.new(method)

aliases = info.aliases.map(&:to_sym)

if info.dynamically_defined?
doc = ""
source = "# This method was defined outside of a source file."
else
doc = info.doc
source = (info.source? ? info.source : "# Not available.")
end

OpenStruct.new(docstring: doc, source: source,
aliases: aliases)
rescue Pry::CommandError
message = $!.message =~ /pry-doc/ ? $!.message : ""
return OpenStruct.new(docstring: message, aliases: aliases)
info = Pry::Method.new(method)

aliases = info.aliases.map(&:to_sym)

if info.dynamically_defined?
doc = ''
source = '# This method was defined outside of a source file.'
else
doc = info.doc
source = (info.source? ? info.source : '# Not available.')
end

OpenStruct.new(docstring: doc, source: source,
aliases: aliases)
rescue Pry::CommandError
message = $ERROR_INFO.message =~ /pry-doc/ ? $ERROR_INFO.message : ''
OpenStruct.new(docstring: message, aliases: aliases)
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/robe/sash/includes_tracker.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'robe/core_ext'

module Robe
Expand Down Expand Up @@ -32,12 +34,12 @@ def rescan_maybe
sc.included_modules.each { |imod| hosts[imod] << [mod, nil] }
end

self.hosts
hosts
end

def current_vm_stats
if RUBY_ENGINE == 'jruby'
JRuby::Util::cache_stats
JRuby::Util.cache_stats
else
RubyVM.stat
end
Expand Down
8 changes: 4 additions & 4 deletions lib/robe/scanners.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'robe/core_ext'

module Robe
Expand Down Expand Up @@ -28,16 +30,14 @@ def scan(modules, check_instance, check_module)

class ModuleScanner < Scanner
def scan_methods(mod, coll)
if mod.__send__(coll, false).include?(@sym)
candidates << mod.instance_method(@sym)
end
candidates << mod.instance_method(@sym) if mod.__send__(coll, false).include?(@sym)
end
end

class MethodScanner < Scanner
def initialize(*args)
super
@re = /^#{Regexp.escape(@sym)}/ if !@sym.to_s.empty?
@re = /^#{Regexp.escape(@sym)}/ unless @sym.to_s.empty?
end

def scan_methods(mod, coll)
Expand Down
Loading

0 comments on commit ef3134a

Please sign in to comment.