8000 Add `ignored_words` option to HardCodedString linter by krzyzak · Pull Request #397 · Shopify/erb_lint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add ignored_words option to HardCodedString linter #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
0