mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Rule dsl
This commit is contained in:
		
							parent
							
								
									7d3d0902e5
								
							
						
					
					
						commit
						c8dcddea22
					
				
					 3 changed files with 22 additions and 21 deletions
				
			
		| 
						 | 
					@ -4,6 +4,16 @@ module Ameba
 | 
				
			||||||
    Rules::TrailingWhitespace,
 | 
					    Rules::TrailingWhitespace,
 | 
				
			||||||
  ]
 | 
					  ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  macro rule(name, &block)
 | 
				
			||||||
 | 
					    module Ameba::Rules
 | 
				
			||||||
 | 
					      struct {{name.id}} < Rule
 | 
				
			||||||
 | 
					        def test(source)
 | 
				
			||||||
 | 
					          {{block.body}}
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  abstract struct Rule
 | 
					  abstract struct Rule
 | 
				
			||||||
    abstract def test(source : Source)
 | 
					    abstract def test(source : Source)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,14 +1,8 @@
 | 
				
			||||||
module Ameba::Rules
 | 
					# A rule that disallows lines longer than 79 symbols.
 | 
				
			||||||
  # A rule that checks the size of lines in sources.
 | 
					
 | 
				
			||||||
  #
 | 
					Ameba.rule LineLength do |source|
 | 
				
			||||||
  # This rule will report an error if there is at least
 | 
					 | 
				
			||||||
  # one line longer than 79 symbols.
 | 
					 | 
				
			||||||
  struct LineLength < Rule
 | 
					 | 
				
			||||||
    def test(source)
 | 
					 | 
				
			||||||
  source.lines.each_with_index do |line, index|
 | 
					  source.lines.each_with_index do |line, index|
 | 
				
			||||||
    next unless 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
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,8 @@
 | 
				
			||||||
module Ameba::Rules
 | 
					 | 
				
			||||||
# A rule that disallows trailing whitespace at the end of a line.
 | 
					# A rule that disallows trailing whitespace at the end of a line.
 | 
				
			||||||
  struct TrailingWhitespace < Rule
 | 
					
 | 
				
			||||||
    def test(source)
 | 
					Ameba.rule TrailingWhitespace do |source|
 | 
				
			||||||
  source.lines.each_with_index do |line, index|
 | 
					  source.lines.each_with_index do |line, index|
 | 
				
			||||||
    next unless line =~ /\s$/
 | 
					    next unless line =~ /\s$/
 | 
				
			||||||
    source.error self, index + 1, "Trailing whitespace detected"
 | 
					    source.error self, index + 1, "Trailing whitespace detected"
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue