forked from otwcode/otwarchive
-
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.
AO3-6732 Add custom cop for calling html_safe on translated strings (o…
…twcode#4890) * AO3-6732 Add custom cop for t().html_safe * AO3-6732 Add yml hint to ts -> t cop * AO3-6732 Remove html_safe from mailer greetings * AO3-6732 Apply suggestions from code review Co-authored-by: Brian Austin <13002992+brianjaustin@users.noreply.github.com> --------- Co-authored-by: Brian Austin <13002992+brianjaustin@users.noreply.github.com>
- Loading branch information
1 parent
35eadee
commit 4942692
Showing
37 changed files
with
151 additions
and
39 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
2 changes: 1 addition & 1 deletion
2
app/views/tag_wrangling_supervisor_mailer/wrangler_username_change_notification.html.erb
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
2 changes: 1 addition & 1 deletion
2
app/views/tag_wrangling_supervisor_mailer/wrangler_username_change_notification.text.erb
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
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
2 changes: 1 addition & 1 deletion
2
app/views/user_mailer/anonymous_or_unrevealed_notification.html.erb
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
2 changes: 1 addition & 1 deletion
2
app/views/user_mailer/anonymous_or_unrevealed_notification.text.erb
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
2 changes: 1 addition & 1 deletion
2
app/views/user_mailer/archivist_added_to_collection_notification.html.erb
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
2 changes: 1 addition & 1 deletion
2
app/views/user_mailer/archivist_added_to_collection_notification.text.erb
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
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
2 changes: 1 addition & 1 deletion
2
app/views/user_mailer/invited_to_collection_notification.html.erb
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
2 changes: 1 addition & 1 deletion
2
app/views/user_mailer/invited_to_collection_notification.text.erb
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
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module I18n | ||
# Checks for uses of html_safe on strings translated with `t`. | ||
# html_safe does not escape HTML in strings, making it potentially unsafe | ||
# to call on user-generated text like interpolation variables. | ||
# Renaming the locale key to end with `.html` or `_html` will escape interpolation variables | ||
# while keeping HTML from Rails helpers like link_to intact. | ||
# | ||
# @example | ||
# # bad | ||
# t(".has_invited", user_name: style_bold(@user_name)).html_safe | ||
# t(".about.popular", search_tags_link: link_to(t(".search_tags"), search_tags_path)).html_safe | ||
# | ||
# @example | ||
# # good | ||
# t(".has_invited.html", user_name: style_bold(@user_name)) | ||
# t(".about.popular_html", search_tags_link: link_to(t(".search_tags"), search_tags_path)) | ||
class HtmlSafeTranslation < RuboCop::Cop::Base | ||
MSG = "Prefer t(key) with locale keys ending in `_html` or `.html` over calling t(key).html_safe" | ||
|
||
RESTRICT_ON_SEND = %i[html_safe].freeze | ||
|
||
# @!method html_safe_translate?(node) | ||
def_node_matcher :html_safe_translate?, <<~PATTERN | ||
(send (send nil? {:t | :translate} ...) :html_safe) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless html_safe_translate?(node) | ||
|
||
add_offense(node) | ||
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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubocop_spec_helper" | ||
require_relative "../../../../rubocop/cop/i18n/html_safe_translation" | ||
|
||
describe RuboCop::Cop::I18n::HtmlSafeTranslation do | ||
context "when using translate" do | ||
it "records a violation for calling `html_safe` on it" do | ||
expect_offense(<<~INVALID) | ||
translate(".foo").html_safe | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer t(key) with locale keys ending in `_html` or `.html` over calling t(key).html_safe | ||
translate(".bar", input: "hello").html_safe | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer t(key) with locale keys ending in `_html` or `.html` over calling t(key).html_safe | ||
INVALID | ||
end | ||
|
||
it "does not record a violation when html_safe is not called" do | ||
expect_no_offenses(<<~RUBY) | ||
translate(".foo") | ||
translate(".bar", input: "hello") | ||
RUBY | ||
end | ||
end | ||
|
||
context "when using t" do | ||
it "records a violation for calling `html_safe` on it" do | ||
expect_offense(<<~INVALID) | ||
t(".foo").html_safe | ||
^^^^^^^^^^^^^^^^^^^ Prefer t(key) with locale keys ending in `_html` or `.html` over calling t(key).html_safe | ||
t(".bar", input: "hello").html_safe | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer t(key) with locale keys ending in `_html` or `.html` over calling t(key).html_safe | ||
INVALID | ||
end | ||
|
||
it "does not record a violation when html_safe is not called" do | ||
expect_no_offenses(<<~RUBY) | ||
t(".foo") | ||
t(".bar", input: "hello") | ||
RUBY | ||
end | ||
end | ||
|
||
# only the helpers in controllers and views support the html suffixes for HTML safe translations | ||
context "when using I18n.t" do | ||
it "does not record a violation for calling `html_safe` on it" do | ||
expect_no_offenses(<<~RUBY) | ||
I18n.t(".foo").html_safe | ||
I18n.t(".bar", input: "hello").html_safe | ||
RUBY | ||
end | ||
end | ||
|
||
# only the helpers in controllers and views support the html suffixes for HTML safe translations | ||
context "when using I18n.translate" do | ||
it "does not record a violation for calling `html_safe` on it" do | ||
expect_no_offenses(<<~RUBY) | ||
I18n.translate(".foo").html_safe | ||
I18n.translate(".bar", input: "hello").html_safe | ||
RUBY | ||
end | ||
end | ||
|
||
context "when using anther method" do | ||
it "does not record a violation for calling `html_safe` on it" do | ||
expect_no_offenses(<<~RUBY) | ||
cat(".foo").html_safe | ||
cat(".bar", input: "hello").html_safe | ||
not_translate(".foo").html_safe | ||
not_translate(".bar", input: "hello").html_safe | ||
RUBY | ||
end | ||
end | ||
end |