diff --git a/spec/ameba/inline_comments_spec.cr b/spec/ameba/inline_comments_spec.cr index 593a6fc2..3f3b2e18 100644 --- a/spec/ameba/inline_comments_spec.cr +++ b/spec/ameba/inline_comments_spec.cr @@ -6,17 +6,22 @@ module Ameba subject = InlineComments::COMMENT_DIRECTIVE_REGEX it "allows to parse action and rule name" do - result = subject.match("#ameba:enable Group/RuleName") - result.should_not be_nil - result.not_nil![1].should eq "enable" - result.not_nil![2].should eq "Group/RuleName" + result = subject.match("# ameba:enable Group/RuleName") + result = result.should_not be_nil + result["action"].should eq "enable" + result["rules"].should eq "Group/RuleName" end - it "ignores the repeatable spaces" do + it "parses multiple rules" do + result = subject.match("# ameba:enable Group/RuleName, OtherRule, Foo/Bar") + result = result.should_not be_nil + result["action"].should eq "enable" + result["rules"].should eq "Group/RuleName, OtherRule, Foo/Bar" + end + + it "fails to parse directives with spaces" do result = subject.match("# ameba : enable Group/RuleName") - result.should_not be_nil - result.not_nil![1].should eq "enable" - result.not_nil![2].should eq "Group/RuleName" + result.should be_nil end end diff --git a/spec/ameba/rule/lint/bad_directive_spec.cr b/spec/ameba/rule/lint/bad_directive_spec.cr index 74b7891d..5667982d 100644 --- a/spec/ameba/rule/lint/bad_directive_spec.cr +++ b/spec/ameba/rule/lint/bad_directive_spec.cr @@ -28,7 +28,7 @@ module Ameba::Rule::Lint it "reports if there are incorrect rule names" do s = Source.new %( - # ameba:enable BadRule1,BadRule2 + # ameba:enable BadRule1, BadRule2 ), "source.cr" subject.catch(s).should_not be_valid s.issues.size.should eq 1 diff --git a/src/ameba/inline_comments.cr b/src/ameba/inline_comments.cr index 998e75c7..cece57fe 100644 --- a/src/ameba/inline_comments.cr +++ b/src/ameba/inline_comments.cr @@ -1,7 +1,7 @@ module Ameba # A module that utilizes inline comments parsing and processing logic. module InlineComments - COMMENT_DIRECTIVE_REGEX = Regex.new "# ameba : (\\w+) ([\\w/, ]*)".gsub(" ", "\\s*") + COMMENT_DIRECTIVE_REGEX = /# ameba:(?\w+) (?\w+(?:\/\w+)?(?:,? \w+(?:\/\w+)?)*)/ # Available actions in the inline comments enum Action @@ -68,10 +68,10 @@ module Ameba # def parse_inline_directive(line) if directive = COMMENT_DIRECTIVE_REGEX.match(line) - return if commented_out?(line.gsub directive[0], "") + return if commented_out?(line.gsub(directive[0], "")) { - action: directive[1], - rules: directive[2].split(/[\s,]/, remove_empty: true), + action: directive["action"], + rules: directive["rules"].split(/[\s,]/, remove_empty: true), } end end @@ -83,8 +83,8 @@ module Ameba end end - private def comment?(line) - return true if line.lstrip.starts_with? '#' + private def comment?(line : String) + line.lstrip.starts_with? '#' end private def line_disabled?(line, rule) @@ -96,7 +96,7 @@ module Ameba private def commented_out?(line) commented = false - lexer = Crystal::Lexer.new(line).tap { |l| l.comments_enabled = true } + lexer = Crystal::Lexer.new(line).tap(&.comments_enabled = true) Tokenizer.new(lexer).run { |t| commented = true if t.type == :COMMENT } commented end diff --git a/src/ameba/rule/lint/bad_directive.cr b/src/ameba/rule/lint/bad_directive.cr index 882c6b7d..76b6d358 100644 --- a/src/ameba/rule/lint/bad_directive.cr +++ b/src/ameba/rule/lint/bad_directive.cr @@ -48,7 +48,7 @@ module Ameba::Rule::Lint private def check_rules(source, token, rules) bad_names = rules - ALL_RULE_NAMES - ALL_GROUP_NAMES - issue_for token, "Such rules do not exist: %s" % bad_names.join(", ") if bad_names.any? + issue_for token, "Such rules do not exist: %s" % bad_names.join(", ") unless bad_names.empty? end end end