mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	New rule: trailing whitespace
This commit is contained in:
		
							parent
							
								
									f7fc34db19
								
							
						
					
					
						commit
						7d3d0902e5
					
				
					 4 changed files with 41 additions and 3 deletions
				
			
		
							
								
								
									
										27
									
								
								spec/ameba/rules/trailing_whitespace_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								spec/ameba/rules/trailing_whitespace_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,27 @@
 | 
				
			||||||
 | 
					require "../../spec_helper"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module Ameba::Rules
 | 
				
			||||||
 | 
					  subject = TrailingWhitespace.new
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  describe TrailingWhitespace do
 | 
				
			||||||
 | 
					    it "passes if all lines do not have trailing whitespace" do
 | 
				
			||||||
 | 
					      source = Source.new "", "no-whispace"
 | 
				
			||||||
 | 
					      subject.catch(source).valid?.should be_true
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "fails if there is a line with trailing whitespace" do
 | 
				
			||||||
 | 
					      source = Source.new "", "whitespace at the end "
 | 
				
			||||||
 | 
					      subject.catch(source).valid?.should be_false
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "reports rule, pos and message" do
 | 
				
			||||||
 | 
					      source = Source.new "", "a = 1\n b = 2 "
 | 
				
			||||||
 | 
					      subject.catch(source).valid?.should be_false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      error = source.errors.first
 | 
				
			||||||
 | 
					      error.rule.should_not be_nil
 | 
				
			||||||
 | 
					      error.pos.should eq 2
 | 
				
			||||||
 | 
					      error.message.should eq "Trailing whitespace detected"
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
module Ameba
 | 
					module Ameba
 | 
				
			||||||
  RULES = [
 | 
					  RULES = [
 | 
				
			||||||
    Rules::LineLength,
 | 
					    Rules::LineLength,
 | 
				
			||||||
 | 
					    Rules::TrailingWhitespace,
 | 
				
			||||||
  ]
 | 
					  ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  abstract struct Rule
 | 
					  abstract struct Rule
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,10 +6,9 @@ module Ameba::Rules
 | 
				
			||||||
  struct LineLength < Rule
 | 
					  struct LineLength < Rule
 | 
				
			||||||
    def test(source)
 | 
					    def test(source)
 | 
				
			||||||
      source.lines.each_with_index do |line, index|
 | 
					      source.lines.each_with_index do |line, index|
 | 
				
			||||||
        if line.size > 79
 | 
					        next unless line.size > 79
 | 
				
			||||||
        source.error self, index + 1, "Line too long (#{line.size} symbols)"
 | 
					        source.error self, index + 1, "Line too long (#{line.size} symbols)"
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
end
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										11
									
								
								src/ameba/rules/trailing_whitespace.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/ameba/rules/trailing_whitespace.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,11 @@
 | 
				
			||||||
 | 
					module Ameba::Rules
 | 
				
			||||||
 | 
					  # A rule that disallows trailing whitespace at the end of a line.
 | 
				
			||||||
 | 
					  struct TrailingWhitespace < Rule
 | 
				
			||||||
 | 
					    def test(source)
 | 
				
			||||||
 | 
					      source.lines.each_with_index do |line, index|
 | 
				
			||||||
 | 
					        next unless line =~ /\s$/
 | 
				
			||||||
 | 
					        source.error self, index + 1, "Trailing whitespace detected"
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue