Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Rails/UnknownEnv cop #4791

Merged
merged 1 commit into from
Sep 29, 2017
Merged

Add new Rails/UnknownEnv cop #4791

merged 1 commit into from
Sep 29, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Sep 25, 2017

This cop checks typo of Rails.env.foobar.

For example:

if Rails.env.proudction?
  #          ^^^^^^^^^^^ `proudction?` is incorrect environment name. Did you mean `production?`?
end

And, the incorrect environment name does not raise errors. It just return false.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

module RuboCop
module Cop
module Rails
# This cop checks typo of environment name.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It technically checks that the specified environment exists, and can also indicate if it's a typo or not. 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I replace it with your comment?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps something like:

This cop checks that environments called with Rails.env predicates exist.

WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍 I'll update this message tomorrow.

#
# # good
# Rails.env.production?
class Env < Cop
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably want a more descriptive name to make it easier to tell what it does, and to not clash with other potential cops. E.g. might want a cop that checks that Rails.env is used instead of ENV["RAILS_ENV"].

class Env < Cop
include NameSimilarity

MSG = '`%<name>s` is incorrect environment name.'.freeze
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably say "unknown" instead of "incorrect", as it gives a better hint what's wrong. 🙂

E.g.: "Unknown environment prduction. Did you mean production?"

PATTERN

def on_send(node)
typo_environment?(node) do |name|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might change typo to unknown.

end
end

def incorrect_env_name?(name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here might also change incorrect to unknown.

end

def environments
cop_config['Environments'].map { |env| env + '?' }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically the ? isn't part of the environment name. Might want to strip it when before checking include? instead.

@Drenmi
Copy link
Collaborator

Drenmi commented Sep 25, 2017

I think this is a brilliant cop! 😍 It can prevent a lot of catastrophic, silent errors, since Rails' string inquirer doesn't discriminate between what exists and not.

@ShockwaveNN
Copy link
Contributor

ShockwaveNN commented Sep 25, 2017

And what about custom environments?
Seems only development, test and production are permitted.
Maybe cop should scan config/environments dir?

@Drenmi
Copy link
Collaborator

Drenmi commented Sep 25, 2017

And what about custom environments?

There's a configuration option named Environments.

@pocke
Copy link
Collaborator Author

pocke commented Sep 25, 2017

And what about custom environments?

There's a configuration option named Environments.

👍

Maybe cop should scan config/environments dir?

If a file for environment does not exist under config/environments/ dir, Rails works.

$ rails new foo --skip-active-record
$ cd foo
$ RAILS_ENV=aaa bin/rails c # It works

And I think a cop should not depend other files existence.

@pocke
Copy link
Collaborator Author

pocke commented Sep 28, 2017

Updated

module RuboCop
module Cop
module Rails
# This cop checks that environments called with Rails.env predicates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to enclose Rails.env in backquotes.
BTW, This pragmatic cop is awesome. Thanks always.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to enclose Rails.env in backquotes.

Good catch, thanks!

BTW, This pragmatic cop is awesome. Thanks always.

😸

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Moreover, it seems necessary to regenerate the document with rake generate_cops_documentation.
https://travis-ci.org/bbatsov/rubocop/jobs/280723348#L590

@pocke pocke changed the title Add new Rails/Env cop Add new Rails/UnknownEnv cop Sep 28, 2017
This cop checks typo of `Rails.env.foobar`.

For example:

```ruby
if Rails.env.proudction?
  #          ^^^^^^^^^^^ `proudction?` is incorrect environment name. Did you mean `production?`?
end
```

And, the incorrect environment name does not raise errors. It just return `false`.
@pocke
Copy link
Collaborator Author

pocke commented Sep 28, 2017

@Drenmi @koic Thanks for reviewing!
I rebased it.

@bbatsov bbatsov merged commit d5e221d into rubocop:master Sep 29, 2017
@pocke pocke deleted the Rails/Env branch September 30, 2017 03:28
tessi added a commit to bitcrowd/rubocop-bitcrowd that referenced this pull request Jun 27, 2018
We usually have four rails environments (production, staging, development, test) where only production, development, and test are the default rails-defined environments. There is a new cop which checks for the environment list (rubocop/rubocop#4791) which we need to configure with our additional staging env.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants