Skip to content
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

Allow hash literals in dynamic keys #397

Merged
merged 3 commits into from
Nov 20, 2021
Merged
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
6 changes: 3 additions & 3 deletions lib/i18n/tasks/scanners/ruby_key_literals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module I18n::Tasks::Scanners
module RubyKeyLiterals
LITERAL_RE = /:?".+?"|:?'.+?'|:\w+/.freeze
LITERAL_RE = /:?"[\[]*(?:\[\s*")?.+(?:"\s*\])?"|:?'.+?'|:\w+/.freeze

# Match literals:
# * String: '', "#{}"
# * String: '', "#{}", "#{hash["key"]}"
# * Symbol: :sym, :'', :"#{}"
def literal_re
LITERAL_RE
Expand All @@ -20,7 +20,7 @@ def strip_literal(literal)
literal
end

VALID_KEY_CHARS = /(?:[[:word:]]|[-.?!:;À-ž\/])/.freeze
VALID_KEY_CHARS = /(?:[[:word:]]|[-.?!:;À-ž\/'"\[\]])/.freeze
VALID_KEY_RE = /^#{VALID_KEY_CHARS}+$/.freeze

def valid_key?(key)
Expand Down
68 changes: 66 additions & 2 deletions spec/scanners/ruby_key_literals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,73 @@
Object.new.extend I18n::Tasks::Scanners::RubyKeyLiterals
end

describe '#literal_re' do
subject do
/(#{scanner.literal_re})/x =~ key
Regexp.last_match(1)
end

context 'string' do
context 'single quoted' do
let(:key) { %('some_key') }
it { is_expected.to eq(key) }
end

context 'double quoted' do
context 'var' do
let(:key) { %q("#{some_key}") } # rubocop:disable Lint/InterpolationCheck
it { is_expected.to eq(key) }
end

context 'hash' do
context 'double quoted key' do
let(:key) { %q("#{some_hash["some_key"]}") } # rubocop:disable Lint/InterpolationCheck
it { is_expected.to eq(key) }
end

context 'single quoted key' do
let(:key) { %q("#{some_hash['some_key']}") } # rubocop:disable Lint/InterpolationCheck
it { is_expected.to eq(key) }
end

context 'symbol key' do
let(:key) { %q("#{some_hash[:some_key]}") } # rubocop:disable Lint/InterpolationCheck
it { is_expected.to eq(key) }
end
end
end
end

context 'symbol' do
context 'regular literal' do
let(:key) { %(:some_key) }
it { is_expected.to eq(key) }
end

context 'single quoted' do
let(:key) { %(:'some_key') }
it { is_expected.to eq(key) }
end

context 'double quoted' do
let(:key) { %q(:"#{some_key}") } # rubocop:disable Lint/InterpolationCheck
it { is_expected.to eq(key) }
end
end
end

describe '#valid_key?' do
it 'allows forward slash in key' do
expect(scanner).to be_valid_key('category/product')
subject { scanner }

context 'slash in key' do
let(:key) { 'category/product' }
it { is_expected.to be_valid_key(key) }
end

context 'hash in key' do
let(:key) { 'category/product' }
let(:key) { 'some_hash["some_key"]' }
it { is_expected.to be_valid_key(key) }
end
end
end