Commit Graph

324 Commits

Author SHA1 Message Date
Javi Martín
621523cfd9 Merge basic and standard rubocop files in one file
A bit of history in order to understand this change.

A year ago we introduced Hound so it would review our pull requests and
warn contributors when they weren't following our coding style.

However, back then we had many rules we hadn't reviewed yet, and we
weren't sure we wanted to ask contributors to follow them.

So we decided to split these files: .rubocop_basic.yml would contain
rules we had already agreed on and wanted contributors to respect, and
.rubocop.yml would contain rules we still had to review.

Now we've finally gone through all these rules. We've removed some of
them, kept some of them, added new ones, and applied them.

Now all rules with a severity level of "convention" or higher return no
offenses. The rules with "severity: refactor" return some offenses,
though:

* Metrics/LineLenght can only be properly accomplished when we define
better multi-line indentation standards, while in some cases long lines
indicate we need to refactor the code
* Rails/DynamicFindBy returns a few false positives
* Rails/HasManyOrHasOneDependent should by all means be implemented,
although it will not be a trivial task
* Rails/SaveBang returns a few false positives and there are a couple of
places where we skip it on purpose

There are also rules excluding some files:

* Rails/InverseOf returns a false positive
* Rails/OutputSafety is ignored on purpose when we add auto-links to a
text we trust
* RSpec/InstanceVariable returns false positives in two files

Other than that, everything works as expected.
2019-10-25 23:23:27 +02:00
Javi Martín
dd0dbee73b Reduce severity of HasManyOrHasOneDependent rule
This is actually a hack. We want Hound to warn us about this rule;
however, we don't want to be notified about our many existing offenses.

Ideally we would add the `dependent` option to all existing models.
However, this is extra tricky because adding this option would change
existing behavior.
2019-10-25 23:17:51 +02:00
Javi Martín
c06186a111 Move performance and security rules to basic cops
As mentioned in commit 9d566a22, I've kept these performance cops but
not for performance reasons but because they make the code easier to
read.

As for the security cops, we've never had problems with any of them, but
since we recently added an `eval` call by accident, there's a chance we
could do the same with JSONLoad and YAMLLoad.
2019-10-25 23:17:51 +02:00
Javi Martín
f3c31f0ea3 Remove MarshalLoad rubocop rule
We don't use Marshal objects in our application.
2019-10-25 23:17:51 +02:00
Javi Martín
97e826f2a4 Don't use update_attribute
This method is ambiguous. Sometimes we use it to set invalid data in
tests (which can usually be done with `update_column`), and other times
we use it instead of `update!`.

I'm removing it because, even if sometimes it could make sense to use
it, it's too similar to `update_attributes` (which is an alias for
`update` and runs validations), making it confusing.

However, there's one case where we're still using it: in the
ActsAsParanoidAliases module, we need to invoke the callbacks, which
`update_column` skips, but tests related to translations fail if we use
`update!`. The reason for this is the tests check what happens if we
restore a record without restoring its translations. But that will make
the record invalid, since there's a validation rule checking it has at
least one translation.

I'm not blacklisting any other method which skips validations because we
know they skip validations and use them anyway (hopefully with care).
2019-10-25 23:17:50 +02:00
Javi Martín
1da0fe1ee2 Apply Rails/HasAndBelongsToMany rubocop rule
We were using it everywhere except in one place.
2019-10-25 19:29:12 +02:00
Javi Martín
42d2e5b3ad Apply Rails/InverseOf rubocop rule
Not doing so has a few gotchas when working with relations, particularly
with records which are not stored in the database.

I'm excluding the related content file because it's got a very peculiar
relationship with itself: the `has_one :opposite_related_content` has no
inverse; the relation itself is its inverse. It's a false positive since
the inverse condition is true:

```
content.opposite_related_content.opposite_related_content.object_id ==
  content.object_id
```
2019-10-25 19:29:12 +02:00
Javi Martín
2fd79de068 Remove unneeded Rails/Exit rubocop rule
I don't think we need it because I've never seen it in our Rails
application.
2019-10-24 21:20:18 +02:00
Javi Martín
6214bda941 Move already in use Rails rules to basic cops
We were already applying the FindEach rule since commit cae210c1, while
the EnumUniqueness rule is essential when we use `enum`, and the
EnvironmentComparison rule is very useful since it forces us to use a
format the UnknownEnv rule will recognize.
2019-10-24 21:20:18 +02:00
Javi Martín
df959b74f6 Make migrations reversible
While I don't use this feature, there are developers who do. It's useful
when running migrations and changing branches.

I'm raising an `ActiveRecord::IrreversibleMigration` exception in every
`drop_table` migration because these migrations were all done before
version 1.0.0, and so making all of them reversible would be too much
work for little benefit.
2019-10-24 21:20:17 +02:00
Javi Martín
a9359fd570 Add rules related to migrations
These are rules we can't apply to existing migrations, so I'm excluding
migrations in the affected years from this check. However, we should
probably add timestamps columns and default values in non-null columns
to at least some of the tables which don't have them.
2019-10-24 21:20:17 +02:00
Javi Martín
93c6347b45 Apply Rails/FindBy rubocop rule
We were already using it in most places.
2019-10-23 18:29:09 +02:00
Javier Martín
181163f2ab Merge pull request #3789 from consul/duplicate_rule
Remove duplicate rubocop rule
2019-10-23 16:57:33 +02:00
Javi Martín
262fdd846c Remove duplicate rubocop rule
It was left by accident in commit b1b449b1.
2019-10-23 15:30:09 +02:00
Javi Martín
7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00
taitus
08957b70c2 Add Security/Eval rubocop rule to rubocop_basic.yml 2019-10-23 10:21:41 +02:00
Javi Martín
91c21b0982 Remove instance variables in RSpec
Instance variables might lead to hard-to-detect issues, since using a
nonexistent instance variable will return `nil` instead of raising an
error.
2019-09-30 16:43:10 +02:00
Javi Martín
7e9e1be1aa Remove Bundler/InsecureProtocolSource rubocop rule
Since we're using rubygems as the only source, this rule is not
necessary.
2019-09-10 22:27:33 +02:00
Javi Martín
16b8bee140 Remove unused rspec rubocop rules
We consider these rules either return false positives or we don't have a
strong opinion about them.
2019-09-10 22:27:33 +02:00
Javi Martín
3fa3801a49 Remove unused rails rubocop rules
We consider these rules either return false positives or we don't have a
strong opinion about them.
2019-09-10 22:27:33 +02:00
Javi Martín
d639cd587a Remove unnecessary uniq calls
The code `where(id: ids)` is equivalent to `where(id: ids.uniq)`.

Since Rails 5 uses `distinct` instead of `uniq` and in most cases where
we use `uniq` with `pluck` we should simply remove the `uniq` call (as
done in this commit), we're also removing the `Rails/UniqBeforePluck`
rubocop rule.
2019-09-10 22:27:33 +02:00
Javi Martín
9fb1d7df31 Remove obsolete ScopeArgs rubocop rule
This rule doesn't make sense with Rails 5 anymore, since breaking it
will raise an error.
2019-09-10 22:27:33 +02:00
Javi Martín
ea17256e3a Remove gemspec rubocop rules
We don't have a gemspec file, so we don't need these ones.
2019-09-10 22:27:33 +02:00
Javi Martín
9d566a2250 Remove most performance rubocop rules
For performance purposes, we need to find bottlenecks in our
application. Optimizing the performance of small methods doesn't make
the application faster.

I've kept a few cops because applying these ones IMHO make the code
easier to read.
2019-09-10 22:27:33 +02:00
Javi Martín
2243809d2e Move basic RSpec rubocop rules to basic cops
These are rules we were already applying.

We've excluded the `factories` folder for some rules because there's a
factory defining a `context` attribute, which rubocop thought was the
`context` RSpec keyword.
2019-09-10 21:43:39 +02:00
Javi Martín
59e107e565 Apply RSpec/HookArgument rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
9541ce892c Apply Bundler rubocop rules
We're not using the InsecureProtocolSource rule because I don't feel
it's necessary.
2019-09-10 21:43:39 +02:00
Javi Martín
c05b9c2aac Apply Capybara/CurrentPathExpectation rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
969a4e21c9 Apply RSpec/RepeatedExample rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
044eabd7ef Apply Rspec/LetSetup rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
58ba517717 Apply RSpec/ExampleWording rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
0788925c1b Apply Rails/Validation rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
27c73c22ea Configure Rails/UnknownEnv rubocop rule
We use staging and preproduction environments, which are not valid by
default.

This rule is useful because misspelling the name of an environment might
otherwise go unnoticed.
2019-09-10 21:43:39 +02:00
Javi Martín
9fe8c47528 Apply Rails/SafeNavigation rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
daa86ca3fc Apply Rails/RequestReferer rubocop rule 2019-09-10 21:43:39 +02:00
Javi Martín
a5ba13b599 Apply Rails/Presence rubocop rule 2019-09-10 21:43:38 +02:00
Javi Martín
adc5906253 Apply Rails/PluralizationGrammar rubocop rule 2019-09-10 21:43:38 +02:00
Javi Martín
b5b07bccd3 Apply PercentLiteralDelimiters rubocop rule 2019-09-10 20:02:15 +02:00
Javi Martín
a27ada662d Move Rails/Date and Rails/TimeZone to basic cops
Using Date.today and Time.now is a common mistake which might lead to
obscure bugs. Now we're making sure we'll receive a warning when a pull
request uses these methods.
2019-08-28 20:32:40 +02:00
Javi Martín
c2869f4887 Move Rails/RelativeDateConstant to basic cops
The reason for this rule is similar to dynamic factories: we want
dynamic dates to be evaluted relative to the current time, and not
relative to the moment the application was loaded.
2019-08-07 19:43:59 +02:00
Javi Martín
444fc524f7 Use the AttributeDefinedStatically rubocop rule
Factory bot has stopped supporting dynamic attributes, and we'll have to
change all factories before upgrading.

In order to apply the rubocop rule, we need to bump rubocop-rspec to its
latest version.
2019-08-07 19:41:45 +02:00
Javi Martín
61be48a6c6 Remove unused and obsolete rubocop rules
These rules are now defined under `Style/`. However, we don't really use
them, so they add noise to the rubocop configuration file without
solving any issues we actually have.
2019-08-07 19:41:45 +02:00
Javi Martín
ae98dbf683 Use Rails 5.1 conventions in tests params 2019-08-07 15:50:00 +02:00
Javi Martín
765d405df1 Use Rails 5 conventions in migrations and models
We forgot to add these changes to pull requests which were in
development before we upgraded to Rails 5.

We're also moving the rubocop rules to the basic files, so we're
notified when we inherit from `ActiveRecord::Base`.
2019-08-07 13:53:27 +02:00
decabeza
15dbfdf6e8 Enable line length rubocop rule 2019-01-09 13:45:42 +01:00
voodoorai2000
c1455cb93c Add not_to Rubocop rule to Hound
https://www.rubydoc.info/gems/rubocop-rspec/1.5.0/RuboCop/Cop/RSpec/NotToNot
2018-12-15 12:41:07 +01:00
Javier Martín
bc77812ae8 Merge pull request #2990 from consul/backport-remove_described_class_cop
Remove described class cop
2018-10-29 14:38:43 +01:00
Javi Martín
4cbe81a142 Remove described class cop
We've agreed `User.new` is easier to read than `described_class.new` and
since we are ignoring Hound's comments regarding this topic, we might as
well remove it.
2018-10-29 13:58:00 +01:00
Javi Martín
4048d17203 Add basic rubocop configuraton for Hound
This way we can ask contributors to follow some basic guidelines like
removing trailing whitespaces while not overwhelming them with all our
rules.
2018-10-26 11:46:17 +02:00
Javi Martín
48140f74e9 Remove rubocop_todo file
No developers are maintaining it anymore.
2018-10-26 11:46:10 +02:00