From 8440747353d0f41184de9b49278c423364d1d703 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sat, 4 Nov 2017 23:13:38 +0200 Subject: [PATCH] New rule: variable names --- spec/ameba/rules/variable_names_spec.cr | 61 +++++++++++++++++++++++++ src/ameba/ast/traverse.cr | 3 ++ src/ameba/rules/variable_names.cr | 55 ++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 spec/ameba/rules/variable_names_spec.cr create mode 100644 src/ameba/rules/variable_names.cr diff --git a/spec/ameba/rules/variable_names_spec.cr b/spec/ameba/rules/variable_names_spec.cr new file mode 100644 index 00000000..9b9fc45c --- /dev/null +++ b/spec/ameba/rules/variable_names_spec.cr @@ -0,0 +1,61 @@ +require "../../spec_helper" + +module Ameba + subject = Rules::VariableNames.new + + private def it_reports_var_name(content, expected) + it "reports method name #{expected}" do + s = Source.new content + Rules::VariableNames.new.catch(s).should_not be_valid + s.errors.first.message.should contain expected + end + end + + describe Rules::VariableNames do + it "passes if var names are underscore-cased" do + s = Source.new %( + class Greeting + @@default_greeting = "Hello world" + + def initialize(@custom_greeting = nil) + end + + def print_greeting + greeting = @custom_greeting || @@default_greeting + puts greeting + end + end + ) + subject.catch(s).should be_valid + end + + it_reports_var_name %(myBadNamedVar = 1), "my_bad_named_var" + it_reports_var_name %(wrong_Name = 'y'), "wrong_name" + + it_reports_var_name %( + class Greeting + def initialize(@badNamed = nil) + end + end + ), "bad_named" + + it_reports_var_name %( + class Greeting + @@defaultGreeting = "Hello world" + end + ), "default_greeting" + + it "reports rule, pos and message" do + s = Source.new %( + badName = "Yeah" + ) + subject.catch(s).should_not be_valid + error = s.errors.first + error.rule.should_not be_nil + error.pos.should eq 2 + error.message.should eq( + "Var name should be underscore-cased: bad_name, not badName" + ) + end + end +end diff --git a/src/ameba/ast/traverse.cr b/src/ameba/ast/traverse.cr index f711f3db..b414e5db 100644 --- a/src/ameba/ast/traverse.cr +++ b/src/ameba/ast/traverse.cr @@ -6,13 +6,16 @@ module Ameba::AST Call, Case, ClassDef, + ClassVar, Def, EnumDef, If, + InstanceVar, LibDef, ModuleDef, StringInterpolation, Unless, + Var, ] abstract class Visitor < Crystal::Visitor diff --git a/src/ameba/rules/variable_names.cr b/src/ameba/rules/variable_names.cr new file mode 100644 index 00000000..f4e095cc --- /dev/null +++ b/src/ameba/rules/variable_names.cr @@ -0,0 +1,55 @@ +module Ameba::Rules + # A rule that enforces variable names to be in underscored case. + # + # For example, these variable names are considered valid: + # + # ``` + # class Greeting + # @@default_greeting = "Hello world" + # + # def initialize(@custom_greeting = nil) + # end + # + # def print_greeting + # greeting = @custom_greeting || @@default_greeting + # puts greeting + # end + # end + # ``` + # + # And these are invalid method names: + # + # ``` + # myBadNamedVar = 1 + # wrong_Name = 2 + # ``` + # + struct VariableNames < Rule + def test(source) + [ + AST::VarVisitor, + AST::InstanceVarVisitor, + AST::ClassVarVisitor, + ].each &.new(self, source) + end + + private def check_node(source, node) + return if (expected = node.name.underscore) == node.name + + source.error self, node.location.try &.line_number, + "Var name should be underscore-cased: #{expected}, not #{node.name}" + end + + def test(source, node : Crystal::Var) + check_node source, node + end + + def test(source, node : Crystal::InstanceVar) + check_node source, node + end + + def test(source, node : Crystal::ClassVar) + check_node source, node + end + end +end