class RuboCop::Cop::Naming::AsciiIdentifiers
FOÖ = “foo”
end
class Foö
# good
@example AsciiConstants: false
FOÖ = “foo”
end
class Foö
# bad
@example AsciiConstants: true (default)
params[:width_gteq]
# good<br><br>params # Arabic character (non-ascii)
# bad
height = 10
# good
신장 = 10 # Hangul character (non-ascii)
# bad
end
def say_hello
# good
end
def hello_🍣 # Emoji (non-ascii)
# bad
end
def こんにちはと言う # Japanese character (non-ascii)
# bad
end
def καλημερα # Greek alphabet (non-ascii)
# bad
@example
can be controlled using AsciiConstants config.
Identifiers are always checked and whether constants are checked
Checks for non-ascii characters in identifier and constant names.
def first_non_ascii_chars(string)
def first_non_ascii_chars(string) string.match(/[^[:ascii:]]+/).to_s end
def first_offense_range(identifier)
def first_offense_range(identifier) expression = identifier.pos first_offense = first_non_ascii_chars(identifier.text) start_position = expression.begin_pos + identifier.text.index(first_offense) end_position = start_position + first_offense.length range_between(start_position, end_position) end
def on_new_investigation
def on_new_investigation processed_source.tokens.each do |token| next if !should_check?(token) || token.text.ascii_only? message = token.type == :tIDENTIFIER ? IDENTIFIER_MSG : CONSTANT_MSG add_offense(first_offense_range(token), message: message) end end
def should_check?(token)
def should_check?(token) token.type == :tIDENTIFIER || (token.type == :tCONSTANT && cop_config['AsciiConstants']) end