From 808cadb447bd337df882c3ae9ff19228b4042a25 Mon Sep 17 00:00:00 2001 From: tompng Date: Sun, 14 Apr 2024 20:39:04 +0900 Subject: [PATCH] Remove internal-only methods from Command::Base 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. --- lib/irb/command/base.rb | 19 ------------------- lib/irb/command/edit.rb | 2 ++ lib/irb/command/internal_helpers.rb | 27 +++++++++++++++++++++++++++ lib/irb/command/load.rb | 1 + lib/irb/command/ls.rb | 2 ++ lib/irb/command/measure.rb | 2 ++ lib/irb/command/show_doc.rb | 2 ++ lib/irb/command/show_source.rb | 2 ++ lib/irb/command/subirb.rb | 2 ++ lib/irb/default_commands.rb | 1 + 10 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 lib/irb/command/internal_helpers.rb diff --git a/lib/irb/command/base.rb b/lib/irb/command/base.rb index ff74b5fb3..b078b4823 100644 --- a/lib/irb/command/base.rb +++ b/lib/irb/command/base.rb @@ -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 diff --git a/lib/irb/command/edit.rb b/lib/irb/command/edit.rb index 3c4a54e5e..e33da2a60 100644 --- a/lib/irb/command/edit.rb +++ b/lib/irb/command/edit.rb @@ -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 diff --git a/lib/irb/command/internal_helpers.rb b/lib/irb/command/internal_helpers.rb new file mode 100644 index 000000000..249b5cded --- /dev/null +++ b/lib/irb/command/internal_helpers.rb @@ -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 diff --git a/lib/irb/command/load.rb b/lib/irb/command/load.rb index 33e327f4a..1cd3f279d 100644 --- a/lib/irb/command/load.rb +++ b/lib/irb/command/load.rb @@ -10,6 +10,7 @@ module IRB module Command class LoaderCommand < Base + include RubyArgsExtractor include IrbLoader def raise_cmd_argument_error diff --git a/lib/irb/command/ls.rb b/lib/irb/command/ls.rb index f6b196486..cbd9998bc 100644 --- a/lib/irb/command/ls.rb +++ b/lib/irb/command/ls.rb @@ -11,6 +11,8 @@ module IRB module Command class Ls < Base + include RubyArgsExtractor + category "Context" description "Show methods, constants, and variables." diff --git a/lib/irb/command/measure.rb b/lib/irb/command/measure.rb index 70dc69cde..f96be20de 100644 --- a/lib/irb/command/measure.rb +++ b/lib/irb/command/measure.rb @@ -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." diff --git a/lib/irb/command/show_doc.rb b/lib/irb/command/show_doc.rb index f9393cd3b..8a2188e4e 100644 --- a/lib/irb/command/show_doc.rb +++ b/lib/irb/command/show_doc.rb @@ -3,6 +3,8 @@ module IRB module Command class ShowDoc < Base + include RubyArgsExtractor + category "Context" description "Look up documentation with RI." diff --git a/lib/irb/command/show_source.rb b/lib/irb/command/show_source.rb index c4c8fc004..f4c6f104a 100644 --- a/lib/irb/command/show_source.rb +++ b/lib/irb/command/show_source.rb @@ -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." diff --git a/lib/irb/command/subirb.rb b/lib/irb/command/subirb.rb index 24428a5c1..138d61c93 100644 --- a/lib/irb/command/subirb.rb +++ b/lib/irb/command/subirb.rb @@ -9,6 +9,8 @@ module IRB module Command class MultiIRBCommand < Base + include RubyArgsExtractor + private def print_deprecated_warning diff --git a/lib/irb/default_commands.rb b/lib/irb/default_commands.rb index 6025b0547..d680655fe 100644 --- a/lib/irb/default_commands.rb +++ b/lib/irb/default_commands.rb @@ -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"