mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Make InlineComments::COMMENT_DIRECTIVE_REGEX more strict (#146)
This commit is contained in:
parent
458c492730
commit
1a25583036
4 changed files with 22 additions and 17 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue