rubocop-packaging 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -3
- data/config/default.yml +2 -1
- data/lib/rubocop-packaging.rb +5 -5
- data/lib/rubocop/cop/packaging/gemspec_git.rb +27 -23
- data/lib/rubocop/cop/packaging/require_relative_hardcoding_lib.rb +91 -0
- data/lib/rubocop/cop/packaging_cops.rb +2 -2
- data/lib/rubocop/packaging.rb +2 -2
- data/lib/rubocop/packaging/version.rb +1 -1
- metadata +5 -5
- data/lib/rubocop/cop/packaging/relative_require_to_lib.rb +0 -67
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72be16aa270e6a0acac89f70963f75e8c4510932a76cf254bf8d4b54e4a25f43
|
4
|
+
data.tar.gz: 68e3647ae5c13139e5fdf799bdc6e32b2e51df23c0ca676e60d2b6e29e3f0f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
10
|
-
|
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
|
|
data/config/default.yml
CHANGED
@@ -6,7 +6,8 @@ Packaging/GemspecGit:
|
|
6
6
|
VersionAdded: '0.1'
|
7
7
|
VersionChanged: '0.1'
|
8
8
|
|
9
|
-
Packaging/
|
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'
|
data/lib/rubocop-packaging.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "rubocop"
|
4
4
|
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
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
|
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
|
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
|
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
|
17
|
+
# spec.files = `git ls-files`.split("\n")
|
18
|
+
# spec.test_files = `git ls-files -- spec`.split("\n")
|
15
19
|
# end
|
16
20
|
#
|
17
|
-
# #
|
21
|
+
# # good
|
18
22
|
# Gem::Specification.new do |spec|
|
19
|
-
# spec.files
|
20
|
-
#
|
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
|
27
|
-
#
|
28
|
-
#
|
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 =
|
34
|
-
# spec.test_files = Dir['spec/**/*']
|
36
|
+
# spec.files = Rake::FileList["**/*"].exclude(*File.read(".gitignore").split)
|
35
37
|
# end
|
36
38
|
#
|
37
|
-
# #
|
39
|
+
# # bad
|
38
40
|
# Gem::Specification.new do |spec|
|
39
|
-
# spec.files =
|
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(
|
45
|
-
# spec.test_files = Dir.glob(
|
46
|
-
# spec.executables = Dir.glob(
|
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 =
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
88
|
+
# It is used to find strings which start with "git".
|
85
89
|
def starts_with_git?(str)
|
86
|
-
str.start_with?(
|
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
|
data/lib/rubocop/packaging.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
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(
|
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)
|
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
|
+
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-
|
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.
|
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.
|
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/
|
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
|