class I18n::Tasks::Scanners::PatternWithScopeScanner
Scans for I18n.t(key, scope: …) usages both scope: “literal”, and scope: [:array, :of, ‘literals’] forms are supported Caveat: scope is only detected when it is the first argument
Protected Instance Methods
default_pattern()
click to toggle source
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 12 def default_pattern # capture the first argument and scope argument if present /#{super} (?: \s*,\s* #{scope_arg_re} )? (?# capture scope in second argument ) /x end
expr_re()
click to toggle source
match a limited subset of code expressions (no parenthesis, commas, etc)
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 59 def expr_re /[\w@.&|\s?!]+/ end
extract_literal_or_array_of_literals(s)
click to toggle source
extract literal or array of literals returns nil on any other input rubocop:disable Metrics/MethodLength,Metrics/PerceivedComplexity
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 66 def extract_literal_or_array_of_literals(s) literals = [] braces_stack = [] acc = [] consume_literal = proc do acc_str = acc.join if acc_str =~ literal_re literals << strip_literal(acc_str) acc = [] else return nil end end s.each_char.with_index do |c, i| if c == '[' return nil unless braces_stack.empty? braces_stack.push(i) elsif c == ']' break elsif c == ',' consume_literal.call break if braces_stack.empty? elsif c =~ VALID_KEY_CHARS || /['":]/ =~ c acc << c elsif c != ' ' return nil end end consume_literal.call unless acc.empty? literals end
first_argument_re()
click to toggle source
parse expressions with literals and variable
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 37 def first_argument_re /(?: (?: #{literal_re} ) | #{expr_re} )/x end
match_to_key(match, path, location)
click to toggle source
Given @param [MatchData] match @param [String] path @return [String] full absolute key name with scope resolved if any
Calls superclass method
I18n::Tasks::Scanners::PatternScanner#match_to_key
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 23 def match_to_key(match, path, location) key = super scope = match[1] if scope scope_parts = extract_literal_or_array_of_literals(scope) return nil if scope_parts.nil? || scope_parts.empty? "#{scope_parts.join('.')}.#{key}" else key unless match[0] =~ /\A\w/ end end
scope_arg_re()
click to toggle source
scope: literal or code expression or an array of these
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 51 def scope_arg_re /(?: :scope\s*=>\s* | (?# :scope => :home ) scope:\s* (?# scope: :home ) ) (\[[^\n)%#]*\]|[^\n)%#,]*)/x end
strip_literal(val)
click to toggle source
strip literals, convert expressions to #{interpolations}
Calls superclass method
I18n::Tasks::Scanners::RubyKeyLiterals#strip_literal
# File lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb, line 42 def strip_literal(val) if val =~ /\A[\w@]/ "\#{#{val}}" else super(val) end end