Skip to content

Commit

Permalink
Add Rubocop Airbnb gem
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Kerr committed Mar 1, 2018
1 parent 8d8fb05 commit dfdc84b
Show file tree
Hide file tree
Showing 69 changed files with 5,986 additions and 17 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Original file line Diff line number Diff line change
@@ -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

9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Original file line Diff line number Diff line change
@@ -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.
34 changes: 17 additions & 17 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ Thus when you create a TODO, it is almost always your name that is given.
end 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 Use keyword arguments (if available - in Ruby 2.0 or later) or an options
hash instead.<sup>[[link](#no-default-args)]</sup> hash instead.<sup>[[link](#no-default-args)]</sup>


Expand Down Expand Up @@ -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> * <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 # bad
unless x == 10 unless x == 10
... ...
end end
# good # good
if x != 10 if x != 10
... ...
end end
# bad # bad
unless x < 10 unless x < 10
... ...
end end
# good # good
if x >= 10 if x >= 10
... ...
end end
# ok # ok
unless x === 10 unless x === 10
... ...
Expand Down Expand Up @@ -835,13 +835,13 @@ In either case:
### Nested conditionals ### Nested conditionals
* <a name="no-nested-conditionals"></a> * <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> ([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 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 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: The general principles boil down to:
* Return immediately once you know your function cannot do anything more. * Return immediately once you know your function cannot do anything more.
* Reduce nesting and indentation in the code by returning early. This makes * Reduce nesting and indentation in the code by returning early. This makes
Expand Down Expand Up @@ -872,7 +872,7 @@ In either case:
return unless client return unless client
request = client.make_request request = client.make_request
return unless request return unless request
process_request(request) process_request(request)
end end
``` ```
Expand Down Expand Up @@ -1125,12 +1125,12 @@ In either case:
3. Referencing the current instance's class: `self.class`. 3. Referencing the current instance's class: `self.class`.


* <a name="freeze-constants"></a>When defining an object of any mutable * <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. examples are strings, arrays, and hashes.
([More on this][ruby-freeze].)<sup>[[link](#freeze-constants)]</sup> ([More on this][ruby-freeze].)<sup>[[link](#freeze-constants)]</sup>


The reason is that Ruby constants are actually mutable. Calling `freeze` 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 attempting to modify them will raise an exception. For strings, this allows
older versions of Ruby below 2.2 to intern them. older versions of Ruby below 2.2 to intern them.


Expand All @@ -1140,21 +1140,21 @@ In either case:
RED = 'red' RED = 'red'
BLUE = 'blue' BLUE = 'blue'
GREEN = 'green' GREEN = 'green'
ALL_COLORS = [ ALL_COLORS = [
RED, RED,
BLUE, BLUE,
GREEN, GREEN,
] ]
COLOR_TO_RGB = { COLOR_TO_RGB = {
RED => 0xFF0000, RED => 0xFF0000,
BLUE => 0x0000FF, BLUE => 0x0000FF,
GREEN => 0x00FF00, GREEN => 0x00FF00,
} }
end end
# good # good
class Color class Color
RED = 'red'.freeze RED = 'red'.freeze
BLUE = 'blue'.freeze BLUE = 'blue'.freeze
Expand All @@ -1165,7 +1165,7 @@ In either case:
BLUE, BLUE,
GREEN, GREEN,
].freeze ].freeze
COLOR_TO_RGB = { COLOR_TO_RGB = {
RED => 0xFF0000, RED => 0xFF0000,
BLUE => 0x0000FF, BLUE => 0x0000FF,
Expand Down
40 changes: 40 additions & 0 deletions rubocop-airbnb/.gitignore
Original file line number Original file line Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions rubocop-airbnb/.rspec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
12 changes: 12 additions & 0 deletions rubocop-airbnb/.rubocop.yml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
inherit_from:
- .rubocop_airbnb.yml
- ./config/default.yml

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

Rails:
Enabled: false
8 changes: 8 additions & 0 deletions rubocop-airbnb/.rubocop_airbnb.yml
Original file line number Original file line Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions rubocop-airbnb/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
# 1.0.0
* First public release of rubocop-airbnb
7 changes: 7 additions & 0 deletions rubocop-airbnb/Gemfile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

gemspec

group :test do
gem 'pry'
end
9 changes: 9 additions & 0 deletions rubocop-airbnb/LICENSE.md
Original file line number Original file line Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions rubocop-airbnb/PULL_REQUEST_TEMPLATE.md
Original file line number Original file line Diff line number Diff line change
@@ -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 --->
67 changes: 67 additions & 0 deletions rubocop-airbnb/README.md
Original file line number Original file line Diff line number Diff line change
@@ -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
```
39 changes: 39 additions & 0 deletions rubocop-airbnb/config/default.yml
Original file line number Original file line Diff line number Diff line change
@@ -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'
Loading

0 comments on commit dfdc84b

Please sign in to comment.