Report string literals in AsciiIdentifiers rule

This commit is contained in:
Sijawusz Pur Rahnama 2023-11-14 05:02:10 +01:00
parent 775650c882
commit be76b3682a
2 changed files with 55 additions and 2 deletions

View file

@ -49,6 +49,14 @@ module Ameba::Rule::Naming
CRYSTAL
end
it "reports defs with parameter default values containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
def forest_adventure(animal_type = :🐺)
# ^^ error: Identifier contains non-ascii characters
end
CRYSTAL
end
it "reports argument names containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
%w[wensleydale cheddar brie].each { |🧀| nil }
@ -56,6 +64,20 @@ module Ameba::Rule::Naming
CRYSTAL
end
it "reports calls with arguments containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
%i[🐺 🐿].index!(:🐺)
# ^^ error: Identifier contains non-ascii characters
CRYSTAL
end
it "reports calls with named arguments containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
%i[🐺 🐿].index!(obj: :🐺)
# ^^ error: Identifier contains non-ascii characters
CRYSTAL
end
it "reports aliases with names containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
alias JSON🧀 = JSON::Any
@ -84,6 +106,20 @@ module Ameba::Rule::Naming
CRYSTAL
end
it "reports assignments with symbol literals containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
foo = :
# ^^^ error: Identifier contains non-ascii characters
CRYSTAL
end
it "reports multiple assignments with symbol literals containing non-ascii characters" do
expect_issue subject, <<-CRYSTAL
foo, bar = :, true
# ^^^ error: Identifier contains non-ascii characters
CRYSTAL
end
it "passes for strings with non-ascii characters" do
expect_no_issues subject, <<-CRYSTAL
space = "👾"

View file

@ -32,11 +32,21 @@ module Ameba::Rule::Naming
if (target = node.target).is_a?(Crystal::Path)
check_issue(source, target, target)
end
check_symbol_literal(source, node.value)
end
def test(source, node : Crystal::MultiAssign)
node.targets.each do |target|
check_issue(source, target, target)
node.values.each do |value|
check_symbol_literal(source, value)
end
end
def test(source, node : Crystal::Call)
node.args.each do |arg|
check_symbol_literal(source, arg)
end
node.named_args.try &.each do |arg|
check_symbol_literal(source, arg.value)
end
end
@ -45,6 +55,7 @@ module Ameba::Rule::Naming
node.args.each do |arg|
check_issue(source, arg, prefer_name_location: true)
check_symbol_literal(source, arg.default_value)
end
end
@ -56,6 +67,12 @@ module Ameba::Rule::Naming
check_issue(source, node.name, node.name)
end
private def check_symbol_literal(source, node)
if node.is_a?(Crystal::SymbolLiteral)
check_issue(source, node, node.value)
end
end
private def check_issue(source, location, end_location, name)
issue_for location, end_location, MSG unless name.to_s.ascii_only?
end