forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4e05e5
commit 666b8a0
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for access of app secrets and credentials. | ||
# Direct access returns nil if the entry does not exist. This can happen | ||
# with typo of entry name. Fetch raises an error upfront to avoid subtle | ||
# issues with nil. | ||
# | ||
# @example | ||
# # bad | ||
# Rails.application.secrets.foo | ||
# Rails.application.credentials.bar | ||
# | ||
# # good | ||
# Rails.application.secrets.fetch(:foo) | ||
# Rails.application.credentials.fetch(:bar) | ||
# Rails.application.credentials.fetch(:baz, "default_baz") | ||
class CredentialsDirectAccess < Cop | ||
MSG = 'Prefer fetch over direct access.' | ||
|
||
def_node_matcher :direct_access?, <<-PATTERN | ||
(send (send (send (const nil? :Rails) :application) ${:secrets :credentials}) _) | ||
PATTERN | ||
|
||
def on_send(node) | ||
add_offense(node, location: :selector) if direct_access?(node) | ||
end | ||
|
||
def autocorrect(node) | ||
selector = node.loc.selector | ||
lambda do |corrector| | ||
corrector.replace(selector, "fetch(:#{selector.source})") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::CredentialsDirectAccess do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
context 'secrets' do | ||
it 'registers an offense when not using fetch' do | ||
expect_offense(<<~RUBY) | ||
Rails.application.secrets.foo | ||
^^^ Prefer fetch over direct access. | ||
Rails.application.secrets.bar | ||
^^^ Prefer fetch over direct access. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with fetch' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.application.secrets.fetch(:foo) | ||
RUBY | ||
end | ||
|
||
it 'autocorrects to fetch' do | ||
new_source = autocorrect_source(<<~RUBY) | ||
Rails.application.secrets.foo | ||
RUBY | ||
|
||
expect(new_source).to eq(<<~RUBY) | ||
Rails.application.secrets.fetch(:foo) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'credentials' do | ||
it 'registers an offense when not using fetch' do | ||
expect_offense(<<~RUBY) | ||
Rails.application.credentials.foo | ||
^^^ Prefer fetch over direct access. | ||
Rails.application.credentials.bar | ||
^^^ Prefer fetch over direct access. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with fetch' do | ||
expect_no_offenses(<<~RUBY) | ||
Rails.application.credentials.fetch(:foo) | ||
RUBY | ||
end | ||
|
||
it 'autocorrects to fetch' do | ||
new_source = autocorrect_source(<<~RUBY) | ||
Rails.application.credentials.foo | ||
RUBY | ||
|
||
expect(new_source).to eq(<<~RUBY) | ||
Rails.application.credentials.fetch(:foo) | ||
RUBY | ||
end | ||
end | ||
end |