rubocop-airbnb 4.0.0 → 5.0.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/CHANGELOG.md +7 -0
- data/config/default.yml +9 -8
- data/config/rubocop-performance.yml +3 -0
- data/config/rubocop-rails.yml +3 -0
- data/config/rubocop-rspec.yml +4 -9
- data/lib/rubocop/airbnb/version.rb +1 -1
- data/lib/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file.rb +1 -1
- data/lib/rubocop/cop/airbnb/const_assigned_in_wrong_file.rb +1 -1
- data/lib/rubocop/cop/airbnb/continuation_slash.rb +1 -1
- data/lib/rubocop/cop/airbnb/default_scope.rb +1 -1
- data/lib/rubocop/cop/airbnb/factory_attr_references_class.rb +1 -1
- data/lib/rubocop/cop/airbnb/factory_class_use_string.rb +1 -1
- data/lib/rubocop/cop/airbnb/mass_assignment_accessible_modifier.rb +1 -1
- data/lib/rubocop/cop/airbnb/module_method_in_wrong_file.rb +1 -1
- data/lib/rubocop/cop/airbnb/no_timeout.rb +1 -1
- data/lib/rubocop/cop/airbnb/opt_arg_parameters.rb +1 -1
- data/lib/rubocop/cop/airbnb/phrase_bundle_keys.rb +1 -1
- data/lib/rubocop/cop/airbnb/risky_activerecord_invocation.rb +1 -1
- data/lib/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace.rb +1 -1
- data/lib/rubocop/cop/airbnb/rspec_environment_modification.rb +1 -1
- data/lib/rubocop/cop/airbnb/simple_modifier_conditional.rb +1 -1
- data/lib/rubocop/cop/airbnb/simple_unless.rb +1 -1
- data/lib/rubocop/cop/airbnb/spec_constant_assignment.rb +3 -4
- data/lib/rubocop/cop/airbnb/unsafe_yaml_marshal.rb +1 -1
- data/rubocop-airbnb.gemspec +3 -3
- data/spec/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file_spec.rb +69 -102
- data/spec/rubocop/cop/airbnb/const_assigned_in_wrong_file_spec.rb +58 -101
- data/spec/rubocop/cop/airbnb/continuation_slash_spec.rb +77 -112
- data/spec/rubocop/cop/airbnb/default_scope_spec.rb +24 -31
- data/spec/rubocop/cop/airbnb/factory_attr_references_class_spec.rb +81 -121
- data/spec/rubocop/cop/airbnb/factory_class_use_string_spec.rb +12 -20
- data/spec/rubocop/cop/airbnb/mass_assignment_accessible_modifier_spec.rb +17 -22
- data/spec/rubocop/cop/airbnb/module_method_in_wrong_file_spec.rb +75 -114
- data/spec/rubocop/cop/airbnb/no_timeout_spec.rb +16 -22
- data/spec/rubocop/cop/airbnb/opt_arg_parameter_spec.rb +46 -73
- data/spec/rubocop/cop/airbnb/phrase_bundle_keys_spec.rb +5 -20
- data/spec/rubocop/cop/airbnb/risky_activerecord_invocation_spec.rb +19 -33
- data/spec/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace_spec.rb +109 -187
- data/spec/rubocop/cop/airbnb/rspec_environment_modification_spec.rb +31 -41
- data/spec/rubocop/cop/airbnb/simple_modifier_conditional_spec.rb +64 -88
- data/spec/rubocop/cop/airbnb/simple_unless_spec.rb +17 -27
- data/spec/rubocop/cop/airbnb/spec_constant_assignment_spec.rb +42 -60
- data/spec/rubocop/cop/airbnb/unsafe_yaml_marshal_spec.rb +24 -36
- data/spec/spec_helper.rb +2 -0
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02cd198bf2e20a941f06c78615775bbbcf525f428a41adbd07d1ef4a7e716509
|
4
|
+
data.tar.gz: 1073b91470656809fb195a4eba990e9303e7389a94af1dba88728bba14ec5c4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db877cf8c1eb61e805eb3a700cd489bbc2b3b61cf7b307ed2ae66e3930aa57f85e41bd14740e7117e1ff1d0c7cc0701de542b05e9bfebcb4a180dae976bd2cb1
|
7
|
+
data.tar.gz: 5392e711442b997a3fdccd3cc17eaec5b9d153df6803a4227221c82b20acb24a841e8643e99311c77a2be36b8283bb7afa99e00629b2c4d8a6baa73ae49c697c
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# 5.0.0
|
2
|
+
* Add support for Ruby 3.1
|
3
|
+
* Drop support for Ruby 2.4
|
4
|
+
* Update rubocop to 1.22.0
|
5
|
+
* Update rubocop-rspec to 2.0.0
|
6
|
+
* Remove deprecated cops InvalidPredicateMatcher and CustomIncludeMethods
|
7
|
+
|
1
8
|
# 4.0.0
|
2
9
|
* Add support for Ruby 3.0
|
3
10
|
* Run CI against Ruby 2.7
|
data/config/default.yml
CHANGED
@@ -15,14 +15,6 @@ AllCops:
|
|
15
15
|
- 'vendor/**/*'
|
16
16
|
- Vagrantfile
|
17
17
|
- Guardfile
|
18
|
-
RSpec:
|
19
|
-
Patterns:
|
20
|
-
- _spec.rb
|
21
|
-
- "(?:^|/)spec/"
|
22
|
-
RSpec/FactoryBot:
|
23
|
-
Patterns:
|
24
|
-
- spec/factories/**/*.rb
|
25
|
-
- features/support/factories/**/*.rb
|
26
18
|
|
27
19
|
# While Rubocop has released a bunch of new cops, not all of these cops have been evaluated as
|
28
20
|
# part of this styleguide. To prevent new, unevaluated cops from imposing on this styleguide, we
|
@@ -33,6 +25,15 @@ AllCops:
|
|
33
25
|
# https://github.com/rubocop/rubocop/blob/1e55b1aa5e4c5eaeccad5d61f08b7930ed6bc341/config/default.yml#L89-L101
|
34
26
|
DisabledByDefault: true
|
35
27
|
|
28
|
+
RSpec:
|
29
|
+
Include:
|
30
|
+
- _spec.rb
|
31
|
+
- "(?:^|/)spec/"
|
32
|
+
RSpec/FactoryBot:
|
33
|
+
Include:
|
34
|
+
- spec/factories/**/*.rb
|
35
|
+
- features/support/factories/**/*.rb
|
36
|
+
|
36
37
|
inherit_from:
|
37
38
|
- './rubocop-airbnb.yml'
|
38
39
|
- './rubocop-bundler.yml'
|
data/config/rubocop-rails.yml
CHANGED
data/config/rubocop-rspec.yml
CHANGED
@@ -56,7 +56,6 @@ RSpec/DescribedClass:
|
|
56
56
|
RSpec/EmptyExampleGroup:
|
57
57
|
Description: Checks if an example group does not include any tests.
|
58
58
|
Enabled: true
|
59
|
-
CustomIncludeMethods: []
|
60
59
|
|
61
60
|
RSpec/EmptyLineAfterExampleGroup:
|
62
61
|
Description: Checks if there is an empty line after example group blocks.
|
@@ -155,10 +154,6 @@ RSpec/InstanceVariable:
|
|
155
154
|
Description: 'Checks for the usage of instance variables.'
|
156
155
|
Enabled: false
|
157
156
|
|
158
|
-
RSpec/InvalidPredicateMatcher:
|
159
|
-
Description: Checks invalid usage for predicate matcher.
|
160
|
-
Enabled: false
|
161
|
-
|
162
157
|
RSpec/ItBehavesLike:
|
163
158
|
Description: Checks that only one `it_behaves_like` style is used.
|
164
159
|
Enabled: false
|
@@ -314,18 +309,18 @@ RSpec/VoidExpect:
|
|
314
309
|
Description: This cop checks void `expect()`.
|
315
310
|
Enabled: false
|
316
311
|
|
317
|
-
Capybara/CurrentPathExpectation:
|
312
|
+
RSpec/Capybara/CurrentPathExpectation:
|
318
313
|
Description: Checks that no expectations are set on Capybara's `current_path`.
|
319
314
|
Enabled: false
|
320
315
|
|
321
|
-
Capybara/FeatureMethods:
|
316
|
+
RSpec/Capybara/FeatureMethods:
|
322
317
|
Description: Checks for consistent method usage in feature specs.
|
323
318
|
Enabled: false
|
324
319
|
|
325
|
-
FactoryBot/AttributeDefinedStatically:
|
320
|
+
RSpec/FactoryBot/AttributeDefinedStatically:
|
326
321
|
Description: Always declare attribute values as blocks.
|
327
322
|
Enabled: false
|
328
323
|
|
329
|
-
FactoryBot/CreateList:
|
324
|
+
RSpec/FactoryBot/CreateList:
|
330
325
|
Description: Checks for create_list usage.
|
331
326
|
Enabled: true
|
@@ -3,7 +3,7 @@ module RuboCop
|
|
3
3
|
module Airbnb
|
4
4
|
# Cop to help prevent the scorge of Default Scopes from ActiveRecord.
|
5
5
|
# Once in place they are almost impossible to remove.
|
6
|
-
class DefaultScope <
|
6
|
+
class DefaultScope < Base
|
7
7
|
MSG = 'Avoid `default_scope`. Default scopes make it difficult to '\
|
8
8
|
'refactor data access patterns since the scope becomes part '\
|
9
9
|
'of every query unless explicitly excluded, even when it is '\
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
# Cop to enforce "attr { CONST }" instead of "attr CONST" in factories,
|
5
5
|
# because the latter forces autoload, which slows down spec startup time and
|
6
6
|
# Zeus reload time after touching a model.
|
7
|
-
class FactoryAttrReferencesClass <
|
7
|
+
class FactoryAttrReferencesClass < Base
|
8
8
|
MSG = "Instead of attr_name MyClass::MY_CONST, use attr_name { MyClass::MY_CONST }. " \
|
9
9
|
"This enables faster spec startup time and Zeus reload time.".freeze
|
10
10
|
|
@@ -3,7 +3,7 @@ module RuboCop
|
|
3
3
|
module Airbnb
|
4
4
|
# Cop to tell developers to use :class => "MyClass" instead of :class => MyClass,
|
5
5
|
# because the latter slows down reloading zeus.
|
6
|
-
class FactoryClassUseString <
|
6
|
+
class FactoryClassUseString < Base
|
7
7
|
MSG = 'Instead of :class => MyClass, use :class => "MyClass". ' \
|
8
8
|
"This enables faster spec startup time and faster Zeus reload time.".freeze
|
9
9
|
|
@@ -3,7 +3,7 @@ module RuboCop
|
|
3
3
|
module Airbnb
|
4
4
|
# Modifying Mass assignment restrictions eliminates the entire point of disabling
|
5
5
|
# mass assignment. It's a lazy, potentially dangerous approach that should be discouraged.
|
6
|
-
class MassAssignmentAccessibleModifier <
|
6
|
+
class MassAssignmentAccessibleModifier < Base
|
7
7
|
MSG = 'Do no override and objects mass assignment restrictions.'.freeze
|
8
8
|
|
9
9
|
def on_send(node)
|
@@ -3,7 +3,7 @@ module RuboCop
|
|
3
3
|
module Airbnb
|
4
4
|
# Cop to enforce use of options hash over default arguments
|
5
5
|
# https://github.com/airbnb/ruby#no-default-args
|
6
|
-
class OptArgParameters <
|
6
|
+
class OptArgParameters < Base
|
7
7
|
MSG =
|
8
8
|
'Do not use default positional arguments. '\
|
9
9
|
'Use keyword arguments or an options hash instead.'.freeze
|
@@ -2,7 +2,7 @@ module RuboCop
|
|
2
2
|
module Cop
|
3
3
|
module Airbnb
|
4
4
|
# Disallow ActiveRecord calls that pass interpolated or added strings as an argument.
|
5
|
-
class RiskyActiverecordInvocation <
|
5
|
+
class RiskyActiverecordInvocation < Base
|
6
6
|
VULNERABLE_AR_METHODS = [
|
7
7
|
:delete_all,
|
8
8
|
:destroy_all,
|
@@ -20,7 +20,7 @@ module RuboCop
|
|
20
20
|
#
|
21
21
|
# describe Foo::Bar
|
22
22
|
# end
|
23
|
-
class RspecDescribeOrContextUnderNamespace <
|
23
|
+
class RspecDescribeOrContextUnderNamespace < Base
|
24
24
|
DESCRIBE_OR_CONTEXT_UNDER_NAMESPACE_MSG =
|
25
25
|
'Declaring a `module` in a spec can break autoloading because subsequent references ' \
|
26
26
|
'to it will not cause it to be loaded from the app. This could cause flaky tests.'.freeze
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
# before(:each) do
|
29
29
|
# stub_env(:production)
|
30
30
|
# end
|
31
|
-
class RspecEnvironmentModification <
|
31
|
+
class RspecEnvironmentModification < Base
|
32
32
|
def_node_matcher :allow_or_expect_rails_env, <<-PATTERN
|
33
33
|
(send (send nil? {:expect :allow} (send (const nil? :Rails) :env)) :to ...)
|
34
34
|
PATTERN
|
@@ -3,7 +3,7 @@ module RuboCop
|
|
3
3
|
module Airbnb
|
4
4
|
# Cop to tackle prevent more complicated modifier if/unless statements
|
5
5
|
# https://github.com/airbnb/ruby#only-simple-if-unless
|
6
|
-
class SimpleModifierConditional <
|
6
|
+
class SimpleModifierConditional < Base
|
7
7
|
MSG = 'Modifier if/unless usage is okay when the body is simple, ' \
|
8
8
|
'the condition is simple, and the whole thing fits on one line. ' \
|
9
9
|
'Otherwise, avoid modifier if/unless.'.freeze
|
@@ -3,7 +3,7 @@ module RuboCop
|
|
3
3
|
module Airbnb
|
4
4
|
# Cop to tackle prevent unless statements with multiple conditions
|
5
5
|
# https://github.com/airbnb/ruby#unless-with-multiple-conditions
|
6
|
-
class SimpleUnless <
|
6
|
+
class SimpleUnless < Base
|
7
7
|
MSG = 'Unless usage is okay when there is only one conditional'.freeze
|
8
8
|
|
9
9
|
def_node_matcher :multiple_conditionals?, '(if ({and or :^} ...) ...)'
|
@@ -25,16 +25,15 @@ module RuboCop
|
|
25
25
|
# describe Something do
|
26
26
|
# before { stub_const('MyClass::PAYLOAD', [1, 2, 3])
|
27
27
|
# end
|
28
|
-
class SpecConstantAssignment <
|
29
|
-
include RuboCop::RSpec::TopLevelDescribe
|
28
|
+
class SpecConstantAssignment < Base
|
30
29
|
MESSAGE = "Defining constants inside of specs can cause spurious behavior. " \
|
31
|
-
"It is almost always preferable to use `let` statements, "\
|
30
|
+
"It is almost always preferable to use `let` statements, " \
|
32
31
|
"anonymous class/module definitions, or stub_const".freeze
|
33
32
|
|
34
33
|
def on_casgn(node)
|
35
34
|
return unless in_spec_file?(node)
|
36
35
|
parent_module_name = node.parent_module_name
|
37
|
-
if
|
36
|
+
if parent_module_name && parent_module_name != "Object"
|
38
37
|
return
|
39
38
|
end
|
40
39
|
add_offense(node, message: MESSAGE)
|
@@ -2,7 +2,7 @@ module RuboCop
|
|
2
2
|
module Cop
|
3
3
|
module Airbnb
|
4
4
|
# Disallow use of YAML/Marshal methods that can trigger RCE on untrusted input
|
5
|
-
class UnsafeYamlMarshal <
|
5
|
+
class UnsafeYamlMarshal < Base
|
6
6
|
MSG = 'Using unsafe YAML parsing methods on untrusted input can lead ' \
|
7
7
|
'to remote code execution. Use `safe_load`, `parse`, `parse_file`, or ' \
|
8
8
|
'`parse_stream` instead'.freeze
|
data/rubocop-airbnb.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.license = 'MIT'
|
16
16
|
spec.version = RuboCop::Airbnb::VERSION
|
17
17
|
spec.platform = Gem::Platform::RUBY
|
18
|
-
spec.required_ruby_version = '>= 2.
|
18
|
+
spec.required_ruby_version = '>= 2.5'
|
19
19
|
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
spec.files = Dir[
|
@@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
|
|
25
25
|
'Gemfile',
|
26
26
|
]
|
27
27
|
|
28
|
-
spec.add_dependency('rubocop', '~>
|
28
|
+
spec.add_dependency('rubocop', '~> 1.22.0')
|
29
29
|
spec.add_dependency('rubocop-performance', '~> 1.10.2')
|
30
30
|
spec.add_dependency('rubocop-rails', '~> 2.9.1')
|
31
|
-
spec.add_dependency('rubocop-rspec', '~>
|
31
|
+
spec.add_dependency('rubocop-rspec', '~> 2.0.0')
|
32
32
|
spec.add_development_dependency('rspec', '~> 3.5')
|
33
33
|
end
|
@@ -1,6 +1,4 @@
|
|
1
|
-
describe RuboCop::Cop::Airbnb::ClassOrModuleDeclaredInWrongFile do
|
2
|
-
subject(:cop) { described_class.new(config) }
|
3
|
-
|
1
|
+
describe RuboCop::Cop::Airbnb::ClassOrModuleDeclaredInWrongFile, :config do
|
4
2
|
let(:config) do
|
5
3
|
RuboCop::Config.new(
|
6
4
|
{
|
@@ -25,150 +23,119 @@ describe RuboCop::Cop::Airbnb::ClassOrModuleDeclaredInWrongFile do
|
|
25
23
|
end
|
26
24
|
|
27
25
|
it 'rejects if class declaration is in a file with non-matching name' do
|
28
|
-
source = [
|
29
|
-
'module Foo',
|
30
|
-
' module Bar',
|
31
|
-
' class Baz',
|
32
|
-
' end',
|
33
|
-
' end',
|
34
|
-
'end',
|
35
|
-
].join("\n")
|
36
|
-
|
37
26
|
File.open "#{models_dir}/qux.rb", "w" do |file|
|
38
|
-
|
27
|
+
expect_offense(<<~RUBY, file)
|
28
|
+
module Foo
|
29
|
+
^^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class/module, move its definition to a file that matches its name. Module Foo should be defined in foo.rb.
|
30
|
+
module Bar
|
31
|
+
^^^^^^^^^^ In order for Rails autoloading [...]
|
32
|
+
class Baz
|
33
|
+
^^^^^^^^^ In order for Rails autoloading [...]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
RUBY
|
39
38
|
end
|
40
|
-
|
41
|
-
expect(cop.offenses.size).to eq(3)
|
42
|
-
expect(cop.offenses.map(&:line).sort).to eq([1, 2, 3])
|
43
|
-
expect(cop.offenses.first.message).to include('Module Foo should be defined in foo.rb.')
|
44
39
|
end
|
45
40
|
|
46
41
|
it 'rejects if class declaration is in a file with matching name but wrong parent dir' do
|
47
|
-
source = [
|
48
|
-
'module Foo',
|
49
|
-
' module Bar',
|
50
|
-
' class Baz',
|
51
|
-
' end',
|
52
|
-
' end',
|
53
|
-
'end',
|
54
|
-
].join("\n")
|
55
|
-
|
56
42
|
File.open "#{models_dir}/baz.rb", "w" do |file|
|
57
|
-
|
43
|
+
expect_offense(<<~RUBY, file)
|
44
|
+
module Foo
|
45
|
+
^^^^^^^^^^ In order for Rails autoloading [...]
|
46
|
+
module Bar
|
47
|
+
^^^^^^^^^^ In order for Rails autoloading [...]
|
48
|
+
class Baz
|
49
|
+
^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class/module, move its definition to a file that matches its name. Class Baz should be defined in foo/bar/baz.rb.
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
RUBY
|
58
54
|
end
|
59
|
-
|
60
|
-
expect(cop.offenses.size).to eq(3)
|
61
|
-
expect(cop.offenses.map(&:line).sort).to eq([1, 2, 3])
|
62
|
-
expect(cop.offenses.last.message).to include('Class Baz should be defined in foo/bar/baz.rb.')
|
63
55
|
end
|
64
56
|
|
65
57
|
it 'accepts if class declaration is in a file with matching name and right parent dir' do
|
66
|
-
source = [
|
67
|
-
'module Foo',
|
68
|
-
' module Bar',
|
69
|
-
' class Baz',
|
70
|
-
' end',
|
71
|
-
' end',
|
72
|
-
'end',
|
73
|
-
].join("\n")
|
74
|
-
|
75
58
|
FileUtils.mkdir_p "#{models_dir}/foo/bar"
|
76
59
|
File.open "#{models_dir}/foo/bar/baz.rb", "w" do |file|
|
77
|
-
|
60
|
+
expect_no_offenses(<<~RUBY, file)
|
61
|
+
module Foo
|
62
|
+
module Bar
|
63
|
+
class Baz
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
RUBY
|
78
68
|
end
|
79
|
-
|
80
|
-
expect(cop.offenses).to be_empty
|
81
69
|
end
|
82
70
|
|
83
71
|
it 'rejects if class declaration is in wrong dir and parent module uses ::' do
|
84
|
-
source = [
|
85
|
-
'module Foo::Bar',
|
86
|
-
' class Baz',
|
87
|
-
' end',
|
88
|
-
'end',
|
89
|
-
].join("\n")
|
90
|
-
|
91
72
|
FileUtils.mkdir_p "#{models_dir}/bar"
|
92
73
|
File.open "#{models_dir}/bar/baz.rb", "w" do |file|
|
93
|
-
|
74
|
+
expect_offense(<<~RUBY, file)
|
75
|
+
module Foo::Bar
|
76
|
+
^^^^^^^^^^^^^^^ In order for Rails autoloading [...]
|
77
|
+
class Baz
|
78
|
+
^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class/module, move its definition to a file that matches its name. Class Baz should be defined in foo/bar/baz.rb.
|
79
|
+
end
|
80
|
+
end
|
81
|
+
RUBY
|
94
82
|
end
|
95
|
-
|
96
|
-
expect(cop.offenses.map(&:line).sort).to eq([1, 2])
|
97
|
-
expect(cop.offenses.last.message).to include('Class Baz should be defined in foo/bar/baz.rb.')
|
98
83
|
end
|
99
84
|
|
100
85
|
it 'accepts if class declaration is in a file with matching name and parent module uses ::' do
|
101
|
-
source = [
|
102
|
-
'module Foo::Bar',
|
103
|
-
' class Baz',
|
104
|
-
' end',
|
105
|
-
'end',
|
106
|
-
].join("\n")
|
107
|
-
|
108
86
|
FileUtils.mkdir_p "#{models_dir}/foo/bar"
|
109
87
|
File.open "#{models_dir}/foo/bar/baz.rb", "w" do |file|
|
110
|
-
|
88
|
+
expect_no_offenses(<<~RUBY, file)
|
89
|
+
module Foo::Bar
|
90
|
+
class Baz
|
91
|
+
end
|
92
|
+
end
|
93
|
+
RUBY
|
111
94
|
end
|
112
|
-
|
113
|
-
expect(cop.offenses).to be_empty
|
114
95
|
end
|
115
96
|
|
116
97
|
it 'accepts class declaration where the containing class uses an acronym' do
|
117
|
-
source = [
|
118
|
-
'module CSVFoo',
|
119
|
-
' class Baz',
|
120
|
-
' end',
|
121
|
-
'end',
|
122
|
-
].join("\n")
|
123
|
-
|
124
98
|
FileUtils.mkdir_p "#{models_dir}/csv_foo"
|
125
99
|
File.open "#{models_dir}/csv_foo/baz.rb", "w" do |file|
|
126
|
-
|
100
|
+
expect_no_offenses(<<~RUBY)
|
101
|
+
module CSVFoo
|
102
|
+
class Baz
|
103
|
+
end
|
104
|
+
end
|
105
|
+
RUBY
|
127
106
|
end
|
128
|
-
|
129
|
-
expect(cop.offenses).to be_empty
|
130
107
|
end
|
131
108
|
|
132
109
|
it 'ignores class/module declaration in a rake task' do
|
133
|
-
source = [
|
134
|
-
'class Baz',
|
135
|
-
'end',
|
136
|
-
].join("\n")
|
137
|
-
|
138
110
|
File.open "#{models_dir}/foo.rake", "w" do |file|
|
139
|
-
|
111
|
+
expect_no_offenses(<<~RUBY, file)
|
112
|
+
class Baz
|
113
|
+
end
|
114
|
+
RUBY
|
140
115
|
end
|
141
|
-
|
142
|
-
expect(cop.offenses).to be_empty
|
143
116
|
end
|
144
117
|
|
145
118
|
it 'suggests moving error classes into the file that defines the owning scope' do
|
146
|
-
source = [
|
147
|
-
'module Foo',
|
148
|
-
' class BarError < StandardError; end',
|
149
|
-
'end',
|
150
|
-
].join("\n")
|
151
|
-
|
152
119
|
File.open "#{models_dir}/bar.rb", "w" do |file|
|
153
|
-
|
120
|
+
expect_offense(<<~RUBY, file)
|
121
|
+
module Foo
|
122
|
+
^^^^^^^^^^ In order for Rails autoloading [...]
|
123
|
+
class BarError < StandardError; end
|
124
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class, move its definition to a file that defines the owning module. Class BarError should be defined in foo.rb.
|
125
|
+
end
|
126
|
+
RUBY
|
154
127
|
end
|
155
|
-
|
156
|
-
expect(cop.offenses.map(&:line)).to include(2)
|
157
|
-
expect(cop.offenses.map(&:message)).to include(%r{Class BarError should be defined in foo\.rb.})
|
158
128
|
end
|
159
129
|
|
160
130
|
it 'recognizes error class based on the superclass name' do
|
161
|
-
source = [
|
162
|
-
'module Foo',
|
163
|
-
' class Bar < StandardError; end',
|
164
|
-
'end',
|
165
|
-
].join("\n")
|
166
|
-
|
167
131
|
File.open "#{models_dir}/bar.rb", "w" do |file|
|
168
|
-
|
132
|
+
expect_offense(<<~RUBY, file)
|
133
|
+
module Foo
|
134
|
+
^^^^^^^^^^ In order for Rails autoloading [...]
|
135
|
+
class Bar < StandardError; end
|
136
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In order for Rails autoloading to be able to find and load this file when someone references this class, move its definition to a file that defines the owning module. Class Bar should be defined in foo.rb.
|
137
|
+
end
|
138
|
+
RUBY
|
169
139
|
end
|
170
|
-
|
171
|
-
expect(cop.offenses.map(&:line)).to include(2)
|
172
|
-
expect(cop.offenses.map(&:message)).to include(%r{Class Bar should be defined in foo\.rb.})
|
173
140
|
end
|
174
141
|
end
|