rubocop-airbnb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2 -0
  3. data/Gemfile +7 -0
  4. data/LICENSE.md +9 -0
  5. data/README.md +68 -0
  6. data/config/default.yml +39 -0
  7. data/config/rubocop-airbnb.yml +96 -0
  8. data/config/rubocop-bundler.yml +8 -0
  9. data/config/rubocop-gemspec.yml +9 -0
  10. data/config/rubocop-layout.yml +514 -0
  11. data/config/rubocop-lint.yml +315 -0
  12. data/config/rubocop-metrics.yml +47 -0
  13. data/config/rubocop-naming.yml +68 -0
  14. data/config/rubocop-performance.yml +143 -0
  15. data/config/rubocop-rails.yml +193 -0
  16. data/config/rubocop-rspec.yml +281 -0
  17. data/config/rubocop-security.yml +13 -0
  18. data/config/rubocop-style.yml +953 -0
  19. data/lib/rubocop-airbnb.rb +11 -0
  20. data/lib/rubocop/airbnb.rb +16 -0
  21. data/lib/rubocop/airbnb/inflections.rb +14 -0
  22. data/lib/rubocop/airbnb/inject.rb +20 -0
  23. data/lib/rubocop/airbnb/rails_autoloading.rb +55 -0
  24. data/lib/rubocop/airbnb/version.rb +8 -0
  25. data/lib/rubocop/cop/airbnb/class_name.rb +47 -0
  26. data/lib/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file.rb +138 -0
  27. data/lib/rubocop/cop/airbnb/const_assigned_in_wrong_file.rb +74 -0
  28. data/lib/rubocop/cop/airbnb/continuation_slash.rb +25 -0
  29. data/lib/rubocop/cop/airbnb/default_scope.rb +20 -0
  30. data/lib/rubocop/cop/airbnb/factory_attr_references_class.rb +74 -0
  31. data/lib/rubocop/cop/airbnb/factory_class_use_string.rb +39 -0
  32. data/lib/rubocop/cop/airbnb/mass_assignment_accessible_modifier.rb +18 -0
  33. data/lib/rubocop/cop/airbnb/module_method_in_wrong_file.rb +104 -0
  34. data/lib/rubocop/cop/airbnb/no_timeout.rb +19 -0
  35. data/lib/rubocop/cop/airbnb/opt_arg_parameters.rb +38 -0
  36. data/lib/rubocop/cop/airbnb/phrase_bundle_keys.rb +67 -0
  37. data/lib/rubocop/cop/airbnb/risky_activerecord_invocation.rb +63 -0
  38. data/lib/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace.rb +114 -0
  39. data/lib/rubocop/cop/airbnb/rspec_environment_modification.rb +58 -0
  40. data/lib/rubocop/cop/airbnb/simple_modifier_conditional.rb +23 -0
  41. data/lib/rubocop/cop/airbnb/spec_constant_assignment.rb +55 -0
  42. data/lib/rubocop/cop/airbnb/unsafe_yaml_marshal.rb +47 -0
  43. data/rubocop-airbnb.gemspec +32 -0
  44. data/spec/rubocop/cop/airbnb/class_name_spec.rb +78 -0
  45. data/spec/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file_spec.rb +174 -0
  46. data/spec/rubocop/cop/airbnb/const_assigned_in_wrong_file_spec.rb +178 -0
  47. data/spec/rubocop/cop/airbnb/continuation_slash_spec.rb +162 -0
  48. data/spec/rubocop/cop/airbnb/default_scope_spec.rb +38 -0
  49. data/spec/rubocop/cop/airbnb/factory_attr_references_class_spec.rb +160 -0
  50. data/spec/rubocop/cop/airbnb/factory_class_use_string_spec.rb +26 -0
  51. data/spec/rubocop/cop/airbnb/mass_assignment_accessible_modifier_spec.rb +28 -0
  52. data/spec/rubocop/cop/airbnb/module_method_in_wrong_file_spec.rb +181 -0
  53. data/spec/rubocop/cop/airbnb/no_timeout_spec.rb +30 -0
  54. data/spec/rubocop/cop/airbnb/opt_arg_parameter_spec.rb +103 -0
  55. data/spec/rubocop/cop/airbnb/phrase_bundle_keys_spec.rb +74 -0
  56. data/spec/rubocop/cop/airbnb/risky_activerecord_invocation_spec.rb +54 -0
  57. data/spec/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace_spec.rb +284 -0
  58. data/spec/rubocop/cop/airbnb/rspec_environment_modification_spec.rb +64 -0
  59. data/spec/rubocop/cop/airbnb/simple_modifier_conditional_spec.rb +122 -0
  60. data/spec/rubocop/cop/airbnb/spec_constant_assignment_spec.rb +80 -0
  61. data/spec/rubocop/cop/airbnb/unsafe_yaml_marshal_spec.rb +50 -0
  62. data/spec/spec_helper.rb +35 -0
  63. metadata +150 -0
@@ -0,0 +1,80 @@
1
+ describe RuboCop::Cop::Airbnb::SpecConstantAssignment do
2
+ subject(:cop) { described_class.new }
3
+
4
+ it 'rejects constant definition inside of a describe block' do
5
+ source = [
6
+ 'describe Someclass do',
7
+ ' CONSTANT = 5',
8
+ 'end',
9
+ ].join("\n")
10
+
11
+ inspect_source(source)
12
+ expect(cop.offenses.size).to eq(1)
13
+ end
14
+
15
+ it 'allows constant defined inside of a module' do
16
+ source = [
17
+ 'module Someclass',
18
+ ' CONSTANT = 5',
19
+ 'end',
20
+ ].join("\n")
21
+
22
+ inspect_source(source)
23
+ expect(cop.offenses).to be_empty
24
+ end
25
+
26
+ it 'allows constant defined in global space' do
27
+ source = [
28
+ 'CONSTANT = 5',
29
+ ].join("\n")
30
+
31
+ inspect_source(source)
32
+ expect(cop.offenses.size).to eq(1)
33
+ end
34
+
35
+ it 'rejects constant assignment inside a before block' do
36
+ source = [
37
+ 'describe Someclass do',
38
+ ' before { CONSTANT = 5 }',
39
+ 'end',
40
+ ].join("\n")
41
+
42
+ inspect_source(source)
43
+ expect(cop.offenses.size).to eq(1)
44
+ end
45
+
46
+ it 'rejects namespaced constant assignment inside a before block' do
47
+ source = [
48
+ 'describe Someclass do',
49
+ ' before { MyModule::CONSTANT = 5 }',
50
+ 'end',
51
+ ].join("\n")
52
+
53
+ inspect_source(source)
54
+ expect(cop.offenses.size).to eq(1)
55
+ end
56
+
57
+ it 'rejects constant assignment inside it block' do
58
+ source = [
59
+ 'describe Someclass do',
60
+ ' it "tests stuff" do',
61
+ ' CONSTANT = 5',
62
+ ' end',
63
+ 'end',
64
+ ].join("\n")
65
+
66
+ inspect_source(source)
67
+ expect(cop.offenses.size).to eq(1)
68
+ end
69
+
70
+ it 'allows let statements that do not assign constants' do
71
+ source = [
72
+ 'describe Someclass do',
73
+ ' let(:constant) { 5 }',
74
+ 'end',
75
+ ].join("\n")
76
+
77
+ inspect_source(source)
78
+ expect(cop.offenses).to be_empty
79
+ end
80
+ end
@@ -0,0 +1,50 @@
1
+ describe RuboCop::Cop::Airbnb::UnsafeYamlMarshal do
2
+ subject(:cop) { described_class.new }
3
+
4
+ context 'send' do
5
+ it 'rejects YAML.load' do
6
+ source = [
7
+ 'def some_method(a)',
8
+ ' YAML.load(a)',
9
+ 'end',
10
+ ].join("\n")
11
+ inspect_source(source)
12
+ expect(cop.offenses.size).to eql(1)
13
+ expect(cop.offenses.first.message).to match(/`safe_load`, `parse`, `parse_file`/)
14
+ end
15
+
16
+ it 'rejects Psych.load' do
17
+ source = [
18
+ 'def some_method(a)',
19
+ ' Psych.load(a)',
20
+ 'end',
21
+ ].join("\n")
22
+ inspect_source(source)
23
+ expect(cop.offenses.size).to eql(1)
24
+ expect(cop.offenses.first.message).to match(/`safe_load`, `parse`, `parse_file`/)
25
+ end
26
+
27
+ it 'accepts YAML.safe_load' do
28
+ source = [
29
+ 'def some_method(a)',
30
+ ' YAML.safe_load(a)',
31
+ 'end',
32
+ ].join("\n")
33
+ inspect_source(source)
34
+ expect(cop.offenses.size).to eql(0)
35
+ end
36
+
37
+ it 'rejects on Marshal.load' do
38
+ source = [
39
+ 'def some_method(a)',
40
+ ' Marshal.load(a)',
41
+ 'end',
42
+ ].join("\n")
43
+ inspect_source(source)
44
+ expect(cop.offenses.size).to eql(1)
45
+ expect(cop.offenses.first.message).to match(
46
+ /`Marshal.load` on untrusted input can lead to remote code execution/
47
+ )
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ require 'rubocop'
2
+ require 'rubocop/rspec/support'
3
+ require 'pry'
4
+
5
+ module SpecHelper
6
+ ROOT = Pathname.new(__FILE__).parent.freeze
7
+ end
8
+
9
+ # Load in Rubocop cops
10
+ require File.expand_path('lib/rubocop-airbnb')
11
+
12
+ spec_helper_glob = File.expand_path('{support,shared}/*.rb', SpecHelper::ROOT)
13
+ Dir.glob(spec_helper_glob).map(&method(:require))
14
+
15
+ RSpec.configure do |config|
16
+ config.order = :random
17
+
18
+ # Define spec metadata for all rspec cop spec files
19
+ cop_specs = 'spec/rubocop/cop/rspec/'
20
+ config.define_derived_metadata(file_path: /\b#{cop_specs}/) do |metadata|
21
+ # Attach metadata that signals the specified code is for an RSpec only cop
22
+ metadata[:rspec_cop] = true
23
+ end
24
+
25
+ config.expect_with :rspec do |expectations|
26
+ expectations.syntax = :expect # Disable `should`
27
+ end
28
+
29
+ config.mock_with :rspec do |mocks|
30
+ mocks.syntax = :expect # Disable `should_receive` and `stub`
31
+ end
32
+ end
33
+
34
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
35
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-airbnb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Airbnb Engineering
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.52.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.52.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.22.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.22.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ description: |2
56
+ A plugin for RuboCop code style enforcing & linting tool. It includes Rubocop configuration
57
+ used at Airbnb and a few custom rules that have cause internal issues at Airbnb but are not
58
+ supported by core Rubocop.
59
+ email:
60
+ - rubocop@airbnb.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - LICENSE.md
68
+ - README.md
69
+ - config/default.yml
70
+ - config/rubocop-airbnb.yml
71
+ - config/rubocop-bundler.yml
72
+ - config/rubocop-gemspec.yml
73
+ - config/rubocop-layout.yml
74
+ - config/rubocop-lint.yml
75
+ - config/rubocop-metrics.yml
76
+ - config/rubocop-naming.yml
77
+ - config/rubocop-performance.yml
78
+ - config/rubocop-rails.yml
79
+ - config/rubocop-rspec.yml
80
+ - config/rubocop-security.yml
81
+ - config/rubocop-style.yml
82
+ - lib/rubocop-airbnb.rb
83
+ - lib/rubocop/airbnb.rb
84
+ - lib/rubocop/airbnb/inflections.rb
85
+ - lib/rubocop/airbnb/inject.rb
86
+ - lib/rubocop/airbnb/rails_autoloading.rb
87
+ - lib/rubocop/airbnb/version.rb
88
+ - lib/rubocop/cop/airbnb/class_name.rb
89
+ - lib/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file.rb
90
+ - lib/rubocop/cop/airbnb/const_assigned_in_wrong_file.rb
91
+ - lib/rubocop/cop/airbnb/continuation_slash.rb
92
+ - lib/rubocop/cop/airbnb/default_scope.rb
93
+ - lib/rubocop/cop/airbnb/factory_attr_references_class.rb
94
+ - lib/rubocop/cop/airbnb/factory_class_use_string.rb
95
+ - lib/rubocop/cop/airbnb/mass_assignment_accessible_modifier.rb
96
+ - lib/rubocop/cop/airbnb/module_method_in_wrong_file.rb
97
+ - lib/rubocop/cop/airbnb/no_timeout.rb
98
+ - lib/rubocop/cop/airbnb/opt_arg_parameters.rb
99
+ - lib/rubocop/cop/airbnb/phrase_bundle_keys.rb
100
+ - lib/rubocop/cop/airbnb/risky_activerecord_invocation.rb
101
+ - lib/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace.rb
102
+ - lib/rubocop/cop/airbnb/rspec_environment_modification.rb
103
+ - lib/rubocop/cop/airbnb/simple_modifier_conditional.rb
104
+ - lib/rubocop/cop/airbnb/spec_constant_assignment.rb
105
+ - lib/rubocop/cop/airbnb/unsafe_yaml_marshal.rb
106
+ - rubocop-airbnb.gemspec
107
+ - spec/rubocop/cop/airbnb/class_name_spec.rb
108
+ - spec/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file_spec.rb
109
+ - spec/rubocop/cop/airbnb/const_assigned_in_wrong_file_spec.rb
110
+ - spec/rubocop/cop/airbnb/continuation_slash_spec.rb
111
+ - spec/rubocop/cop/airbnb/default_scope_spec.rb
112
+ - spec/rubocop/cop/airbnb/factory_attr_references_class_spec.rb
113
+ - spec/rubocop/cop/airbnb/factory_class_use_string_spec.rb
114
+ - spec/rubocop/cop/airbnb/mass_assignment_accessible_modifier_spec.rb
115
+ - spec/rubocop/cop/airbnb/module_method_in_wrong_file_spec.rb
116
+ - spec/rubocop/cop/airbnb/no_timeout_spec.rb
117
+ - spec/rubocop/cop/airbnb/opt_arg_parameter_spec.rb
118
+ - spec/rubocop/cop/airbnb/phrase_bundle_keys_spec.rb
119
+ - spec/rubocop/cop/airbnb/risky_activerecord_invocation_spec.rb
120
+ - spec/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace_spec.rb
121
+ - spec/rubocop/cop/airbnb/rspec_environment_modification_spec.rb
122
+ - spec/rubocop/cop/airbnb/simple_modifier_conditional_spec.rb
123
+ - spec/rubocop/cop/airbnb/spec_constant_assignment_spec.rb
124
+ - spec/rubocop/cop/airbnb/unsafe_yaml_marshal_spec.rb
125
+ - spec/spec_helper.rb
126
+ homepage: https://github.com/airbnb/ruby
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '2.1'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.5.1
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Custom code style checking for Airbnb.
150
+ test_files: []