Skip to content

Commit

Permalink
Allow constants to be in PascalCase (i.e. Log, SuperConstant)
Browse files Browse the repository at this point in the history
closes #148
  • Loading branch information
veelenga committed Apr 13, 2020
1 parent 1a25583 commit 478da94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions spec/ameba/rule/style/constant_names_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,30 @@ module Ameba
def works(n : Int32)
end
Log = ::Log.for("db")
a = 1
myVar = 2
m_var = 3
)
subject.catch(s).should be_valid
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=3", "WRONG_NAME"

it "reports rule, pos and message" do
s = Source.new %(
Const = 1
Const_Name = 1
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
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(
"Constant name should be screaming-cased: CONST, not Const"
"Constant name should be screaming-cased: CONST_NAME, not Const_Name"
)
end
end
Expand Down
8 changes: 5 additions & 3 deletions src/ameba/rule/style/constant_names.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module Ameba::Rule::Style
# And these are invalid names:
#
# ```
# MyBadConstant = 1
# Wrong_NAME = 2
# myBadConstant = 1
# Wrong_NAME = 2
# ```
#
# YAML configuration example:
Expand All @@ -32,7 +32,9 @@ module Ameba::Rule::Style
def test(source, node : Crystal::Assign)
if (target = node.target).is_a? Crystal::Path
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}
end
Expand Down

0 comments on commit 478da94

Please sign in to comment.