rubocop-packaging 0.3.0 → 0.4.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: 72be16aa270e6a0acac89f70963f75e8c4510932a76cf254bf8d4b54e4a25f43
4
- data.tar.gz: 68e3647ae5c13139e5fdf799bdc6e32b2e51df23c0ca676e60d2b6e29e3f0f0c
3
+ metadata.gz: ccfa9bb6e8043ae716d5349730d77dd122ad9fec2b6b6c278f79db7a604ba7aa
4
+ data.tar.gz: f9a8b7c4fe1c3793827d27681af59a94495cb8f59e39e727c058ce6ce3a2f448
5
5
  SHA512:
6
- metadata.gz: c938f05cabce5bccc176fc40ecb62c029718884f7cfbdcd35c7cdfe712164ef86827d3e1eb93ae64f03ebf3ee7ea086acfa1967082d65fdefd9fed851987a42e
7
- data.tar.gz: d7830f8e572577ba14554c2e8589ee99ae9898b91451143ab6b441c30d80930608ad97421582e38fdea212f047a826821ee1f83adbfd6f4261182af7b9bae9de
6
+ metadata.gz: cfd44f46a045f09ac6647f22e68e5351fe9d5640c061fe4fe37ec789f3b6bf0f4fbbee527edfcfafe25135cb2f86e37b45d446a1181b455de9064ca97270431a
7
+ data.tar.gz: 68fb8cc0cdcb1e2bc8682c5e3aef39bcaa02ebd131ea70927cd18f5f2b63dabbafe1c5da49c54e878ee48ffbec90a83eece281251bd06d28afe15389f00896cd
data/README.md CHANGED
@@ -10,7 +10,7 @@ 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://github.com/utkarsh2102/packaging-style-guide).
13
+ reasoning behind it, can be found here: [packaging-style-guide](https://packaging.rubystyle.guide/).
14
14
 
15
15
  In case anything is not clear, please feel free to raise an issue, asking
16
16
  for more explanation!
@@ -1,5 +1,10 @@
1
1
  # This is the default configuration file.
2
2
 
3
+ Packaging/BundlerSetupInTests:
4
+ Description: 'Avoid using `bundler/setup` in your tests.'
5
+ Enabled: true
6
+ VersionAdded: '0.4'
7
+
3
8
  Packaging/GemspecGit:
4
9
  Description: 'Use pure Ruby alternative instead of `git ls-files`.'
5
10
  Enabled: true
@@ -11,3 +16,8 @@ Packaging/RequireRelativeHardcodingLib:
11
16
  Enabled: true
12
17
  VersionAdded: '0.2'
13
18
  VersionChanged: '0.3'
19
+
20
+ Packaging/RequireWithRelativePath:
21
+ Description: 'Avoid using `require` with relative path.'
22
+ Enabled: true
23
+ VersionAdded: '0.4'
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop/packaging/lib_helper_module"
4
+
5
+ module RuboCop # :nodoc:
6
+ module Cop # :nodoc:
7
+ module Packaging # :nodoc:
8
+ # This cop flags the `require "bundler/setup"` calls if they're
9
+ # made from inside the tests directory.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # require "foo"
15
+ # require "bundler/setup"
16
+ #
17
+ # # good
18
+ # require "foo"
19
+ #
20
+ class BundlerSetupInTests < Base
21
+ include RuboCop::Packaging::LibHelperModule
22
+
23
+ # This is the message that will be displayed when RuboCop::Packaging finds
24
+ # an offense of using `require "bundler/setup"` in the tests directory.
25
+ MSG = "Avoid using `bundler/setup` in your tests."
26
+
27
+ def_node_matcher :bundler_setup?, <<~PATTERN
28
+ (send nil? :require
29
+ (str #bundler_setup_in_test_dir?))
30
+ PATTERN
31
+
32
+ # Extended from the Base class.
33
+ # More about the `#on_new_investigation` method can be found here:
34
+ # https://github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb
35
+ #
36
+ # Processing of the AST happens here.
37
+ def on_new_investigation
38
+ @file_path = processed_source.file_path
39
+ @file_directory = File.dirname(@file_path)
40
+ end
41
+
42
+ # Extended from AST::Traversal.
43
+ # More about the `#on_send` method can be found here:
44
+ # https://github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112
45
+ def on_send(node)
46
+ return unless bundler_setup?(node)
47
+
48
+ add_offense(node)
49
+ end
50
+
51
+ # This method is called from inside `#def_node_matcher`.
52
+ # It flags an offense if the `require "bundler/setup"`
53
+ # call is made from the tests directory.
54
+ def bundler_setup_in_test_dir?(str)
55
+ str.eql?("bundler/setup") && falls_in_test_dir?
56
+ end
57
+
58
+ # This method determines if the call is made *from* the tests directory.
59
+ def falls_in_test_dir?
60
+ %w[spec specs test tests].any? { |dir| File.expand_path(@file_directory).start_with?("#{root_dir}/#{dir}") }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -7,9 +7,6 @@ module RuboCop # :nodoc:
7
7
  # and suggests to use a plain Ruby alternative, like `Dir`,
8
8
  # `Dir.glob`, or `Rake::FileList` instead.
9
9
  #
10
- # More information about the GemspecGit cop can be found here:
11
- # https://github.com/utkarsh2102/packaging-style-guide#gemspec-git
12
- #
13
10
  # @example
14
11
  #
15
12
  # # bad
@@ -50,7 +47,7 @@ module RuboCop # :nodoc:
50
47
  # spec.executables = Dir.glob("bin/*").map{ |f| File.basename(f) }
51
48
  # end
52
49
  #
53
- class GemspecGit < Cop
50
+ class GemspecGit < Base
54
51
  # This is the message that will be displayed when RuboCop finds an
55
52
  # offense of using `git ls-files`.
56
53
  MSG = "Avoid using git to produce lists of files. " \
@@ -72,13 +69,12 @@ module RuboCop # :nodoc:
72
69
  # https://github.com/rubocop-hq/rubocop/blob/59543c8e2b66bff249de131fa9105f3eb11e9edb/lib/rubocop/cop/cop.rb#L13-L25
73
70
  #
74
71
  # Processing of the AST happens here.
75
- def investigate(processed_source)
72
+ def on_new_investigation
76
73
  return if processed_source.blank?
77
74
 
78
75
  xstr(processed_source.ast).each do |node|
79
76
  add_offense(
80
- processed_source.ast,
81
- location: node.loc.expression,
77
+ node.loc.expression,
82
78
  message: MSG
83
79
  )
84
80
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "rubocop/packaging/lib_helper_module"
4
+
3
5
  module RuboCop # :nodoc:
4
6
  module Cop # :nodoc:
5
7
  module Packaging # :nodoc:
@@ -7,9 +9,6 @@ module RuboCop # :nodoc:
7
9
  # mapping to the "lib" directory, except originating from lib/ or
8
10
  # the gemspec file, and suggests to use `require` instead.
9
11
  #
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
12
  # @example
14
13
  #
15
14
  # # bad
@@ -29,6 +28,8 @@ module RuboCop # :nodoc:
29
28
  # require_relative "spec/foo/bar"
30
29
  #
31
30
  class RequireRelativeHardcodingLib < Base
31
+ include RuboCop::Packaging::LibHelperModule
32
+
32
33
  # This is the message that will be displayed when RuboCop finds an
33
34
  # offense of using `require_relative` with relative path to lib.
34
35
  MSG = "Avoid using `require_relative` with relative path to lib. " \
@@ -58,33 +59,12 @@ module RuboCop # :nodoc:
58
59
  add_offense(node)
59
60
  end
60
61
 
61
- # For determining the root directory of the project.
62
- def root_dir
63
- RuboCop::ConfigLoader.project_root
64
- end
65
-
66
62
  # This method is called from inside `#def_node_matcher`.
67
63
  # It flags an offense if the `require_relative` call is made
68
64
  # from anywhere except the "lib" directory.
69
65
  def falls_in_lib?(str)
70
66
  target_falls_in_lib?(str) && !inspected_file_falls_in_lib? && !inspected_file_is_gemspec?
71
67
  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
68
  end
89
69
  end
90
70
  end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop/packaging/lib_helper_module"
4
+
5
+ module RuboCop # :nodoc:
6
+ module Cop # :nodoc:
7
+ module Packaging # :nodoc:
8
+ # This cop flags the `require` calls, from anywhere mapping to
9
+ # the "lib" directory, except originating from lib/.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # require "../lib/foo/bar"
15
+ #
16
+ # # good
17
+ # require "foo/bar"
18
+ #
19
+ # # bad
20
+ # require File.expand_path("../../lib/foo", __FILE__)
21
+ #
22
+ # # good
23
+ # require "foo"
24
+ #
25
+ # # bad
26
+ # require File.expand_path("../../../lib/foo/bar/baz/qux", __dir__)
27
+ #
28
+ # # good
29
+ # require "foo/bar/baz/qux"
30
+ #
31
+ # # bad
32
+ # require File.dirname(__FILE__) + "/../../lib/baz/qux"
33
+ #
34
+ # # good
35
+ # require "baz/qux"
36
+ #
37
+ class RequireWithRelativePath < Base
38
+ include RuboCop::Packaging::LibHelperModule
39
+
40
+ # This is the message that will be displayed when RuboCop::Packaging
41
+ # finds an offense of using `require` with relative path to lib.
42
+ MSG = "Avoid using `require` with relative path to `lib/`."
43
+
44
+ def_node_matcher :require?, <<~PATTERN
45
+ {(send nil? :require (str #falls_in_lib?))
46
+ (send nil? :require (send (const nil? :File) :expand_path (str #falls_in_lib?) (send nil? :__dir__)))
47
+ (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?)))}
49
+ PATTERN
50
+
51
+ # Extended from the Base class.
52
+ # More about the `#on_new_investigation` method can be found here:
53
+ # https://github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb
54
+ #
55
+ # Processing of the AST happens here.
56
+ def on_new_investigation
57
+ @file_path = processed_source.file_path
58
+ @file_directory = File.dirname(@file_path)
59
+ end
60
+
61
+ # Extended from AST::Traversal.
62
+ # More about the `#on_send` method can be found here:
63
+ # https://github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112
64
+ def on_send(node)
65
+ return unless require?(node)
66
+
67
+ add_offense(node)
68
+ end
69
+
70
+ # This method is called from inside `#def_node_matcher`.
71
+ # It flags an offense if the `require` call is made from
72
+ # anywhere except the "lib" directory.
73
+ def falls_in_lib?(str)
74
+ target_falls_in_lib?(str) && !inspected_file_falls_in_lib?
75
+ end
76
+
77
+ # This method is called from inside `#def_node_matcher`.
78
+ # It flags an offense if the `require` call (using the __FILE__
79
+ # arguement) is made from anywhere except the "lib" directory.
80
+ def falls_in_lib_using_file?(str)
81
+ target_falls_in_lib_using_file?(str) && !inspected_file_falls_in_lib?
82
+ end
83
+
84
+ # This method preprends a "." to the string that starts with "/".
85
+ # And then determines if that call is made to "lib/".
86
+ def falls_in_lib_with_file_dirname_plus_str?(str)
87
+ str.prepend(".")
88
+ target_falls_in_lib?(str)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "packaging/bundler_setup_in_tests"
3
4
  require_relative "packaging/gemspec_git"
4
5
  require_relative "packaging/require_relative_hardcoding_lib"
6
+ require_relative "packaging/require_with_relative_path"
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop # :nodoc:
4
+ module Packaging # :nodoc:
5
+ # This helper module extracts the methods which can be used
6
+ # in other cop classes.
7
+ module LibHelperModule
8
+ # For determining the root directory of the project.
9
+ def root_dir
10
+ RuboCop::ConfigLoader.project_root
11
+ end
12
+
13
+ # This method determines if the calls are made to the "lib" directory.
14
+ def target_falls_in_lib?(str)
15
+ File.expand_path(str, @file_directory).start_with?("#{root_dir}/lib")
16
+ end
17
+
18
+ # This method determines if the calls (using the __FILE__ argument)
19
+ # are made to the "lib" directory.
20
+ def target_falls_in_lib_using_file?(str)
21
+ File.expand_path(str, @file_path).start_with?("#{root_dir}/lib")
22
+ end
23
+
24
+ # This method determines if that call is made *from* the "lib" directory.
25
+ def inspected_file_falls_in_lib?
26
+ @file_path.start_with?("#{root_dir}/lib")
27
+ end
28
+
29
+ # This method determines if that call is made *from* the "gemspec" file.
30
+ def inspected_file_is_gemspec?
31
+ @file_path.end_with?("gemspec")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Packaging
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.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-08-19 00:00:00.000000000 Z
11
+ date: 2020-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -107,11 +107,14 @@ files:
107
107
  - README.md
108
108
  - config/default.yml
109
109
  - lib/rubocop-packaging.rb
110
+ - lib/rubocop/cop/packaging/bundler_setup_in_tests.rb
110
111
  - lib/rubocop/cop/packaging/gemspec_git.rb
111
112
  - lib/rubocop/cop/packaging/require_relative_hardcoding_lib.rb
113
+ - lib/rubocop/cop/packaging/require_with_relative_path.rb
112
114
  - lib/rubocop/cop/packaging_cops.rb
113
115
  - lib/rubocop/packaging.rb
114
116
  - lib/rubocop/packaging/inject.rb
117
+ - lib/rubocop/packaging/lib_helper_module.rb
115
118
  - lib/rubocop/packaging/version.rb
116
119
  homepage: https://github.com/utkarsh2102/rubocop-packaging
117
120
  licenses: