lib/i18n/tasks/scanners/pattern_scanner.rb in i18n-tasks-0.9.4 vs lib/i18n/tasks/scanners/pattern_scanner.rb in i18n-tasks-0.9.5
- old
+ new
@@ -1,15 +1,17 @@
# frozen_string_literal: true
require 'i18n/tasks/scanners/file_scanner'
require 'i18n/tasks/scanners/relative_keys'
require 'i18n/tasks/scanners/occurrence_from_position'
+require 'i18n/tasks/scanners/ruby_key_literals'
module I18n::Tasks::Scanners
# Scan for I18n.t usages using a simple regular expression.
class PatternScanner < FileScanner
include RelativeKeys
include OccurrenceFromPosition
+ include RubyKeyLiterals
def initialize(**args)
super
@pattern = config[:pattern].present? ? Regexp.new(config[:pattern]) : default_pattern
@ignore_lines_res = (config[:ignore_lines] || []).inject({}) { |h, (ext, re)| h.update(ext => Regexp.new(re)) }
@@ -48,30 +50,17 @@
def exclude_line?(line, path)
re = @ignore_lines_res[File.extname(path)[1..-1]]
re && re =~ line
end
- # remove the leading colon and unwrap quotes from the key match
- # @param literal [String] e.g: "key", 'key', or :key.
- # @return [String] key
- def strip_literal(literal)
- key = literal
- key = key[1..-1] if ':'.freeze == key[0]
- key = key[1..-2] if QUOTES.include?(key[0])
- key
- end
+ VALID_KEY_RE_DYNAMIC = /^(#{VALID_KEY_CHARS}|[:\#{@}\[\]])+$/
- QUOTES = ["'".freeze, '"'.freeze].freeze
- VALID_KEY_CHARS = /(?:[[:word:]]|[-.?!;À-ž])/
- VALID_KEY_RE_STRICT = /^#{VALID_KEY_CHARS}+$/
- VALID_KEY_RE = /^(#{VALID_KEY_CHARS}|[:\#{@}\[\]])+$/
-
def valid_key?(key)
if @config[:strict]
- key =~ VALID_KEY_RE_STRICT && !key.end_with?('.')
+ super(key)
else
- key =~ VALID_KEY_RE
+ key =~ VALID_KEY_RE_DYNAMIC
end
end
def key_relative_to_method?(path)
/controllers|mailers/ =~ path
@@ -83,16 +72,9 @@
method && method.strip.sub(/^def\s*/, '').sub(/[\(\s;].*$/, '')
end
def translate_call_re
/(?<=^|[^\w'\-.]|[^\w'\-]I18n\.|I18n\.)t(?:ranslate)?/
- end
-
- # Match literals:
- # * String: '', "#{}"
- # * Symbol: :sym, :'', :"#{}"
- def literal_re
- /:?".+?"|:?'.+?'|:\w+/
end
def default_pattern
# capture only the first argument
/