-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from utkarsh2102/add-relative_require_to_lib-cop
Implement the new RelativeRequireToLib cop + add tests
- Loading branch information
Showing
10 changed files
with
216 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ language: ruby | |
cache: bundler | ||
before_install: gem install bundler -v 2.1.4 | ||
rvm: | ||
- 2.4.10 | ||
- 2.5.8 | ||
- 2.6.6 | ||
- 2.7.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop # :nodoc: | ||
module Cop # :nodoc: | ||
module Packaging # :nodoc: | ||
# This cop is used to identify the `require_relative` calls, | ||
# mapping to the "lib" directory and suggests to use `require` | ||
# instead. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# require_relative 'lib/foo.rb' | ||
# | ||
# # bad | ||
# require_relative '../../lib/foo/bar' | ||
# | ||
# # good | ||
# require 'foo.rb' | ||
# | ||
# # good | ||
# require 'foo/bar' | ||
# | ||
# # good | ||
# require_relative 'spec_helper' | ||
# require_relative 'foo/bar' | ||
# | ||
class RelativeRequireToLib < Base | ||
# This is the message that will be displayed when RuboCop finds an | ||
# offense of using `require_relative` with relative path to lib. | ||
MSG = 'Avoid using `require_relative` with relative path to lib. ' \ | ||
'Use `require` instead.' | ||
|
||
def_node_matcher :require_relative, <<~PATTERN | ||
(send nil? :require_relative | ||
(str #target_falls_in_lib?)) | ||
PATTERN | ||
|
||
# Extended from the Base class. | ||
# More about the `#on_new_investigation` method can be found here: | ||
# https://github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb | ||
# | ||
# Processing of the AST happens here. | ||
def on_new_investigation | ||
@file_path = processed_source.file_path | ||
@file_directory = File.dirname(@file_path) | ||
end | ||
|
||
# Extended from AST::Traversal. | ||
# More about the `#on_send` method can be found here: | ||
# https://github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112 | ||
def on_send(node) | ||
return unless require_relative(node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
# This method is called from inside `#def_node_matcher`. | ||
# It is used to find paths which starts with "lib". | ||
def target_falls_in_lib?(str) | ||
root_dir = RuboCop::ConfigLoader.project_root | ||
File.expand_path(str, @file_directory).start_with?(root_dir + '/lib') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'packaging/gemspec_git' | ||
require_relative 'packaging/relative_require_to_lib' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
spec/rubocop/cop/packaging/relative_require_to_lib_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Packaging::RelativeRequireToLib, :config do | ||
let(:message) { RuboCop::Cop::Packaging::RelativeRequireToLib::MSG } | ||
|
||
let(:project_root) { RuboCop::ConfigLoader.project_root } | ||
|
||
context 'when `require_relative` call lies outside spec/' do | ||
let(:filename) { "#{project_root}/spec/foo_spec.rb" } | ||
let(:source) { 'require_relative "../lib/foo.rb"' } | ||
|
||
it 'registers an offense' do | ||
expect_offense(<<~RUBY, filename) | ||
#{source} | ||
#{'^' * source.length} #{message} | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `require_relative` call with nested path lies outside test/' do | ||
let(:filename) { "#{project_root}/test/rubocop/cop/bar_spec.rb" } | ||
let(:source) { 'require_relative "../../../lib/bar"' } | ||
|
||
it 'registers an offense' do | ||
expect_offense(<<~RUBY, filename) | ||
#{source} | ||
#{'^' * source.length} #{message} | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when one `require_relative` call lies outside specs/' do | ||
let(:filename) { "#{project_root}/specs/baz_spec.rb" } | ||
let(:source) { <<~RUBY.chomp } | ||
require_relative 'spec_helper' | ||
require_relative '../lib/rubocop/baz' | ||
RUBY | ||
|
||
it 'registers an offense' do | ||
expect_offense(<<~RUBY, filename) | ||
#{source} | ||
#{'^' * 37} #{message} | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `require_relative` call with `unshift` lies outside tests/' do | ||
let(:filename) { "#{project_root}/tests/qux_spec.rb" } | ||
let(:source) { <<~RUBY.chomp } | ||
$:.unshift('../lib') | ||
require_relative "../lib/qux" | ||
RUBY | ||
|
||
it 'registers an offense' do | ||
expect_offense(<<~RUBY, filename) | ||
#{source} | ||
#{'^' * 29} #{message} | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the `require_relative` call to `lib` lies inside spec/' do | ||
let(:filename) { "#{project_root}/spec/rubocop/cop/foo_spec.rb" } | ||
let(:source) { 'require_relative "../lib/foo"' } | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, filename) | ||
#{source} | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the `require_relative` call lies inside tests/' do | ||
let(:filename) { "#{project_root}/tests/rubocop/cop/bar_spec.rb" } | ||
let(:source) { 'require_relative "../bar"' } | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, filename) | ||
#{source} | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the `require_relative` call lies inside test/' do | ||
let(:filename) { "#{project_root}/test/qux_spec.rb" } | ||
let(:source) { 'require_relative "spec/rubocop/qux.rb"' } | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, filename) | ||
#{source} | ||
RUBY | ||
end | ||
end | ||
end |