Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RubyVersionFile cop (read version from .ruby-version) #6

Merged
merged 2 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ EightyFourCodes/CommandLiteralInjection:
Description: "Check for Command Injection in `` and %x"
Enabled: true
VersionAdded: "0.0.1"

EightyFourCodes/RubyVersionFile:
Description: "Ensure .ruby-version file use in Gemfile"
Enabled: true
VersionAdded: "0.0.1"
47 changes: 47 additions & 0 deletions lib/rubocop/cop/eighty_four_codes/ruby_version_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module RuboCop
module Cop
module EightyFourCodes
# Read Ruby version from a .ruby-version file
#
# Instead of staticly defining the Ruby runtime version in Gemfile, load it from
# a .ruby-version file definition. As this Ruby version file is read by rbenv, chruby etc
# it's much easier for the developer to work with multiple projects with different versions.
#
# @example
# # bad
# ruby 2.6.6
#
# # good
# ruby File.read('.ruby-version')
class RubyVersionFile < Base
extend AutoCorrector

MSG = "Control Ruby version via .ruby-version, fix by replacing with File.read('.ruby-version')"

RESTRICT_ON_SEND = %i[ruby].freeze

def_node_matcher :static_version_found?, <<~PATTERN
(send nil? :ruby
$(str _))
PATTERN

def on_send(node)
static_version_found?(node) do |source_node, source|
message = format(MSG, source: source)

add_offense(
source_node,
message: message
) do |corrector|
corrector.replace(
source_node, "File.read('.ruby-version')"
)
end
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions spec/rubocop/cop/eighty_four_codes/ruby_version_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::EightyFourCodes::RubyVersionFile do
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

it 'registers an offense defined with double quotes' do
expect_offense(<<~'RUBY')
ruby "2.6.6"
^^^^^^^ Control Ruby version via .ruby-version, fix by replacing with File.read('.ruby-version')
RUBY

expect_correction(<<~RUBY)
ruby File.read('.ruby-version')
RUBY
end

it 'registers an offense defined with single quotes' do
expect_offense(<<~'RUBY')
ruby '2.1.0'
^^^^^^^ Control Ruby version via .ruby-version, fix by replacing with File.read('.ruby-version')
RUBY

expect_correction(<<~RUBY)
ruby File.read('.ruby-version')
RUBY
end

it 'does not register an offense when reading .ruby-version' do
expect_no_offenses(<<~RUBY)
ruby File.read('.ruby-version')
RUBY
end
end