From 32985b46a5c27251ca42e55f2e014bf6a010ad63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krzy=C5=BCanowski?= Date: Thu, 12 Dec 2024 11:47:54 +0100 Subject: [PATCH] Add ability to pass list of ignored words --- lib/erb_lint/linters/hard_coded_string.rb | 16 ++++++++++++---- .../erb_lint/linters/hard_coded_string_spec.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/erb_lint/linters/hard_coded_string.rb b/lib/erb_lint/linters/hard_coded_string.rb index bc862a4..138ac01 100644 --- a/lib/erb_lint/linters/hard_coded_string.rb +++ b/lib/erb_lint/linters/hard_coded_string.rb @@ -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 = [ " ", "&", "<", @@ -43,11 +43,12 @@ class HardCodedString < Linter "×", "«", "»", - ]) + ] 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 @@ -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 diff --git a/spec/erb_lint/linters/hard_coded_string_spec.rb b/spec/erb_lint/linters/hard_coded_string_spec.rb index 8e9db2a..2c610cc 100644 --- a/spec/erb_lint/linters/hard_coded_string_spec.rb +++ b/spec/erb_lint/linters/hard_coded_string_spec.rb @@ -89,6 +89,24 @@ it { expect(subject).to(eq([])) } end + context "when file contains hard coded number" do + let(:file) { <<~FILE } + © 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 } + © 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 }