rubocop-packaging 0.4.0 → 0.5.2

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: ccfa9bb6e8043ae716d5349730d77dd122ad9fec2b6b6c278f79db7a604ba7aa
4
- data.tar.gz: f9a8b7c4fe1c3793827d27681af59a94495cb8f59e39e727c058ce6ce3a2f448
3
+ metadata.gz: 45a3b0981916c4440d8092538357892c433f57955f845b45ab3890086e0e0bd2
4
+ data.tar.gz: b7caa60273e00c8d3468e5f7d7235a1c5a99088d935fc0a2803d8611c4df1f87
5
5
  SHA512:
6
- metadata.gz: cfd44f46a045f09ac6647f22e68e5351fe9d5640c061fe4fe37ec789f3b6bf0f4fbbee527edfcfafe25135cb2f86e37b45d446a1181b455de9064ca97270431a
7
- data.tar.gz: 68fb8cc0cdcb1e2bc8682c5e3aef39bcaa02ebd131ea70927cd18f5f2b63dabbafe1c5da49c54e878ee48ffbec90a83eece281251bd06d28afe15389f00896cd
6
+ metadata.gz: 676a58d6efb8be59c432cb299dc229b497753b4630802e4f0b274f0359d3e2349b08311519f7e252965d877f3046cb7d30ee1bfba6f6a19d546d38b411bae349
7
+ data.tar.gz: 3fd0d9db07d161dbf6740d8e3947909f2fe486235b933ae5af06befce00c461ca7c61149a65559d7568ed1a1cfb674f0d1c45b01d8a7e8644daadbd8d5a5d5c5
data/README.md CHANGED
@@ -10,7 +10,10 @@ environment without any problems.
10
10
  ## Documentation
11
11
 
12
12
  A detailed documentation, explaining what this extension is doing and the
13
- reasoning behind it, can be found here: [packaging-style-guide](https://packaging.rubystyle.guide/).
13
+ reasoning behind it, can be found here: https://docs.rubocop.org/rubocop-packaging/
14
+
15
+ We also have a [packaging-style-guide](https://packaging.rubystyle.guide/),
16
+ listing some good and bad examples and the rationale behind these cops.
14
17
 
15
18
  In case anything is not clear, please feel free to raise an issue, asking
16
19
  for more explanation!
data/config/default.yml CHANGED
@@ -1,23 +1,35 @@
1
1
  # This is the default configuration file.
2
2
 
3
3
  Packaging/BundlerSetupInTests:
4
- Description: 'Avoid using `bundler/setup` in your tests.'
4
+ Description: >-
5
+ Using `bundler/setup` in tests is redundant. Consider
6
+ removing it.
5
7
  Enabled: true
6
8
  VersionAdded: '0.4'
9
+ VersionChanged: '0.5'
7
10
 
8
11
  Packaging/GemspecGit:
9
- Description: 'Use pure Ruby alternative instead of `git ls-files`.'
12
+ Description: >-
13
+ Avoid using git to produce lists of files. Downstreams
14
+ often need to build your package in an environment
15
+ that does not have git (on purpose). Use some pure
16
+ Ruby alternative, like `Dir` or `Dir.glob`.
10
17
  Enabled: true
11
18
  VersionAdded: '0.1'
12
19
  VersionChanged: '0.1'
13
20
 
14
- Packaging/RequireRelativeHardcodingLib:
15
- Description: 'Avoid using `require_relative` with relative path to lib.'
21
+ Packaging/RequireHardcodingLib:
22
+ Description: >-
23
+ Avoid using `require` with relative path to lib. Use
24
+ `require` with absolute path instead.
16
25
  Enabled: true
17
- VersionAdded: '0.2'
18
- VersionChanged: '0.3'
26
+ VersionAdded: '0.4'
27
+ VersionChanged: '0.5'
19
28
 
20
- Packaging/RequireWithRelativePath:
21
- Description: 'Avoid using `require` with relative path.'
29
+ Packaging/RequireRelativeHardcodingLib:
30
+ Description: >-
31
+ Avoid using `require_relative` with relative path to
32
+ lib. Use `require` with absolute path instead.
22
33
  Enabled: true
23
- VersionAdded: '0.4'
34
+ VersionAdded: '0.2'
35
+ VersionChanged: '0.5'
@@ -19,10 +19,12 @@ module RuboCop # :nodoc:
19
19
  #
20
20
  class BundlerSetupInTests < Base
21
21
  include RuboCop::Packaging::LibHelperModule
22
+ include RangeHelp
23
+ extend AutoCorrector
22
24
 
23
25
  # This is the message that will be displayed when RuboCop::Packaging finds
24
26
  # an offense of using `require "bundler/setup"` in the tests directory.
25
- MSG = "Avoid using `bundler/setup` in your tests."
27
+ MSG = "Using `bundler/setup` in tests is redundant. Consider removing it."
26
28
 
27
29
  def_node_matcher :bundler_setup?, <<~PATTERN
28
30
  (send nil? :require
@@ -45,7 +47,17 @@ module RuboCop # :nodoc:
45
47
  def on_send(node)
46
48
  return unless bundler_setup?(node)
47
49
 
48
- add_offense(node)
50
+ add_offense(node) do |corrector|
51
+ autocorrect(corrector, node)
52
+ end
53
+ end
54
+
55
+ # Called from on_send, this method helps to autocorrect
56
+ # the offenses flagged by this cop.
57
+ def autocorrect(corrector, node)
58
+ range = range_by_whole_lines(node.source_range, include_final_newline: true)
59
+
60
+ corrector.remove(range)
49
61
  end
50
62
 
51
63
  # This method is called from inside `#def_node_matcher`.
@@ -11,14 +11,12 @@ module RuboCop # :nodoc:
11
11
  #
12
12
  # # bad
13
13
  # Gem::Specification.new do |spec|
14
- # spec.files = `git ls-files`.split("\n")
15
- # spec.test_files = `git ls-files -- spec`.split("\n")
14
+ # spec.files = `git ls-files`.split("\n")
16
15
  # end
17
16
  #
18
17
  # # good
19
18
  # Gem::Specification.new do |spec|
20
- # spec.files = Dir["lib/**/*", "LICENSE", "README.md"]
21
- # spec.test_files = Dir["spec/**/*"]
19
+ # spec.files = Dir["lib/**/*", "LICENSE", "README.md"]
22
20
  # end
23
21
  #
24
22
  # # bad
@@ -29,31 +27,31 @@ module RuboCop # :nodoc:
29
27
  # end
30
28
  #
31
29
  # # good
30
+ # require "rake/file_list"
31
+ #
32
32
  # Gem::Specification.new do |spec|
33
- # spec.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
33
+ # spec.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
34
34
  # end
35
35
  #
36
36
  # # bad
37
37
  # Gem::Specification.new do |spec|
38
- # spec.files = `git ls-files -- lib/`.split("\n")
39
- # spec.test_files = `git ls-files -- test/{functional,unit}/*`.split("\n")
40
- # spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
38
+ # spec.files = `git ls-files -- lib/`.split("\n")
39
+ # spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
41
40
  # end
42
41
  #
43
42
  # # good
44
43
  # Gem::Specification.new do |spec|
45
- # spec.files = Dir.glob("lib/**/*")
46
- # spec.test_files = Dir.glob("test/{functional,test}/*")
47
- # spec.executables = Dir.glob("bin/*").map{ |f| File.basename(f) }
44
+ # spec.files = Dir.glob("lib/**/*")
45
+ # spec.executables = Dir.glob("bin/*").map{ |f| File.basename(f) }
48
46
  # end
49
47
  #
50
48
  class GemspecGit < Base
51
49
  # This is the message that will be displayed when RuboCop finds an
52
50
  # offense of using `git ls-files`.
53
51
  MSG = "Avoid using git to produce lists of files. " \
54
- "Downstreams often need to build your package in an environment " \
55
- "that does not have git (on purpose). " \
56
- "Use some pure Ruby alternative, like `Dir` or `Dir.glob`."
52
+ "Downstreams often need to build your package in an environment " \
53
+ "that does not have git (on purpose). " \
54
+ "Use some pure Ruby alternative, like `Dir` or `Dir.glob`."
57
55
 
58
56
  def_node_search :xstr, <<~PATTERN
59
57
  (block
@@ -34,18 +34,21 @@ module RuboCop # :nodoc:
34
34
  # # good
35
35
  # require "baz/qux"
36
36
  #
37
- class RequireWithRelativePath < Base
37
+ class RequireHardcodingLib < Base
38
38
  include RuboCop::Packaging::LibHelperModule
39
+ extend AutoCorrector
39
40
 
40
41
  # This is the message that will be displayed when RuboCop::Packaging
41
42
  # finds an offense of using `require` with relative path to lib.
42
- MSG = "Avoid using `require` with relative path to `lib/`."
43
+ MSG = "Avoid using `require` with relative path to `lib/`. " \
44
+ "Use `require` with absolute path instead."
43
45
 
44
46
  def_node_matcher :require?, <<~PATTERN
45
47
  {(send nil? :require (str #falls_in_lib?))
46
48
  (send nil? :require (send (const nil? :File) :expand_path (str #falls_in_lib?) (send nil? :__dir__)))
47
49
  (send nil? :require (send (const nil? :File) :expand_path (str #falls_in_lib_using_file?) (str _)))
48
- (send nil? :require (send (send (const nil? :File) :dirname {(str _) (send nil? _)}) :+ (str #falls_in_lib_with_file_dirname_plus_str?)))}
50
+ (send nil? :require (send (send (const nil? :File) :dirname {(str _) (send nil? _)}) :+ (str #falls_in_lib_with_file_dirname_plus_str?)))
51
+ (send nil? :require (dstr (begin (send (const nil? :File) :dirname {(str _) (send nil? _)})) (str #falls_in_lib_with_file_dirname_plus_str?)))}
49
52
  PATTERN
50
53
 
51
54
  # Extended from the Base class.
@@ -64,28 +67,40 @@ module RuboCop # :nodoc:
64
67
  def on_send(node)
65
68
  return unless require?(node)
66
69
 
67
- add_offense(node)
70
+ add_offense(node) do |corrector|
71
+ corrector.replace(node, good_require_call)
72
+ end
73
+ end
74
+
75
+ # Called from on_send, this method helps to replace
76
+ # the "bad" require call with the "good" one.
77
+ def good_require_call
78
+ good_call = @str.sub(%r{^.*/lib/}, "")
79
+ %(require "#{good_call}")
68
80
  end
69
81
 
70
82
  # This method is called from inside `#def_node_matcher`.
71
83
  # It flags an offense if the `require` call is made from
72
84
  # anywhere except the "lib" directory.
73
85
  def falls_in_lib?(str)
74
- target_falls_in_lib?(str) && !inspected_file_falls_in_lib?
86
+ @str = str
87
+ target_falls_in_lib?(str) && inspected_file_is_not_in_lib_or_gemspec?
75
88
  end
76
89
 
77
90
  # This method is called from inside `#def_node_matcher`.
78
91
  # It flags an offense if the `require` call (using the __FILE__
79
92
  # arguement) is made from anywhere except the "lib" directory.
80
93
  def falls_in_lib_using_file?(str)
81
- target_falls_in_lib_using_file?(str) && !inspected_file_falls_in_lib?
94
+ @str = str
95
+ target_falls_in_lib_using_file?(str) && inspected_file_is_not_in_lib_or_gemspec?
82
96
  end
83
97
 
84
98
  # This method preprends a "." to the string that starts with "/".
85
99
  # And then determines if that call is made to "lib/".
86
100
  def falls_in_lib_with_file_dirname_plus_str?(str)
101
+ @str = str
87
102
  str.prepend(".")
88
- target_falls_in_lib?(str)
103
+ target_falls_in_lib?(str) && inspected_file_is_not_in_lib_or_gemspec?
89
104
  end
90
105
  end
91
106
  end
@@ -12,10 +12,10 @@ module RuboCop # :nodoc:
12
12
  # @example
13
13
  #
14
14
  # # bad
15
- # require_relative "lib/foo.rb"
15
+ # require_relative "lib/foo"
16
16
  #
17
17
  # # good
18
- # require "foo.rb"
18
+ # require "foo"
19
19
  #
20
20
  # # bad
21
21
  # require_relative "../../lib/foo/bar"
@@ -24,16 +24,17 @@ module RuboCop # :nodoc:
24
24
  # require "foo/bar"
25
25
  #
26
26
  # # good
27
- # require_relative "spec_helper"
28
- # require_relative "spec/foo/bar"
27
+ # require_relative "foo/bar/bax"
28
+ # require_relative "baz/qux"
29
29
  #
30
30
  class RequireRelativeHardcodingLib < Base
31
31
  include RuboCop::Packaging::LibHelperModule
32
+ extend AutoCorrector
32
33
 
33
34
  # This is the message that will be displayed when RuboCop finds an
34
35
  # offense of using `require_relative` with relative path to lib.
35
36
  MSG = "Avoid using `require_relative` with relative path to lib. " \
36
- "Use `require` instead."
37
+ "Use `require` with absolute path instead."
37
38
 
38
39
  def_node_matcher :require_relative, <<~PATTERN
39
40
  (send nil? :require_relative
@@ -56,14 +57,24 @@ module RuboCop # :nodoc:
56
57
  def on_send(node)
57
58
  return unless require_relative(node)
58
59
 
59
- add_offense(node)
60
+ add_offense(node) do |corrector|
61
+ corrector.replace(node, good_require_call)
62
+ end
63
+ end
64
+
65
+ # Called from on_send, this method helps to replace the
66
+ # "bad" require_relative call with the "good" one.
67
+ def good_require_call
68
+ good_call = File.expand_path(@str, @file_directory).delete_prefix("#{root_dir}/lib/")
69
+ %(require "#{good_call}")
60
70
  end
61
71
 
62
72
  # This method is called from inside `#def_node_matcher`.
63
73
  # It flags an offense if the `require_relative` call is made
64
74
  # from anywhere except the "lib" directory.
65
75
  def falls_in_lib?(str)
66
- target_falls_in_lib?(str) && !inspected_file_falls_in_lib? && !inspected_file_is_gemspec?
76
+ @str = str
77
+ target_falls_in_lib?(str) && inspected_file_is_not_in_lib_or_gemspec?
67
78
  end
68
79
  end
69
80
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  require_relative "packaging/bundler_setup_in_tests"
4
4
  require_relative "packaging/gemspec_git"
5
+ require_relative "packaging/require_hardcoding_lib"
5
6
  require_relative "packaging/require_relative_hardcoding_lib"
6
- require_relative "packaging/require_with_relative_path"
@@ -7,7 +7,7 @@ module RuboCop # :nodoc:
7
7
  module LibHelperModule
8
8
  # For determining the root directory of the project.
9
9
  def root_dir
10
- RuboCop::ConfigLoader.project_root
10
+ RuboCop::ConfigFinder.project_root
11
11
  end
12
12
 
13
13
  # This method determines if the calls are made to the "lib" directory.
@@ -30,6 +30,12 @@ module RuboCop # :nodoc:
30
30
  def inspected_file_is_gemspec?
31
31
  @file_path.end_with?("gemspec")
32
32
  end
33
+
34
+ # This method determines if the inspected file is not in lib/ or
35
+ # isn't a gemspec file.
36
+ def inspected_file_is_not_in_lib_or_gemspec?
37
+ !inspected_file_falls_in_lib? && !inspected_file_is_gemspec?
38
+ end
33
39
  end
34
40
  end
35
41
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Packaging
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.2"
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.4.0
4
+ version: 0.5.2
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-09-03 00:00:00.000000000 Z
11
+ date: 2022-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -84,18 +84,24 @@ dependencies:
84
84
  name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '1.33'
90
+ - - "<"
88
91
  - !ruby/object:Gem::Version
89
- version: '0.89'
92
+ version: '2.0'
90
93
  type: :runtime
91
94
  prerelease: false
92
95
  version_requirements: !ruby/object:Gem::Requirement
93
96
  requirements:
94
- - - "~>"
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '1.33'
100
+ - - "<"
95
101
  - !ruby/object:Gem::Version
96
- version: '0.89'
102
+ version: '2.0'
97
103
  description: |
98
- A collection of RuboCop cops to check for downstream compatability issues in the
104
+ A collection of RuboCop cops to check for downstream compatibility issues in the
99
105
  Ruby code.
100
106
  email:
101
107
  - utkarsh@debian.org
@@ -109,8 +115,8 @@ files:
109
115
  - lib/rubocop-packaging.rb
110
116
  - lib/rubocop/cop/packaging/bundler_setup_in_tests.rb
111
117
  - lib/rubocop/cop/packaging/gemspec_git.rb
118
+ - lib/rubocop/cop/packaging/require_hardcoding_lib.rb
112
119
  - lib/rubocop/cop/packaging/require_relative_hardcoding_lib.rb
113
- - lib/rubocop/cop/packaging/require_with_relative_path.rb
114
120
  - lib/rubocop/cop/packaging_cops.rb
115
121
  - lib/rubocop/packaging.rb
116
122
  - lib/rubocop/packaging/inject.rb
@@ -122,6 +128,7 @@ licenses:
122
128
  metadata:
123
129
  homepage_uri: https://github.com/utkarsh2102/rubocop-packaging
124
130
  source_code_uri: https://github.com/utkarsh2102/rubocop-packaging
131
+ rubygems_mfa_required: 'true'
125
132
  post_install_message:
126
133
  rdoc_options: []
127
134
  require_paths:
@@ -130,15 +137,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
137
  requirements:
131
138
  - - ">="
132
139
  - !ruby/object:Gem::Version
133
- version: 2.3.0
140
+ version: 2.6.0
134
141
  required_rubygems_version: !ruby/object:Gem::Requirement
135
142
  requirements:
136
143
  - - ">="
137
144
  - !ruby/object:Gem::Version
138
145
  version: '0'
139
146
  requirements: []
140
- rubygems_version: 3.1.2
147
+ rubygems_version: 3.3.5
141
148
  signing_key:
142
149
  specification_version: 4
143
- summary: Automatic downstream compatability checking tool for Ruby code
150
+ summary: Automatic downstream compatibility checking tool for Ruby code
144
151
  test_files: []