From cd1a9bbe5eaac5d4a08c76e34605309d5d9e7bcb Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Wed, 8 May 2019 00:13:29 +0300 Subject: Fix offences of the Style/FrozenStringLiteralComment cop Fixes #1824 (Enabling `# frozen_string_literal: true` in `~/.pryc` crashes most operations) --- lib/pry/code.rb | 4 +-- lib/pry/command.rb | 2 +- lib/pry/commands/cat/input_expression_formatter.rb | 6 ++-- lib/pry/commands/code_collector.rb | 2 +- lib/pry/commands/edit.rb | 4 +-- lib/pry/commands/find_method.rb | 2 +- lib/pry/commands/fix_indent.rb | 2 +- lib/pry/commands/help.rb | 2 +- lib/pry/commands/ls/formatter.rb | 2 +- lib/pry/commands/show_info.rb | 40 +++++++++++----------- lib/pry/commands/watch_expression/expression.rb | 2 +- lib/pry/commands/whereami.rb | 2 +- lib/pry/input_completer.rb | 6 ++-- lib/pry/object_path.rb | 4 +-- lib/pry/pager.rb | 2 +- lib/pry/pry_instance.rb | 8 ++--- lib/pry/slop/option.rb | 6 ++-- spec/command_integration_spec.rb | 1 - spec/commands/play_spec.rb | 23 +++++++------ spec/helpers/table_spec.rb | 3 +- spec/hooks_spec.rb | 10 +++--- spec/pryrc_spec.rb | 2 +- spec/wrapped_module_spec.rb | 13 ++++--- 23 files changed, 76 insertions(+), 72 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index dcd33d4a..ad38b4f2 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -256,12 +256,12 @@ class Pry # @return [String] a formatted representation (based on the configuration of # the object). def to_s - print_to_output("", false) + print_to_output(''.dup, false) end # @return [String] a (possibly highlighted) copy of the source code. def highlighted - print_to_output("", true) + print_to_output(''.dup, true) end # Writes a formatted representation (based on the configuration of the diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 6b274133..99bde110 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -354,7 +354,7 @@ class Pry # @param [String] val The line of input # @return [Array] def tokenize(val) - val.replace(interpolate_string(val)) if command_options[:interpolate] + val = interpolate_string(val) if command_options[:interpolate] self.class.command_regex =~ val diff --git a/lib/pry/commands/cat/input_expression_formatter.rb b/lib/pry/commands/cat/input_expression_formatter.rb index 4d8468fe..2f491406 100644 --- a/lib/pry/commands/cat/input_expression_formatter.rb +++ b/lib/pry/commands/cat/input_expression_formatter.rb @@ -16,10 +16,10 @@ class Pry raise CommandError, "No input expressions!" if numbered_input_items.empty? if numbered_input_items.length > 1 - content = "" + content = '' numbered_input_items.each do |i, s| - content << "#{Helpers::Text.bold(i.to_s)}:\n" - content << decorate(Pry::Code(s).with_indentation(2)).to_s + content += "#{Helpers::Text.bold(i.to_s)}:\n" + content += decorate(Pry::Code(s).with_indentation(2)).to_s end content diff --git a/lib/pry/commands/code_collector.rb b/lib/pry/commands/code_collector.rb index a827abf6..5ca385f5 100644 --- a/lib/pry/commands/code_collector.rb +++ b/lib/pry/commands/code_collector.rb @@ -144,7 +144,7 @@ class Pry end ranged_array = Array(array[range]) || [] - ranged_array.compact.each { |v| all << yield(v) } + ranged_array.compact.each { |v| all += yield(v) } end all diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index 1819f8a4..4c31e089 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -71,9 +71,7 @@ class Pry initial_temp_file_content, initial_temp_file_content.lines.count ) - silence_warnings do - eval_string.replace content - end + pry_instance.eval_string = content Pry.history.push(content) end diff --git a/lib/pry/commands/find_method.rb b/lib/pry/commands/find_method.rb index 31a6a319..6afced3c 100644 --- a/lib/pry/commands/find_method.rb +++ b/lib/pry/commands/find_method.rb @@ -99,7 +99,7 @@ class Pry # if `-c` was not given def additional_info(header, method) if opts.content? - ": " << colorize_code(matched_method_lines(header, method)) + ': ' + colorize_code(matched_method_lines(header, method)) else "" end diff --git a/lib/pry/commands/fix_indent.rb b/lib/pry/commands/fix_indent.rb index 6c338691..4974acb4 100644 --- a/lib/pry/commands/fix_indent.rb +++ b/lib/pry/commands/fix_indent.rb @@ -14,7 +14,7 @@ class Pry def process indented_str = Pry::Indent.indent(eval_string) - eval_string.replace indented_str + pry_instance.eval_string = indented_str end end diff --git a/lib/pry/commands/help.rb b/lib/pry/commands/help.rb index 03f82895..1dee2c64 100644 --- a/lib/pry/commands/help.rb +++ b/lib/pry/commands/help.rb @@ -60,7 +60,7 @@ class Pry # @param [Array] commands # @return [String] The generated help string. def help_text_for_commands(name, commands) - "#{bold(name.capitalize)}\n" << commands.map do |command| + "#{bold(name.capitalize)}\n" + commands.map do |command| " #{command.options[:listing].to_s.ljust(18)} " \ "#{command.description.capitalize}" end.join("\n") diff --git a/lib/pry/commands/ls/formatter.rb b/lib/pry/commands/ls/formatter.rb index c5163ca5..ddd17842 100644 --- a/lib/pry/commands/ls/formatter.rb +++ b/lib/pry/commands/ls/formatter.rb @@ -35,7 +35,7 @@ class Pry end def format_value(value) - Pry::ColorPrinter.pp(value, '') + Pry::ColorPrinter.pp(value, ''.dup) end def correct_opts? diff --git a/lib/pry/commands/show_info.rb b/lib/pry/commands/show_info.rb index 4e982af8..9037cc1f 100644 --- a/lib/pry/commands/show_info.rb +++ b/lib/pry/commands/show_info.rb @@ -75,7 +75,7 @@ class Pry end def content_and_header_for_code_object(code_object) - header(code_object) << content_for(code_object) + header(code_object) + content_for(code_object) end def content_and_headers_for_all_module_candidates(mod) @@ -84,13 +84,13 @@ class Pry mod.number_of_candidates.times do |v| candidate = mod.candidate(v) begin - result << "\nCandidate #{v + 1}/#{mod.number_of_candidates}: " \ + result += "\nCandidate #{v + 1}/#{mod.number_of_candidates}: " \ "#{candidate.source_file}:#{candidate.source_line}\n" content = content_for(candidate) - result << "Number of lines: #{content.lines.count}\n\n" << content + result += "Number of lines: #{content.lines.count}\n\n" + content rescue Pry::RescuableException - result << "\nNo content found.\n" + result += "\nNo content found.\n" next end end @@ -108,17 +108,17 @@ class Pry content = content_for(code_object) h = "\n#{bold('From:')} #{file_name}" - h << code_object_header(code_object, line_num) - h << "\n#{bold('Number of lines:')} " << "#{content.lines.count}\n\n" + h += code_object_header(code_object, line_num) + h += "\n#{bold('Number of lines:')} " + "#{content.lines.count}\n\n" if @used_super - h << bold('** Warning:') - h << " Cannot find code for #{@original_code_object.nonblank_name}. " \ + h += bold('** Warning:') + h += " Cannot find code for #{@original_code_object.nonblank_name}. " \ "Showing superclass #{code_object.nonblank_name} instead. **\n\n" end if content.lines.none? - h << bold('** Warning:') - h << " Cannot find code for '#{code_object.name}' (source_location is nil)" + h += bold('** Warning:') + h += " Cannot find code for '#{code_object.name}' (source_location is nil)" end h @@ -141,23 +141,23 @@ class Pry def method_header(code_object, line_num) h = "" - h << (code_object.c_method? ? ' (C Method):' : ":#{line_num}:") - h << method_sections(code_object)[:owner] - h << method_sections(code_object)[:visibility] - h << method_sections(code_object)[:signature] + h += (code_object.c_method? ? ' (C Method):' : ":#{line_num}:") + h += method_sections(code_object)[:owner] + h += method_sections(code_object)[:visibility] + h += method_sections(code_object)[:signature] h end def module_header(code_object, line_num) h = "" - h << ":#{line_num}\n" - h << bold(code_object.module? ? "Module" : "Class") - h << " #{bold('name:')} #{code_object.nonblank_name}" + h += ":#{line_num}\n" + h += bold(code_object.module? ? "Module" : "Class") + h += " #{bold('name:')} #{code_object.nonblank_name}" if code_object.number_of_candidates > 1 - h << bold("\nNumber of monkeypatches: ") - h << code_object.number_of_candidates.to_s - h << ". Use the `-a` option to display all available monkeypatches" + h += bold("\nNumber of monkeypatches: ") + h += code_object.number_of_candidates.to_s + h += ". Use the `-a` option to display all available monkeypatches" end h end diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb index 3b0d605a..2214b875 100644 --- a/lib/pry/commands/watch_expression/expression.rb +++ b/lib/pry/commands/watch_expression/expression.rb @@ -14,7 +14,7 @@ class Pry def eval! @previous_value = value - @value = Pry::ColorPrinter.pp(target_eval(target, source), "") + @value = Pry::ColorPrinter.pp(target_eval(target, source), ''.dup) end def to_s diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index 5adc187a..f18026e4 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -107,7 +107,7 @@ class Pry .with_marker(marker) .highlighted pry_instance.pager.page( - "\n#{bold('From:')} #{location}:\n\n" << pretty_code << "\n" + "\n#{bold('From:')} #{location}:\n\n" + pretty_code + "\n" ) end diff --git a/lib/pry/input_completer.rb b/lib/pry/input_completer.rb index 16d55fb1..9566b32d 100644 --- a/lib/pry/input_completer.rb +++ b/lib/pry/input_completer.rb @@ -94,7 +94,7 @@ class Pry when SYMBOL_REGEXP # Symbol if Symbol.respond_to?(:all_symbols) sym = Regexp.quote(Regexp.last_match(1)) - candidates = Symbol.all_symbols.collect { |s| ":" << s.id2name } + candidates = Symbol.all_symbols.collect { |s| ":" + s.id2name } candidates.grep(/^#{sym}/) else [] @@ -102,7 +102,7 @@ class Pry when TOPLEVEL_LOOKUP_REGEXP # Absolute Constant or class methods receiver = Regexp.last_match(1) candidates = Object.constants.collect(&:to_s) - candidates.grep(/^#{receiver}/).collect { |e| "::" << e } + candidates.grep(/^#{receiver}/).collect { |e| "::" + e } when CONSTANT_REGEXP # Constant message = Regexp.last_match(1) begin @@ -230,7 +230,7 @@ class Pry candidates.grep(/^#{message}/).collect do |e| next unless e =~ /^[a-zA-Z_]/ - path.call(receiver + "." << e) + path.call(receiver + "." + e) end.compact end diff --git a/lib/pry/object_path.rb b/lib/pry/object_path.rb index b200c2bb..28895a34 100644 --- a/lib/pry/object_path.rb +++ b/lib/pry/object_path.rb @@ -37,13 +37,13 @@ class Pry loop do # Scan for as long as we don't see a slash - next_segment << scanner.scan(%r{[^/]*}) + next_segment += scanner.scan(%r{[^/]*}) if complete?(next_segment) || scanner.eos? scanner.getch # consume the slash break else - next_segment << scanner.getch # append the slash + next_segment += scanner.getch # append the slash end end diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb index 966e06c1..693e9e8e 100644 --- a/lib/pry/pager.rb +++ b/lib/pry/pager.rb @@ -170,7 +170,7 @@ class Pry write_to_pager str else @tracker.record str - @buffer << str + @buffer += str write_to_pager @buffer if @tracker.page? end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index b46e703d..ef963a07 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -81,7 +81,7 @@ class Pry def initialize(options = {}) @binding_stack = [] @indent = Pry::Indent.new - @eval_string = "" + @eval_string = ''.dup @backtrace = options.delete(:backtrace) || caller target = options.delete(:target) @config = self.class.config.merge(options) @@ -229,7 +229,7 @@ class Pry # Reset the current eval string. If the user has entered part of a multiline # expression, this discards that input. def reset_eval_string - @eval_string = "" + @eval_string = ''.dup end # Pass a line of input to Pry. @@ -345,7 +345,7 @@ class Pry # the command that was invoked was non-void (had a return value) and so we make # the value of the current expression equal to the return value # of the command. - @eval_string.replace "::Pry.current[:pry_cmd_result].retval\n" + @eval_string = "::Pry.current[:pry_cmd_result].retval\n" end true else @@ -608,7 +608,7 @@ class Pry inject_sticky_locals! begin unless process_command_safely(line) - @eval_string << "#{line.chomp}\n" if !line.empty? || !@eval_string.empty? + @eval_string += "#{line.chomp}\n" if !line.empty? || !@eval_string.empty? end rescue RescuableException => e self.last_exception = e diff --git a/lib/pry/slop/option.rb b/lib/pry/slop/option.rb index 8a0df7e1..d39998b8 100644 --- a/lib/pry/slop/option.rb +++ b/lib/pry/slop/option.rb @@ -127,12 +127,12 @@ class Pry out = " #{short ? "-#{short}, " : ' ' * 4}" if long - out << "--#{long}" + out += "--#{long}" size = long.size diff = @slop.config[:longest_flag] - size - out << (' ' * (diff + 6)) + out += (' ' * (diff + 6)) else - out << (' ' * (@slop.config[:longest_flag] + 8)) + out += (' ' * (@slop.config[:longest_flag] + 8)) end "#{out}#{description}" diff --git a/spec/command_integration_spec.rb b/spec/command_integration_spec.rb index c1b63052..7579da62 100644 --- a/spec/command_integration_spec.rb +++ b/spec/command_integration_spec.rb @@ -452,7 +452,6 @@ describe "commands" do ) do klass = Pry::CommandSet.new do command "hello", "", keep_retval: true do - eval_string.replace("6") 7 end end diff --git a/spec/commands/play_spec.rb b/spec/commands/play_spec.rb index 000f6fe5..04573eb9 100644 --- a/spec/commands/play_spec.rb +++ b/spec/commands/play_spec.rb @@ -37,6 +37,7 @@ describe "play" do it 'should play a file' do @t.process_command 'play spec/fixtures/whereami_helper.rb' expect(@t.eval_string).to eq unindent(<<-STR) + # frozen_string_literal: true # rubocop:disable Layout/EmptyLineBetweenDefs class Cor def a; end @@ -50,16 +51,18 @@ describe "play" do it 'should output file contents with print option' do @t.process_command 'play --print spec/fixtures/whereami_helper.rb' - expect(@t.last_output).to eq unindent(<<-STR) - 1: # rubocop:disable Layout/EmptyLineBetweenDefs - 2: class Cor - 3: def a; end - 4: def b; end - 5: def c; end - 6: def d; end - 7: end - 8: # rubocop:enable Layout/EmptyLineBetweenDefs - STR + expect(@t.last_output).to eq( + " 1: \# frozen_string_literal: true\n" \ + " 2: \n" \ + " 3: \# rubocop:disable Layout/EmptyLineBetweenDefs\n" \ + " 4: class Cor\n" \ + " 5: def a; end\n" \ + " 6: def b; end\n" \ + " 7: def c; end\n" \ + " 8: def d; end\n" \ + " 9: end\n" \ + "10: \# rubocop:enable Layout/EmptyLineBetweenDefs\n" + ) end end diff --git a/spec/helpers/table_spec.rb b/spec/helpers/table_spec.rb index 66b3acfa..aad9ed6b 100644 --- a/spec/helpers/table_spec.rb +++ b/spec/helpers/table_spec.rb @@ -22,7 +22,8 @@ describe 'Formatting Table' do def try_round_trip(expected) things = expected.split(/\s+/).sort actual = Pry::Helpers.tablify(things, FAKE_COLUMNS).to_s.strip - [expected, actual].each { |e| e.gsub!(/\s+$/, '') } + expected = expected.gsub(/\s+$/, '') + actual = actual.gsub(/\s+$/, '') if actual != expected bar = '-' * 25 puts \ diff --git a/spec/hooks_spec.rb b/spec/hooks_spec.rb index 0b8d6f9c..16af79a4 100644 --- a/spec/hooks_spec.rb +++ b/spec/hooks_spec.rb @@ -123,14 +123,14 @@ describe Pry::Hooks do end it 'should preserve hook order' do - name = "" + name = '' h1 = Pry::Hooks.new - h1.add_hook(:test_hook, :testing3) { name << "h" } - h1.add_hook(:test_hook, :testing4) { name << "n" } + h1.add_hook(:test_hook, :testing3) { name += "h" } + h1.add_hook(:test_hook, :testing4) { name += "n" } h2 = Pry::Hooks.new - h2.add_hook(:test_hook, :testing1) { name << "j" } - h2.add_hook(:test_hook, :testing2) { name << "o" } + h2.add_hook(:test_hook, :testing1) { name += "j" } + h2.add_hook(:test_hook, :testing2) { name += "o" } h2.merge!(h1) h2.exec_hook(:test_hook) diff --git a/spec/pryrc_spec.rb b/spec/pryrc_spec.rb index a7b5fc2a..59f2eed9 100644 --- a/spec/pryrc_spec.rb +++ b/spec/pryrc_spec.rb @@ -39,7 +39,7 @@ describe Pry do it "should not load the pryrc if pryrc's directory permissions do not allow this" do Dir.mktmpdir do |dir| File.chmod 0o000, dir - Pry::LOCAL_RC_FILE.replace File.join(dir, '.pryrc') + stub_const('Pry::LOCAL_RC_FILE', File.join(dir, '.pryrc')) Pry.config.should_load_rc = true expect do Pry.start(self, input: StringIO.new("exit-all\n"), output: StringIO.new) diff --git a/spec/wrapped_module_spec.rb b/spec/wrapped_module_spec.rb index a97affb9..138b9b1a 100644 --- a/spec/wrapped_module_spec.rb +++ b/spec/wrapped_module_spec.rb @@ -185,7 +185,8 @@ describe Pry::WrappedModule do describe ".singleton_class?" do it "should be true for singleton classes" do - expect(Pry::WrappedModule.new(class << ""; self; end).singleton_class?).to eq true + mod = Pry::WrappedModule.new(class << Object.new; self; end) + expect(mod).to be_singleton_class end it "should be false for normal classes" do @@ -204,10 +205,12 @@ describe Pry::WrappedModule do end it "should return the attached object" do - expect(Pry::WrappedModule.new(class << "hi"; self; end).singleton_instance) - .to eq "hi" - expect(Pry::WrappedModule.new(class << Object; self; end).singleton_instance) - .to equal(Object) + instance = Object.new + mod = class << instance; self; end + expect(Pry::WrappedModule.new(mod).singleton_instance).to eq(instance) + + klass = class << Object; self; end + expect(Pry::WrappedModule.new(klass).singleton_instance).to equal(Object) end end -- cgit v1.2.1