Skip to content
Permalink
Browse files

Add Rubocop Airbnb gem

  • Loading branch information...
Allen Kerr
Allen Kerr committed Feb 21, 2018
1 parent 8d8fb05 commit dfdc84b89284d537043b7eb4aa3ccc607940303f
Showing with 5,986 additions and 17 deletions.
  1. +15 −0 .travis.yml
  2. +9 −0 LICENSE.md
  3. +17 −17 README.md
  4. +40 −0 rubocop-airbnb/.gitignore
  5. +2 −0 rubocop-airbnb/.rspec
  6. +12 −0 rubocop-airbnb/.rubocop.yml
  7. +8 −0 rubocop-airbnb/.rubocop_airbnb.yml
  8. +2 −0 rubocop-airbnb/CHANGELOG.md
  9. +7 −0 rubocop-airbnb/Gemfile
  10. +9 −0 rubocop-airbnb/LICENSE.md
  11. +19 −0 rubocop-airbnb/PULL_REQUEST_TEMPLATE.md
  12. +67 −0 rubocop-airbnb/README.md
  13. +39 −0 rubocop-airbnb/config/default.yml
  14. +96 −0 rubocop-airbnb/config/rubocop-airbnb.yml
  15. +8 −0 rubocop-airbnb/config/rubocop-bundler.yml
  16. +9 −0 rubocop-airbnb/config/rubocop-gemspec.yml
  17. +514 −0 rubocop-airbnb/config/rubocop-layout.yml
  18. +315 −0 rubocop-airbnb/config/rubocop-lint.yml
  19. +47 −0 rubocop-airbnb/config/rubocop-metrics.yml
  20. +68 −0 rubocop-airbnb/config/rubocop-naming.yml
  21. +143 −0 rubocop-airbnb/config/rubocop-performance.yml
  22. +193 −0 rubocop-airbnb/config/rubocop-rails.yml
  23. +281 −0 rubocop-airbnb/config/rubocop-rspec.yml
  24. +13 −0 rubocop-airbnb/config/rubocop-security.yml
  25. +953 −0 rubocop-airbnb/config/rubocop-style.yml
  26. +11 −0 rubocop-airbnb/lib/rubocop-airbnb.rb
  27. +16 −0 rubocop-airbnb/lib/rubocop/airbnb.rb
  28. +14 −0 rubocop-airbnb/lib/rubocop/airbnb/inflections.rb
  29. +20 −0 rubocop-airbnb/lib/rubocop/airbnb/inject.rb
  30. +55 −0 rubocop-airbnb/lib/rubocop/airbnb/rails_autoloading.rb
  31. +8 −0 rubocop-airbnb/lib/rubocop/airbnb/version.rb
  32. +47 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/class_name.rb
  33. +138 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file.rb
  34. +74 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/const_assigned_in_wrong_file.rb
  35. +25 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/continuation_slash.rb
  36. +20 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/default_scope.rb
  37. +74 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/factory_attr_references_class.rb
  38. +39 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/factory_class_use_string.rb
  39. +18 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/mass_assignment_accessible_modifier.rb
  40. +104 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/module_method_in_wrong_file.rb
  41. +19 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/no_timeout.rb
  42. +38 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/opt_arg_parameters.rb
  43. +67 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/phrase_bundle_keys.rb
  44. +63 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/risky_activerecord_invocation.rb
  45. +114 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace.rb
  46. +58 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/rspec_environment_modification.rb
  47. +23 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/simple_modifier_conditional.rb
  48. +55 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/spec_constant_assignment.rb
  49. +47 −0 rubocop-airbnb/lib/rubocop/cop/airbnb/unsafe_yaml_marshal.rb
  50. +32 −0 rubocop-airbnb/rubocop-airbnb.gemspec
  51. +78 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/class_name_spec.rb
  52. +174 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/class_or_module_declared_in_wrong_file_spec.rb
  53. +178 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/const_assigned_in_wrong_file_spec.rb
  54. +162 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/continuation_slash_spec.rb
  55. +38 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/default_scope_spec.rb
  56. +160 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/factory_attr_references_class_spec.rb
  57. +26 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/factory_class_use_string_spec.rb
  58. +28 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/mass_assignment_accessible_modifier_spec.rb
  59. +181 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/module_method_in_wrong_file_spec.rb
  60. +30 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/no_timeout_spec.rb
  61. +103 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/opt_arg_parameter_spec.rb
  62. +74 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/phrase_bundle_keys_spec.rb
  63. +54 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/risky_activerecord_invocation_spec.rb
  64. +284 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/rspec_describe_or_context_under_namespace_spec.rb
  65. +64 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/rspec_environment_modification_spec.rb
  66. +122 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/simple_modifier_conditional_spec.rb
  67. +80 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/spec_constant_assignment_spec.rb
  68. +50 −0 rubocop-airbnb/spec/rubocop/cop/airbnb/unsafe_yaml_marshal_spec.rb
  69. +35 −0 rubocop-airbnb/spec/spec_helper.rb
@@ -0,0 +1,15 @@
language: ruby
cache:
bundler: true
sudo: false
rvm:
- 2.1.8
- 2.2
- 2.3
install:
- cd rubocop-airbnb
- bundle install
script:
- bundle exec rspec
- bundle exec rubocop --config .rubocop.yml

@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2012 Airbnb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -514,7 +514,7 @@ Thus when you create a TODO, it is almost always your name that is given.
end
```

* <a name="no-default-args"></a>Do not use default positional arguments.
* <a name="no-default-args"></a>Do not use default positional arguments.
Use keyword arguments (if available - in Ruby 2.0 or later) or an options
hash instead.<sup>[[link](#no-default-args)]</sup>

@@ -736,27 +736,27 @@ In either case:

* <a name="unless-with-comparison-operator"></a>Avoid `unless` with comparison operators if you can use `if` with an opposing comparison operator.<sup>[[link](#unless-with-comparison-operator)]</sup>

```ruby
```ruby
# bad
unless x == 10
...
end
# good
if x != 10
...
end
# bad
unless x < 10
...
end
# good
if x >= 10
...
end
# ok
unless x === 10
...
@@ -835,13 +835,13 @@ In either case:
### Nested conditionals

* <a name="no-nested-conditionals"></a>
Avoid the use of nested conditionals for flow of control.
Avoid the use of nested conditionals for flow of control.
([More on this][avoid-else-return-early].) <sup>[[link](#no-nested-conditionals)]</sup>

Prefer a guard clause when you can assert invalid data. A guard clause
is a conditional statement at the top of a function that returns as soon
as it can.
as it can.

The general principles boil down to:
* Return immediately once you know your function cannot do anything more.
* Reduce nesting and indentation in the code by returning early. This makes
@@ -872,7 +872,7 @@ In either case:
return unless client
request = client.make_request
return unless request
process_request(request)
process_request(request)
end
```

@@ -1125,12 +1125,12 @@ In either case:
3. Referencing the current instance's class: `self.class`.

* <a name="freeze-constants"></a>When defining an object of any mutable
type meant to be a constant, make sure to call `freeze` on it. Common
type meant to be a constant, make sure to call `freeze` on it. Common
examples are strings, arrays, and hashes.
([More on this][ruby-freeze].)<sup>[[link](#freeze-constants)]</sup>

The reason is that Ruby constants are actually mutable. Calling `freeze`
ensures they are not mutated and are therefore truly constant and
ensures they are not mutated and are therefore truly constant and
attempting to modify them will raise an exception. For strings, this allows
older versions of Ruby below 2.2 to intern them.

@@ -1140,21 +1140,21 @@ In either case:
RED = 'red'
BLUE = 'blue'
GREEN = 'green'
ALL_COLORS = [
RED,
BLUE,
GREEN,
]
COLOR_TO_RGB = {
RED => 0xFF0000,
BLUE => 0x0000FF,
GREEN => 0x00FF00,
}
end
# good
# good
class Color
RED = 'red'.freeze
BLUE = 'blue'.freeze
@@ -1165,7 +1165,7 @@ In either case:
BLUE,
GREEN,
].freeze
COLOR_TO_RGB = {
RED => 0xFF0000,
BLUE => 0x0000FF,
@@ -0,0 +1,40 @@
# Ignore bundler config
/.bundle
bundle
vendor/bundle/
vendor/rubocop/

# Ignore the default SQLite database.
/db/*.sqlite3

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# Ignore development bundled assets
.sass-cache/
public/assets/

/config/database.yml
/config/configatron/secure.yml
/config/initializers/aws_credentials.rb

.DS_Store
.rvmrc
.ruby-version
.*.swp
.\#*
\#*#
node_modules
npm-debug.log

rubocop-airbnb*.gem

# IDEs
.idea

# Temp files
*.swo
.byebug_history

Gemfile.lock
@@ -0,0 +1,2 @@
--color
--require spec_helper
@@ -0,0 +1,12 @@
inherit_from:
- .rubocop_airbnb.yml
- ./config/default.yml

AllCops:
CacheRootDirectory: tmp/rubocop
Exclude:
- 'vendor/**/*'
- 'node_modules/**/*'

Rails:
Enabled: false
@@ -0,0 +1,8 @@
# Default rules. These will be overridden by modified rules in
# `.rubocop_todo.yml`. Eventually, both this and `.rubocop_todo.yml` should be
# merged and moved "upstairs" into `.rubocop.yml`, but due to RuboCop's
# inheritance model we'll be doing this for now.

# For the rubocop-airbnb gem specifically, we want to load the local configuration.
require:
- ./lib/rubocop-airbnb.rb
@@ -0,0 +1,2 @@
# 1.0.0
* First public release of rubocop-airbnb
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

gemspec

group :test do
gem 'pry'
end
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2012 Airbnb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
#### Summary

<!--- required --->

#### Test Plan: what steps did you take to test this?

<!--- required --->

#### Reviewers

<!--- required --->

#### JIRA Task

<!--- optional --->

## Screenshots/GIF (if visual change)

<!--- optional --->
@@ -0,0 +1,67 @@
# RuboCop Airbnb

Airbnb specific analysis for [RuboCop](https://github.com/bbatsov/rubocop).

It contains Airbnb's internally used configuration for
[RuboCop](https://github.com/bbatsov/rubocop) and
[RuboCop RSpec](https://github.com/backus/rubocop-rspec). It also includes custom

## Installation

Just put this in your `Gemfile` it depends on the appropriate version of rubocop and rubocop-rspec.

```
gem 'rubocop-airbnb'
```

## Usage

You need to tell RuboCop to load the Airbnb extension. There are three
ways to do this:

### RuboCop configuration file
First Create a new file `.rubocop_airbnb.yml` in the same directory as your `.rubocop.yml`
this file should contain
```
require:
- rubocop-airbnb
```

Next add the following to `.rubocop.yml`
or add before `.rubocop_todo.yml` in your existin `inherit_from`

```
inherit_from:
- .rubocop_airbnb.yml
- .rubocop_todo.yml
```

You need to inherit `.rubocop_airbnb.yml` from another file becuase Rubocop order of operations.
It runs `inherit_from` before `require` commands. If the configuration is not in a separate file
you could potentially experience a bunch of warnings from `.rubocop_todo.yml` for non-existant
`Airbnb` rules.

Now you can run `rubocop` and it will automatically load the RuboCop Airbnb
cops together with the standard cops.

### Command line

```bash
rubocop --require rubocop-airbnb
```

## The Cops

All cops are located under
[`lib/rubocop/cop/airbnb`](lib/rubocop/cop/airbnb), and contain
examples/documentation.

In your `.rubocop.yml`, you may treat the Airbnb cops just like any other
cop. For example:

```yaml
Airbnb/PhraseBundleKeys:
Exclude:
- spec/my_poorly_named_spec_file.rb
```

@@ -0,0 +1,39 @@
Rails:
Enabled: true

##############
# Global rules

AllCops:
Exclude:
- '.chefrepo/**/*'
- '.vagrant/**/*'
- '.git/**/*'
- 'node_modules/**/*'
- 'tungsten/**/*'
- 'tungsten-support/**/*'
- 'vendor/**/*'
- Vagrantfile
- Guardfile
RSpec:
Patterns:
- _spec.rb
- "(?:^|/)spec/"
RSpec/FactoryBot:
Patterns:
- spec/factories/**/*.rb
- features/support/factories/**/*.rb

inherit_from:
- './rubocop-airbnb.yml'
- './rubocop-bundler.yml'
- './rubocop-gemspec.yml'
- './rubocop-layout.yml'
- './rubocop-lint.yml'
- './rubocop-metrics.yml'
- './rubocop-naming.yml'
- './rubocop-performance.yml'
- './rubocop-rails.yml'
- './rubocop-rspec.yml'
- './rubocop-security.yml'
- './rubocop-style.yml'

0 comments on commit dfdc84b

Please sign in to comment.
You can’t perform that action at this time.