-
-
Notifications
You must be signed in to change notification settings - Fork 261
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
Showing
7 changed files
with
189 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
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,80 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop enforces that mailer names end with `Mailer` suffix. | ||
# | ||
# Without the `Mailer` suffix it isn't immediately apparent what's a mailer | ||
# and which views are related to the mailer. | ||
# | ||
# @example | ||
# # bad | ||
# class User < ActionMailer::Base | ||
# end | ||
# | ||
# class User < ApplicationMailer | ||
# end | ||
# | ||
# # good | ||
# class UserMailer < ActionMailer::Base | ||
# end | ||
# | ||
# class UserMailer < ApplicationMailer | ||
# end | ||
# | ||
class MailerName < Cop | ||
MSG = 'Mailer should end with `Mailer` suffix.' | ||
|
||
def_node_matcher :mailer_base_class?, <<~PATTERN | ||
{ | ||
(const (const nil? :ActionMailer) :Base) | ||
(const nil? :ApplicationMailer) | ||
} | ||
PATTERN | ||
|
||
def_node_matcher :class_definition?, <<~PATTERN | ||
(class $(const _ !#mailer_suffix?) #mailer_base_class? ...) | ||
PATTERN | ||
|
||
def_node_matcher :class_new_definition?, <<~PATTERN | ||
(send (const nil? :Class) :new #mailer_base_class?) | ||
PATTERN | ||
|
||
def on_class(node) | ||
class_definition?(node) do |name_node| | ||
add_offense(name_node) | ||
end | ||
end | ||
|
||
def on_send(node) | ||
return unless class_new_definition?(node) | ||
|
||
casgn_parent = node.each_ancestor(:casgn).first | ||
return unless casgn_parent | ||
|
||
name = casgn_parent.children[1] | ||
add_offense(casgn_parent, location: :name) unless mailer_suffix?(name) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
if node.casgn_type? | ||
name = node.children[1] | ||
corrector.replace(node.loc.name, "#{name}Mailer") | ||
else | ||
name = node.children.last | ||
corrector.replace(node.source_range, "#{name}Mailer") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def mailer_suffix?(mailer_name) | ||
mailer_name.to_s.end_with?('Mailer') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::MailerName do | ||
subject(:cop) { described_class.new } | ||
|
||
['ActionMailer::Base', 'ApplicationMailer'].each do |base| | ||
context 'when regular class definition' do | ||
it 'registers an offense and corrects when name without suffix' do | ||
expect_offense(<<~RUBY) | ||
class User < #{base} | ||
^^^^ Mailer should end with `Mailer` suffix. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class UserMailer < #{base} | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when name with suffix' do | ||
expect_no_offenses(<<~RUBY) | ||
class UserMailer < #{base} | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `Class.new` definition' do | ||
it 'registers an offense and corrects when name without suffix' do | ||
expect_offense(<<~RUBY) | ||
User = Class.new(#{base}) | ||
^^^^ Mailer should end with `Mailer` suffix. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
UserMailer = Class.new(#{base}) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when name with suffix' do | ||
expect_no_offenses(<<~RUBY) | ||
UserMailer = Class.new(#{base}) | ||
RUBY | ||
end | ||
end | ||
end | ||
end |