Skip to content

Commit 0262793

Browse files
authored
Prism: Detects more invalid scopes (#671)
1 parent d4bf09b commit 0262793

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/i18n/tasks/scanners/prism_scanners/nodes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ def scope
140140
return nil if @options.nil?
141141
return nil unless @options["scope"]
142142

143-
fail(ScopeError, "Could not process scope") if @options.key?("scope") && Array(@options["scope"]).empty?
143+
fail(ScopeError, "Could not process scope") if @options.key?("scope") && (Array(@options["scope"]).empty? || !Array(@options["scope"]).all? { |s| s.is_a?(String) || s.is_a?(Symbol) })
144144

145-
Array(@options["scope"]).compact.map(&:to_s).join(".")
145+
Array(@options["scope"]).join(".")
146146
end
147147

148148
def occurrence(file_path)

spec/prism_scanner_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,18 +594,28 @@ def what
594594
it "handles scope" do
595595
source = <<~RUBY
596596
scope = 'special.events'
597+
# These should be detected
597598
t('scope_string', scope: 'events.descriptions')
598599
I18n.t('scope_array', scope: ['events', 'titles'])
599-
I18n.t(model.key, **translation_options(model))
600-
I18n.t("success", scope: scope)
601600
I18n.t("scope_array_symbol", scope: %i[events descriptions])
602601
I18n.t("scope_array_words", scope: %w[events descriptions])
602+
603+
# Cannot handle, should ignore
604+
I18n.t("scope_with_known_variable", scope: ["this", "that", scope])
605+
I18n.t("scope_with_unknown", scope: ["this", "that", unknown, "other"])
606+
I18n.t(model.key, **translation_options(model))
607+
I18n.t("success", scope: scope)
603608
RUBY
604609

605610
occurrences = process_string("scope.rb", source)
606611

607612
expect(occurrences.map(&:first).uniq).to match_array(
608-
%w[events.descriptions.scope_string events.titles.scope_array events.descriptions.scope_array_symbol events.descriptions.scope_array_words]
613+
%w[
614+
events.descriptions.scope_string
615+
events.titles.scope_array
616+
events.descriptions.scope_array_symbol
617+
events.descriptions.scope_array_words
618+
]
609619
)
610620
end
611621
end

0 commit comments

Comments
 (0)