class RuboCop::Cop::Style::AsciiComments
# Translates from English to Japanese
# good
# Translates from English to 日本語。
# bad
@example
AllowedChars attribute (empty by default).
in comments. You could set an array of allowed non-ascii chars in
This cop checks for non-ascii (non-English) characters
def allowed_non_ascii_chars
def allowed_non_ascii_chars cop_config['AllowedChars'] || [] end
def first_non_ascii_chars(string)
def first_non_ascii_chars(string) string.match(/[^[:ascii:]]+/).to_s end
def first_offense_range(comment)
def first_offense_range(comment) expression = comment.loc.expression first_offense = first_non_ascii_chars(comment.text) start_position = expression.begin_pos + comment.text.index(first_offense) end_position = start_position + first_offense.length range_between(start_position, end_position) end
def investigate(processed_source)
def investigate(processed_source) processed_source.comments.each do |comment| next if comment.text.ascii_only? next if only_allowed_non_ascii_chars?(comment.text) add_offense(comment, location: first_offense_range(comment)) end end
def only_allowed_non_ascii_chars?(string)
def only_allowed_non_ascii_chars?(string) non_ascii = string.scan(/[^[:ascii:]]/) (non_ascii - allowed_non_ascii_chars).empty? end