Allow constants to be in PascalCase (i.e. Log, SuperConstant)

closes #148
This commit is contained in:
Vitalii Elenhaupt 2020-04-13 09:38:05 +03:00
parent 1a25583036
commit 478da94c20
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
2 changed files with 11 additions and 7 deletions

View file

@ -24,6 +24,8 @@ module Ameba
def works(n : Int32) def works(n : Int32)
end end
Log = ::Log.for("db")
a = 1 a = 1
myVar = 2 myVar = 2
m_var = 3 m_var = 3
@ -31,21 +33,21 @@ module Ameba
subject.catch(s).should be_valid subject.catch(s).should be_valid
end end
it_reports_constant "MyBadConstant=1", "MYBADCONSTANT" # it_reports_constant "MyBadConstant=1", "MYBADCONSTANT"
it_reports_constant "Wrong_NAME=2", "WRONG_NAME" it_reports_constant "Wrong_NAME=2", "WRONG_NAME"
it_reports_constant "Wrong_Name=3", "WRONG_NAME" it_reports_constant "Wrong_Name=3", "WRONG_NAME"
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new %( s = Source.new %(
Const = 1 Const_Name = 1
), "source.cr" ), "source.cr"
subject.catch(s).should_not be_valid subject.catch(s).should_not be_valid
issue = s.issues.first issue = s.issues.first
issue.rule.should_not be_nil issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1" issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:5" issue.end_location.to_s.should eq "source.cr:1:10"
issue.message.should eq( issue.message.should eq(
"Constant name should be screaming-cased: CONST, not Const" "Constant name should be screaming-cased: CONST_NAME, not Const_Name"
) )
end end
end end

View file

@ -11,8 +11,8 @@ module Ameba::Rule::Style
# And these are invalid names: # And these are invalid names:
# #
# ``` # ```
# MyBadConstant = 1 # myBadConstant = 1
# Wrong_NAME = 2 # Wrong_NAME = 2
# ``` # ```
# #
# YAML configuration example: # YAML configuration example:
@ -32,7 +32,9 @@ module Ameba::Rule::Style
def test(source, node : Crystal::Assign) def test(source, node : Crystal::Assign)
if (target = node.target).is_a? Crystal::Path if (target = node.target).is_a? Crystal::Path
name = target.names.first name = target.names.first
return if (expected = name.upcase) == name expected = name.upcase
return if expected == name || name.camelcase == name
issue_for target, MSG % {expected, name} issue_for target, MSG % {expected, name}
end end