Fix offences of the Style/FrozenStringLiteralComment cop

Fixes #1824 (Enabling `# frozen_string_literal: true` in `~/.pryc` crashes most
operations)
This commit is contained in:
Kyrylo Silin 2019-05-08 00:13:29 +03:00
parent 18c45d26c5
commit cd1a9bbe5e
23 changed files with 76 additions and 72 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -60,7 +60,7 @@ class Pry
# @param [Array<Pry::Command>] 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")

View File

@ -35,7 +35,7 @@ class Pry
end
def format_value(value)
Pry::ColorPrinter.pp(value, '')
Pry::ColorPrinter.pp(value, ''.dup)
end
def correct_opts?

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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}"

View File

@ -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

View File

@ -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

View File

@ -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 \

View File

@ -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)

View File

@ -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)

View File

@ -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