rubocop-packaging 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a302f7a6e19e06fa73adecfaa4be36d8db647d2aef3571554d4246b222306c6
4
- data.tar.gz: af9c1d6e9435f94adbdaaf8a256291e3546fe3dfc024f36abf462c1e1b707046
3
+ metadata.gz: 72be16aa270e6a0acac89f70963f75e8c4510932a76cf254bf8d4b54e4a25f43
4
+ data.tar.gz: 68e3647ae5c13139e5fdf799bdc6e32b2e51df23c0ca676e60d2b6e29e3f0f0c
5
5
  SHA512:
6
- metadata.gz: 11fc92be8299fb440ed62524b58d9d0684e520ad30879c86c2a79d16a6880248090f595a50ace1bc8569f551ad0b5e56ae5773398a6b2f4c91232b1e31009612
7
- data.tar.gz: b50af49a31c46beb60f283604bb2877b0bd461bcd702adacf39f635d48603484782f52ef923b9fe960f035c0ce9bda3fe7d0fa1be17bf0076864445ab7571183
6
+ metadata.gz: c938f05cabce5bccc176fc40ecb62c029718884f7cfbdcd35c7cdfe712164ef86827d3e1eb93ae64f03ebf3ee7ea086acfa1967082d65fdefd9fed851987a42e
7
+ data.tar.gz: d7830f8e572577ba14554c2e8589ee99ae9898b91451143ab6b441c30d80930608ad97421582e38fdea212f047a826821ee1f83adbfd6f4261182af7b9bae9de
data/README.md CHANGED
@@ -5,9 +5,15 @@ which is a Ruby static code analyzer (a.k.a. linter) and code formatter.
5
5
 
6
6
  It helps enforcing some of the guidelines that are expected of upstream
7
7
  maintainers so that the downstream can build their packages in a clean
8
- environment without any problems.
9
- Some of the other basic guidelines can be found
10
- [here](https://wiki.debian.org/Teams/Ruby/RubyExtras/UpstreamDevelopers).
8
+ environment without any problems.
9
+
10
+ ## Documentation
11
+
12
+ A detailed documentation, explaining what this extension is doing and the
13
+ reasoning behind it, can be found here: [packaging-style-guide](https://github.com/utkarsh2102/packaging-style-guide).
14
+
15
+ In case anything is not clear, please feel free to raise an issue, asking
16
+ for more explanation!
11
17
 
12
18
  ## Installation
13
19
 
@@ -6,7 +6,8 @@ Packaging/GemspecGit:
6
6
  VersionAdded: '0.1'
7
7
  VersionChanged: '0.1'
8
8
 
9
- Packaging/RelativeRequireToLib:
9
+ Packaging/RequireRelativeHardcodingLib:
10
10
  Description: 'Avoid using `require_relative` with relative path to lib.'
11
11
  Enabled: true
12
12
  VersionAdded: '0.2'
13
+ VersionChanged: '0.3'
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubocop'
3
+ require "rubocop"
4
4
 
5
- require_relative 'rubocop/packaging'
6
- require_relative 'rubocop/packaging/version'
7
- require_relative 'rubocop/packaging/inject'
5
+ require_relative "rubocop/packaging"
6
+ require_relative "rubocop/packaging/version"
7
+ require_relative "rubocop/packaging/inject"
8
8
 
9
9
  RuboCop::Packaging::Inject.defaults!
10
10
 
11
- require_relative 'rubocop/cop/packaging_cops'
11
+ require_relative "rubocop/cop/packaging_cops"
@@ -3,56 +3,60 @@
3
3
  module RuboCop # :nodoc:
4
4
  module Cop # :nodoc:
5
5
  module Packaging # :nodoc:
6
- # This cop is used to identify the usage of `git ls-files`
6
+ # This cop flags the usage of `git ls-files` in gemspec
7
7
  # and suggests to use a plain Ruby alternative, like `Dir`,
8
- # `Dir.glob` or `Rake::FileList` instead.
8
+ # `Dir.glob`, or `Rake::FileList` instead.
9
+ #
10
+ # More information about the GemspecGit cop can be found here:
11
+ # https://github.com/utkarsh2102/packaging-style-guide#gemspec-git
9
12
  #
10
13
  # @example
11
14
  #
12
15
  # # bad
13
16
  # Gem::Specification.new do |spec|
14
- # spec.files = `git ls-files`.split('\n')
17
+ # spec.files = `git ls-files`.split("\n")
18
+ # spec.test_files = `git ls-files -- spec`.split("\n")
15
19
  # end
16
20
  #
17
- # # bad
21
+ # # good
18
22
  # Gem::Specification.new do |spec|
19
- # spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
- # `git ls-files -z`.split('\\x0').reject { |f| f.match(%r{^(test|spec|features)/}) }
21
- # end
23
+ # spec.files = Dir["lib/**/*", "LICENSE", "README.md"]
24
+ # spec.test_files = Dir["spec/**/*"]
22
25
  # end
23
26
  #
24
27
  # # bad
25
28
  # Gem::Specification.new do |spec|
26
- # spec.files = `git ls-files`.split('\n')
27
- # spec.test_files = `git ls-files -- test/{functional,unit}/*`.split('\n')
28
- # spec.executables = `git ls-files -- bin/*`.split('\n').map{ |f| File.basename(f) }
29
+ # spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
+ # `git ls-files -z`.split("\\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ # end
29
32
  # end
30
33
  #
31
34
  # # good
32
35
  # Gem::Specification.new do |spec|
33
- # spec.files = Dir['lib/**/*', 'LICENSE', 'README.md']
34
- # spec.test_files = Dir['spec/**/*']
36
+ # spec.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
35
37
  # end
36
38
  #
37
- # # good
39
+ # # bad
38
40
  # Gem::Specification.new do |spec|
39
- # spec.files = Rake::FileList['**/*'].exclude(*File.read('.gitignore').split)
41
+ # spec.files = `git ls-files -- lib/`.split("\n")
42
+ # spec.test_files = `git ls-files -- test/{functional,unit}/*`.split("\n")
43
+ # spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
40
44
  # end
41
45
  #
42
46
  # # good
43
47
  # Gem::Specification.new do |spec|
44
- # spec.files = Dir.glob('lib/**/*')
45
- # spec.test_files = Dir.glob('test/{functional,test}/*')
46
- # spec.executables = Dir.glob('bin/*').map{ |f| File.basename(f) }
48
+ # spec.files = Dir.glob("lib/**/*")
49
+ # spec.test_files = Dir.glob("test/{functional,test}/*")
50
+ # spec.executables = Dir.glob("bin/*").map{ |f| File.basename(f) }
47
51
  # end
48
52
  #
49
53
  class GemspecGit < Cop
50
54
  # This is the message that will be displayed when RuboCop finds an
51
55
  # offense of using `git ls-files`.
52
- MSG = 'Avoid using git to produce lists of files. ' \
53
- 'Downstreams often need to build your package in an environment ' \
54
- 'that does not have git (on purpose). ' \
55
- 'Use some pure Ruby alternative, like `Dir` or `Dir.glob`.'
56
+ MSG = "Avoid using git to produce lists of files. " \
57
+ "Downstreams often need to build your package in an environment " \
58
+ "that does not have git (on purpose). " \
59
+ "Use some pure Ruby alternative, like `Dir` or `Dir.glob`."
56
60
 
57
61
  def_node_search :xstr, <<~PATTERN
58
62
  (block
@@ -81,9 +85,9 @@ module RuboCop # :nodoc:
81
85
  end
82
86
 
83
87
  # This method is called from inside `#def_node_search`.
84
- # It is used to find strings which start with 'git'.
88
+ # It is used to find strings which start with "git".
85
89
  def starts_with_git?(str)
86
- str.start_with?('git')
90
+ str.start_with?("git")
87
91
  end
88
92
  end
89
93
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop # :nodoc:
4
+ module Cop # :nodoc:
5
+ module Packaging # :nodoc:
6
+ # This cop flags the `require_relative` calls, from anywhere
7
+ # mapping to the "lib" directory, except originating from lib/ or
8
+ # the gemspec file, and suggests to use `require` instead.
9
+ #
10
+ # More information about the RequireRelativeHardcodingLib cop can be found here:
11
+ # https://github.com/utkarsh2102/packaging-style-guide#require-relative-hardcoding-lib
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # require_relative "lib/foo.rb"
17
+ #
18
+ # # good
19
+ # require "foo.rb"
20
+ #
21
+ # # bad
22
+ # require_relative "../../lib/foo/bar"
23
+ #
24
+ # # good
25
+ # require "foo/bar"
26
+ #
27
+ # # good
28
+ # require_relative "spec_helper"
29
+ # require_relative "spec/foo/bar"
30
+ #
31
+ class RequireRelativeHardcodingLib < Base
32
+ # This is the message that will be displayed when RuboCop finds an
33
+ # offense of using `require_relative` with relative path to lib.
34
+ MSG = "Avoid using `require_relative` with relative path to lib. " \
35
+ "Use `require` instead."
36
+
37
+ def_node_matcher :require_relative, <<~PATTERN
38
+ (send nil? :require_relative
39
+ (str #falls_in_lib?))
40
+ PATTERN
41
+
42
+ # Extended from the Base class.
43
+ # More about the `#on_new_investigation` method can be found here:
44
+ # https://github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb
45
+ #
46
+ # Processing of the AST happens here.
47
+ def on_new_investigation
48
+ @file_path = processed_source.file_path
49
+ @file_directory = File.dirname(@file_path)
50
+ end
51
+
52
+ # Extended from AST::Traversal.
53
+ # More about the `#on_send` method can be found here:
54
+ # https://github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112
55
+ def on_send(node)
56
+ return unless require_relative(node)
57
+
58
+ add_offense(node)
59
+ end
60
+
61
+ # For determining the root directory of the project.
62
+ def root_dir
63
+ RuboCop::ConfigLoader.project_root
64
+ end
65
+
66
+ # This method is called from inside `#def_node_matcher`.
67
+ # It flags an offense if the `require_relative` call is made
68
+ # from anywhere except the "lib" directory.
69
+ def falls_in_lib?(str)
70
+ target_falls_in_lib?(str) && !inspected_file_falls_in_lib? && !inspected_file_is_gemspec?
71
+ end
72
+
73
+ # This method determines if the `require_relative` call is made
74
+ # to the "lib" directory.
75
+ def target_falls_in_lib?(str)
76
+ File.expand_path(str, @file_directory).start_with?("#{root_dir}/lib")
77
+ end
78
+
79
+ # This method determines if that call is made *from* the "lib" directory.
80
+ def inspected_file_falls_in_lib?
81
+ @file_path.start_with?("#{root_dir}/lib")
82
+ end
83
+
84
+ # This method determines if that call is made *from* the "gemspec" file.
85
+ def inspected_file_is_gemspec?
86
+ @file_path.end_with?("gemspec")
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'packaging/gemspec_git'
4
- require_relative 'packaging/relative_require_to_lib'
3
+ require_relative "packaging/gemspec_git"
4
+ require_relative "packaging/require_relative_hardcoding_lib"
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubocop/packaging/version'
3
+ require "rubocop/packaging/version"
4
4
 
5
5
  module RuboCop
6
6
  # RuboCop Packaging project namespace
7
7
  module Packaging
8
8
  PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
9
- CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
9
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
10
10
  CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
11
11
 
12
12
  private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Packaging
5
- VERSION = '0.2.0'
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-packaging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Utkarsh Gupta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-24 00:00:00.000000000 Z
11
+ date: 2020-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0.88'
89
+ version: '0.89'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0.88'
96
+ version: '0.89'
97
97
  description: |
98
98
  A collection of RuboCop cops to check for downstream compatability issues in the
99
99
  Ruby code.
@@ -108,7 +108,7 @@ files:
108
108
  - config/default.yml
109
109
  - lib/rubocop-packaging.rb
110
110
  - lib/rubocop/cop/packaging/gemspec_git.rb
111
- - lib/rubocop/cop/packaging/relative_require_to_lib.rb
111
+ - lib/rubocop/cop/packaging/require_relative_hardcoding_lib.rb
112
112
  - lib/rubocop/cop/packaging_cops.rb
113
113
  - lib/rubocop/packaging.rb
114
114
  - lib/rubocop/packaging/inject.rb
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop # :nodoc:
4
- module Cop # :nodoc:
5
- module Packaging # :nodoc:
6
- # This cop is used to identify the `require_relative` calls,
7
- # mapping to the "lib" directory and suggests to use `require`
8
- # instead.
9
- #
10
- # @example
11
- #
12
- # # bad
13
- # require_relative 'lib/foo.rb'
14
- #
15
- # # bad
16
- # require_relative '../../lib/foo/bar'
17
- #
18
- # # good
19
- # require 'foo.rb'
20
- #
21
- # # good
22
- # require 'foo/bar'
23
- #
24
- # # good
25
- # require_relative 'spec_helper'
26
- # require_relative 'foo/bar'
27
- #
28
- class RelativeRequireToLib < Base
29
- # This is the message that will be displayed when RuboCop finds an
30
- # offense of using `require_relative` with relative path to lib.
31
- MSG = 'Avoid using `require_relative` with relative path to lib. ' \
32
- 'Use `require` instead.'
33
-
34
- def_node_matcher :require_relative, <<~PATTERN
35
- (send nil? :require_relative
36
- (str #target_falls_in_lib?))
37
- PATTERN
38
-
39
- # Extended from the Base class.
40
- # More about the `#on_new_investigation` method can be found here:
41
- # https://github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb
42
- #
43
- # Processing of the AST happens here.
44
- def on_new_investigation
45
- @file_path = processed_source.file_path
46
- @file_directory = File.dirname(@file_path)
47
- end
48
-
49
- # Extended from AST::Traversal.
50
- # More about the `#on_send` method can be found here:
51
- # https://github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112
52
- def on_send(node)
53
- return unless require_relative(node)
54
-
55
- add_offense(node)
56
- end
57
-
58
- # This method is called from inside `#def_node_matcher`.
59
- # It is used to find paths which starts with "lib".
60
- def target_falls_in_lib?(str)
61
- root_dir = RuboCop::ConfigLoader.project_root
62
- File.expand_path(str, @file_directory).start_with?(root_dir + '/lib')
63
- end
64
- end
65
- end
66
- end
67
- end