Skip to content

Commit

Permalink
Non-AST: instance variables in t(@v, scope: :lit)
Browse files Browse the repository at this point in the history
Fixes #256
  • Loading branch information
glebm committed Aug 13, 2017
1 parent 30ba37a commit 1d2c6d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 4 additions & 12 deletions lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def match_to_key(match, path, location)

# parse expressions with literals and variable
def first_argument_re
/(?: (?: #{literal_re} ) | #{variable_re} )/x
/(?: (?: #{literal_re} ) | #{expr_re} )/x
end

# strip literals, convert expressions to #{interpolations}
def strip_literal(val)
if val =~ /\A\w/
if val =~ /\A[\w@]/
"\#{#{val}}"
else
super(val)
Expand All @@ -54,17 +54,9 @@ def scope_arg_re
) (\[[^\n)%#]*\]|[^\n)%#,]*)/x
end

# match code expression
# matches characters until , as a heuristic to parse scopes like [:categories, cat.key]
# can be massively improved by matching parenthesis
# match a limited subset of code expressions (no parenthesis, commas, etc)
def expr_re
/[\w():"'.\s]+/x
end

# match variable key
# e.g: t(key, scope: 'scope') => t('scope.#{key}')
def variable_re
/\w+/
/[\w@.&|\s?!]+/
end

# extract literal or array of literals
Expand Down
8 changes: 8 additions & 0 deletions spec/pattern_with_scope_scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def stub_source(scanner, source)
expect(scanner.keys.map(&:key)).to eq(['scope.#{key}'])
end

it 'matches a literal scope with an instance variable' do
stub_source scanner, '= t @key, scope: "scope"'
expect(scanner.keys.map(&:key)).to eq(['scope.#{@key}'])

stub_source scanner, '= t @key.m, scope: "scope"'
expect(scanner.keys.map(&:key)).to eq(['scope.#{@key.m}'])
end

it 'matches an array of literals scope' do
stub_source scanner, '= t :key, :scope => [:a, :b]'
expect(scanner.keys.map(&:key)).to eq(['a.b.key'])
Expand Down

0 comments on commit 1d2c6d0

Please sign in to comment.