rubocop-packaging 0.4.0 → 0.5.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: ccfa9bb6e8043ae716d5349730d77dd122ad9fec2b6b6c278f79db7a604ba7aa
4
- data.tar.gz: f9a8b7c4fe1c3793827d27681af59a94495cb8f59e39e727c058ce6ce3a2f448
3
+ metadata.gz: 4e096b94cc72a619f1accaa3fda9021424a1954ddd3364d34df4c36e66cb8926
4
+ data.tar.gz: d96b87bbb45fd4448665e6578039f3c167818a78894066953cd4a87a0af3e2de
5
5
  SHA512:
6
- metadata.gz: cfd44f46a045f09ac6647f22e68e5351fe9d5640c061fe4fe37ec789f3b6bf0f4fbbee527edfcfafe25135cb2f86e37b45d446a1181b455de9064ca97270431a
7
- data.tar.gz: 68fb8cc0cdcb1e2bc8682c5e3aef39bcaa02ebd131ea70927cd18f5f2b63dabbafe1c5da49c54e878ee48ffbec90a83eece281251bd06d28afe15389f00896cd
6
+ metadata.gz: 2d5f7400a946fc39f6de1907f01445d13e3935299058db606edef6e1527dae2efeb944921bee5e7bce5dfddc73d8e4fd39c1cc3a16105b0e1dcdb550a8962e71
7
+ data.tar.gz: f122820131b25c3fe87ce4c4abe5cce1f9fa22bed5e0bbf60e5fe68273ca4f8a089a7479ca9bf11a85d34227cae8878fd6875c0b7a3216e0bb7377aee73b27ac
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!
@@ -1,23 +1,36 @@
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
+ Using `bundler/setup` in tests is redundant. Consider
14
+ Avoid using git to produce lists of files. Downstreams
15
+ often need to build your package in an environment
16
+ that does not have git (on purpose). Use some pure
17
+ Ruby alternative, like `Dir` or `Dir.glob`.
10
18
  Enabled: true
11
19
  VersionAdded: '0.1'
12
20
  VersionChanged: '0.1'
13
21
 
14
- Packaging/RequireRelativeHardcodingLib:
15
- Description: 'Avoid using `require_relative` with relative path to lib.'
22
+ Packaging/RequireHardcodingLib:
23
+ Description: >-
24
+ Avoid using `require` with relative path to lib. Use
25
+ `require` with absolute path instead.
16
26
  Enabled: true
17
- VersionAdded: '0.2'
18
- VersionChanged: '0.3'
27
+ VersionAdded: '0.4'
28
+ VersionChanged: '0.5'
19
29
 
20
- Packaging/RequireWithRelativePath:
21
- Description: 'Avoid using `require` with relative path.'
30
+ Packaging/RequireRelativeHardcodingLib:
31
+ Description: >-
32
+ Avoid using `require_relative` with relative path to
33
+ lib. Use `require` with absolute path instead.
22
34
  Enabled: true
23
- VersionAdded: '0.4'
35
+ VersionAdded: '0.2'
36
+ 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`.
@@ -29,6 +29,8 @@ module RuboCop # :nodoc:
29
29
  # end
30
30
  #
31
31
  # # good
32
+ # require "rake/file_list"
33
+ #
32
34
  # Gem::Specification.new do |spec|
33
35
  # spec.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
34
36
  # end
@@ -51,9 +53,9 @@ module RuboCop # :nodoc:
51
53
  # This is the message that will be displayed when RuboCop finds an
52
54
  # offense of using `git ls-files`.
53
55
  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`."
56
+ "Downstreams often need to build your package in an environment " \
57
+ "that does not have git (on purpose). " \
58
+ "Use some pure Ruby alternative, like `Dir` or `Dir.glob`."
57
59
 
58
60
  def_node_search :xstr, <<~PATTERN
59
61
  (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
@@ -29,11 +29,12 @@ module RuboCop # :nodoc:
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"
@@ -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.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.4.0
4
+ version: 0.5.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-09-03 00:00:00.000000000 Z
11
+ date: 2020-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -109,8 +109,8 @@ files:
109
109
  - lib/rubocop-packaging.rb
110
110
  - lib/rubocop/cop/packaging/bundler_setup_in_tests.rb
111
111
  - lib/rubocop/cop/packaging/gemspec_git.rb
112
+ - lib/rubocop/cop/packaging/require_hardcoding_lib.rb
112
113
  - lib/rubocop/cop/packaging/require_relative_hardcoding_lib.rb
113
- - lib/rubocop/cop/packaging/require_with_relative_path.rb
114
114
  - lib/rubocop/cop/packaging_cops.rb
115
115
  - lib/rubocop/packaging.rb
116
116
  - lib/rubocop/packaging/inject.rb
@@ -130,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
- version: 2.3.0
133
+ version: 2.4.0
134
134
  required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="