Skip to content

Commit

Permalink
Make InlineComments::COMMENT_DIRECTIVE_REGEX more strict (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored Apr 11, 2020
1 parent 458c492 commit 1a25583
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
21 changes: 13 additions & 8 deletions spec/ameba/inline_comments_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/rule/lint/bad_directive_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/ameba/inline_comments.cr
Original file line number Diff line number Diff line change
@@ -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:(?<action>\w+) (?<rules>\w+(?:\/\w+)?(?:,? \w+(?:\/\w+)?)*)/

# Available actions in the inline comments
enum Action
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ameba/rule/lint/bad_directive.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1a25583

Please sign in to comment.