Skip to content

Commit

Permalink
Remove internal-only methods from Command::Base
Browse files Browse the repository at this point in the history
Command#ruby_args and Command#unwrap_string_literal are used for default command's argument backward compatibility.
Moved these methods to another module to avoid being used from custom commands.
  • Loading branch information
tompng committed Apr 15, 2024
1 parent 8886434 commit 808cadb
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 19 deletions.
19 changes: 0 additions & 19 deletions lib/irb/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,6 @@ def initialize(irb_context)

attr_reader :irb_context

def unwrap_string_literal(str)
return if str.empty?

sexp = Ripper.sexp(str)
if sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
@irb_context.workspace.binding.eval(str).to_s
else
str
end
end

def ruby_args(arg)
# Use throw and catch to handle arg that includes `;`
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
catch(:EXTRACT_RUBY_ARGS) do
@irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
end || [[], {}]
end

def execute(arg)
#nop
end
Expand Down
2 changes: 2 additions & 0 deletions lib/irb/command/edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module IRB

module Command
class Edit < Base
include IRB::Command::RubyArgsExtractor

category "Misc"
description 'Open a file or source location.'
help_message <<~HELP_MESSAGE
Expand Down
27 changes: 27 additions & 0 deletions lib/irb/command/internal_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module IRB
module Command
# Internal use only, for default command's backward compatibility.
module RubyArgsExtractor # :nodoc:
def unwrap_string_literal(str)
return if str.empty?

sexp = Ripper.sexp(str)
if sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
@irb_context.workspace.binding.eval(str).to_s
else
str
end
end

def ruby_args(arg)
# Use throw and catch to handle arg that includes `;`
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
catch(:EXTRACT_RUBY_ARGS) do
@irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
end || [[], {}]
end
end
end
end
1 change: 1 addition & 0 deletions lib/irb/command/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module IRB

module Command
class LoaderCommand < Base
include RubyArgsExtractor
include IrbLoader

def raise_cmd_argument_error
Expand Down
2 changes: 2 additions & 0 deletions lib/irb/command/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module IRB

module Command
class Ls < Base
include RubyArgsExtractor

category "Context"
description "Show methods, constants, and variables."

Expand Down
2 changes: 2 additions & 0 deletions lib/irb/command/measure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module IRB

module Command
class Measure < Base
include RubyArgsExtractor

category "Misc"
description "`measure` enables the mode to measure processing time. `measure :off` disables it."

Expand Down
2 changes: 2 additions & 0 deletions lib/irb/command/show_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module IRB
module Command
class ShowDoc < Base
include RubyArgsExtractor

category "Context"
description "Look up documentation with RI."

Expand Down
2 changes: 2 additions & 0 deletions lib/irb/command/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
module IRB
module Command
class ShowSource < Base
include RubyArgsExtractor

category "Context"
description "Show the source code of a given method, class/module, or constant."

Expand Down
2 changes: 2 additions & 0 deletions lib/irb/command/subirb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module IRB

module Command
class MultiIRBCommand < Base
include RubyArgsExtractor

private

def print_deprecated_warning
Expand Down
1 change: 1 addition & 0 deletions lib/irb/default_commands.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "command"
require_relative "command/internal_helpers"
require_relative "command/context"
require_relative "command/exit"
require_relative "command/force_exit"
Expand Down

0 comments on commit 808cadb

Please sign in to comment.