From c0356a0b935d4ce6236b08803cc5c7328ac0ec43 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Thu, 18 Apr 2024 03:36:25 +0900 Subject: [PATCH] [ruby/irb] Remove internal-only methods from Command::Base (https://github.com/ruby/irb/pull/922) * 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. * Update lib/irb/command/edit.rb --------- https://github.com/ruby/irb/commit/7405a841e8 Co-authored-by: Stan Lo --- 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 ff74b5fb35438b..b078b482375711 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 3c4a54e5e2fa64..cb7e0c4873b3a3 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 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 00000000000000..249b5cdedeb080 --- /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 33e327f4a93838..1cd3f279d1003e 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 f6b19648640489..cbd9998bc4e601 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 70dc69cdec5ef5..f96be20de86bbd 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 f9393cd3b5cab9..8a2188e4ebc365 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 c4c8fc004121c2..f4c6f104a2d577 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 24428a5c130ed7..138d61c9307228 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 6025b0547da0be..d680655feed9f0 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"