Commit 39a629df authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '209922-add-readlines-each-cop' into 'master'

Add Performance/ReadlinesEach Cop

See merge request gitlab-org/gitlab!27901
parents a3788157 86f2bcba
......@@ -380,3 +380,6 @@ Style/FloatDivision:
Cop/BanCatchThrow:
Enabled: true
Performance/ReadlinesEach:
Enabled: true
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
class ReadlinesEach < RuboCop::Cop::Cop
MESSAGE = 'Avoid `IO.readlines.each`, since it reads contents into memory in full. ' \
'Use `IO.each_line` or `IO.each` instead.'
def_node_matcher :full_file_read_via_class?, <<~PATTERN
(send
(send (const nil? {:IO :File}) :readlines _) :each)
PATTERN
def_node_matcher :full_file_read_via_instance?, <<~PATTERN
(... (... :readlines) :each)
PATTERN
def on_send(node)
full_file_read_via_class?(node) { add_offense(node, location: :selector, message: MESSAGE) }
full_file_read_via_instance?(node) { add_offense(node, location: :selector, message: MESSAGE) }
end
def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.expression, node.source.gsub('readlines.each', 'each_line'))
end
end
end
end
end
end
......@@ -64,7 +64,7 @@ describe IrkerService do
irker.execute(sample_data)
conn = @irker_server.accept
conn.readlines.each do |line|
conn.each_line do |line|
msg = JSON.parse(line.chomp("\n"))
expect(msg.keys).to match_array(%w(to privmsg))
expect(msg['to']).to match_array(["irc://chat.freenode.net/#commits",
......
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../support/helpers/expect_offense'
require_relative '../../../../rubocop/cop/performance/readlines_each'
describe RuboCop::Cop::Performance::ReadlinesEach do
include CopHelper
include ExpectOffense
subject(:cop) { described_class.new }
let(:message) { 'Avoid `IO.readlines.each`, since it reads contents into memory in full. Use `IO.each_line` or `IO.each` instead.' }
shared_examples_for(:class_read) do |klass|
context "and it is called as a class method on #{klass}" do
# We can't use `expect_offense` here because indentation changes based on `klass`
it 'flags it as an offense' do
inspect_source "#{klass}.readlines(file_path).each { |line| puts line }"
expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ReadlinesEach')
end
end
context 'when just using readlines without each' do
it 'does not flag it as an offense' do
expect_no_offenses "contents = #{klass}.readlines(file_path)"
end
end
end
context 'when reading all lines using IO.readlines.each' do
%w(IO File).each do |klass|
it_behaves_like(:class_read, klass)
end
context 'and it is called as an instance method on a return value' do
it 'flags it as an offense' do
expect_offense <<~SOURCE
get_file.readlines.each { |line| puts line }
^^^^ #{message}
SOURCE
end
end
context 'and it is called as an instance method on an assigned variable' do
it 'flags it as an offense' do
expect_offense <<~SOURCE
file = File.new(path)
file.readlines.each { |line| puts line }
^^^^ #{message}
SOURCE
end
end
context 'and it is called as an instance method on a new object' do
it 'flags it as an offense' do
expect_offense <<~SOURCE
File.new(path).readlines.each { |line| puts line }
^^^^ #{message}
SOURCE
end
end
it 'autocorrects `readlines.each` to `each_line`' do
expect(autocorrect_source('obj.readlines.each { |line| line }')).to(
eq('obj.each_line { |line| line }')
)
end
end
context 'when just using readlines without each' do
it 'does not flag it as an offense' do
expect_no_offenses 'contents = my_file.readlines'
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment