Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/erb_lint/linters/hard_coded_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HardCodedString < Linter
ALLOWED_CORRECTORS = ["I18nCorrector", "RuboCop::Corrector::I18n::HardCodedString"]

NON_TEXT_TAGS = Set.new(["script", "style", "xmp", "iframe", "noembed", "noframes", "listing"])
NO_TRANSLATION_NEEDED = Set.new([
NO_TRANSLATION_NEEDED = [
"&nbsp;",
"&amp;",
"&lt;",
Expand All @@ -43,11 +43,12 @@ class HardCodedString < Linter
"&times;",
"&laquo;",
"&raquo;",
])
]

class ConfigSchema < LinterConfig
property :corrector, accepts: Hash, required: false, default: -> { {} }
property :i18n_load_path, accepts: String, required: false, default: ""
property :ignored_words, accepts: array_of?(String), required: false, default: -> { [] }
end
self.config_schema = ConfigSchema

Expand Down Expand Up @@ -98,8 +99,15 @@ def autocorrect(processed_source, offense)
private

def check_string?(str)
string = str.gsub(/\s*/, "")
string.length > 1 && !NO_TRANSLATION_NEEDED.include?(string)
str
.gsub(ignored_words, "")
.gsub(/\s*/, "")
.gsub(/\d*/, "")
.length > 1
end

def ignored_words
@ignored_words ||= Regexp.union(*NO_TRANSLATION_NEEDED, *@config.ignored_words)
end

def load_corrector
Expand Down
18 changes: 18 additions & 0 deletions spec/erb_lint/linters/hard_coded_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@
it { expect(subject).to(eq([])) }
end

context "when file contains hard coded number" do
let(:file) { <<~FILE }
&copy; 2024
FILE

it { expect(subject).to(eq([])) }
end

context "when file contains hard coded string added to the list of ignored words" do
let(:file) { <<~FILE }
&copy; My brand 2024
FILE

let(:linter_options) { { ignored_words: ["My brand"] } }

it { expect(subject).to(eq([])) }
end

context "when file contains irrelevant hard coded string" do
let(:file) { <<~FILE }
<span class="example">
Expand Down