Merge pull request #424 from crystal-ameba/report-string-literals-in-ascii-identifiers-rule

Report symbol literals in `Naming/AsciiIdentifiers` rule
This commit is contained in:
Sijawusz Pur Rahnama 2023-11-14 10:24:29 +01:00 committed by GitHub
commit 3b87aa6490
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 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,11 +106,46 @@ 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 = "👾"
space = :invader # 👾
CRYSTAL
end
context "properties" do
context "#ignore_symbols" do
it "returns `false` by default" do
rule = AsciiIdentifiers.new
rule.ignore_symbols?.should be_false
end
it "stops reporting symbol literals if set to `true`" do
rule = AsciiIdentifiers.new
rule.ignore_symbols = true
expect_no_issues rule, <<-CRYSTAL
def forest_adventure(animal_type = :🐺); end
%i[🐺 🐿].index!(:🐺)
foo, bar = :, true
foo = :
CRYSTAL
end
end
end
end
end

View file

@ -32,6 +32,7 @@ module Ameba::AST
IsA,
LibDef,
ModuleDef,
MultiAssign,
NilLiteral,
StringInterpolation,
Unless,

View file

@ -20,10 +20,12 @@ module Ameba::Rule::Naming
# ```
# Naming/AsciiIdentifiers:
# Enabled: true
# IgnoreSymbols: false
# ```
class AsciiIdentifiers < Base
properties do
description "Disallows non-ascii characters in identifiers"
ignore_symbols false
end
MSG = "Identifier contains non-ascii characters"
@ -32,11 +34,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 +57,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 +69,13 @@ module Ameba::Rule::Naming
check_issue(source, node.name, node.name)
end
private def check_symbol_literal(source, node)
return if ignore_symbols?
return unless node.is_a?(Crystal::SymbolLiteral)
check_issue(source, node, node.value)
end
private def check_issue(source, location, end_location, name)
issue_for location, end_location, MSG unless name.to_s.ascii_only?
end