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
				
			
		| 
						 | 
					@ -7,16 +7,21 @@ module Ameba
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it "allows to parse action and rule name" do
 | 
					      it "allows to parse action and rule name" do
 | 
				
			||||||
        result = subject.match("# ameba:enable Group/RuleName")
 | 
					        result = subject.match("# ameba:enable Group/RuleName")
 | 
				
			||||||
        result.should_not be_nil
 | 
					        result = result.should_not be_nil
 | 
				
			||||||
        result.not_nil![1].should eq "enable"
 | 
					        result["action"].should eq "enable"
 | 
				
			||||||
        result.not_nil![2].should eq "Group/RuleName"
 | 
					        result["rules"].should eq "Group/RuleName"
 | 
				
			||||||
      end
 | 
					      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 = subject.match("# ameba  :  enable     Group/RuleName")
 | 
				
			||||||
        result.should_not be_nil
 | 
					        result.should be_nil
 | 
				
			||||||
        result.not_nil![1].should eq "enable"
 | 
					 | 
				
			||||||
        result.not_nil![2].should eq "Group/RuleName"
 | 
					 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
module Ameba
 | 
					module Ameba
 | 
				
			||||||
  # A module that utilizes inline comments parsing and processing logic.
 | 
					  # A module that utilizes inline comments parsing and processing logic.
 | 
				
			||||||
  module InlineComments
 | 
					  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
 | 
					    # Available actions in the inline comments
 | 
				
			||||||
    enum Action
 | 
					    enum Action
 | 
				
			||||||
| 
						 | 
					@ -68,10 +68,10 @@ module Ameba
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    def parse_inline_directive(line)
 | 
					    def parse_inline_directive(line)
 | 
				
			||||||
      if directive = COMMENT_DIRECTIVE_REGEX.match(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],
 | 
					          action: directive["action"],
 | 
				
			||||||
          rules:  directive[2].split(/[\s,]/, remove_empty: true),
 | 
					          rules:  directive["rules"].split(/[\s,]/, remove_empty: true),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
| 
						 | 
					@ -83,8 +83,8 @@ module Ameba
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private def comment?(line)
 | 
					    private def comment?(line : String)
 | 
				
			||||||
      return true if line.lstrip.starts_with? '#'
 | 
					      line.lstrip.starts_with? '#'
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private def line_disabled?(line, rule)
 | 
					    private def line_disabled?(line, rule)
 | 
				
			||||||
| 
						 | 
					@ -96,7 +96,7 @@ module Ameba
 | 
				
			||||||
    private def commented_out?(line)
 | 
					    private def commented_out?(line)
 | 
				
			||||||
      commented = false
 | 
					      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 }
 | 
					      Tokenizer.new(lexer).run { |t| commented = true if t.type == :COMMENT }
 | 
				
			||||||
      commented
 | 
					      commented
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -48,7 +48,7 @@ module Ameba::Rule::Lint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private def check_rules(source, token, rules)
 | 
					    private def check_rules(source, token, rules)
 | 
				
			||||||
      bad_names = rules - ALL_RULE_NAMES - ALL_GROUP_NAMES
 | 
					      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
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue