mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Syntax rule
This commit is contained in:
		
							parent
							
								
									2f9d31b02d
								
							
						
					
					
						commit
						c9db63bf34
					
				
					 2 changed files with 74 additions and 0 deletions
				
			
		
							
								
								
									
										39
									
								
								spec/ameba/rule/syntax_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								spec/ameba/rule/syntax_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,39 @@
 | 
				
			||||||
 | 
					require "../../spec_helper"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module Ameba::Rule
 | 
				
			||||||
 | 
					  describe Syntax do
 | 
				
			||||||
 | 
					    subject = Syntax.new
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "passes if there is no invalid syntax" do
 | 
				
			||||||
 | 
					      s = Source.new %(
 | 
				
			||||||
 | 
					        def hello
 | 
				
			||||||
 | 
					          puts "totally valid"
 | 
				
			||||||
 | 
					        rescue e: Exception
 | 
				
			||||||
 | 
					          #
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					      subject.catch(s).should be_valid
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "fails if there is an invalid syntax" do
 | 
				
			||||||
 | 
					      s = Source.new %(
 | 
				
			||||||
 | 
					        def hello
 | 
				
			||||||
 | 
					          puts "invalid"
 | 
				
			||||||
 | 
					        rescue Exception => e
 | 
				
			||||||
 | 
					          #
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					      subject.catch(s).should_not be_valid
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it "reports rule, location and message" do
 | 
				
			||||||
 | 
					      s = Source.new "def hello end", "source.cr"
 | 
				
			||||||
 | 
					      subject.catch(s).should_not be_valid
 | 
				
			||||||
 | 
					      error = s.errors.first
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      error.rule.should_not be_nil
 | 
				
			||||||
 | 
					      error.location.to_s.should eq "source.cr:1:11"
 | 
				
			||||||
 | 
					      error.message.should eq "unexpected token: end (expected ';' or newline)"
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
							
								
								
									
										35
									
								
								src/ameba/rule/syntax.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/ameba/rule/syntax.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,35 @@
 | 
				
			||||||
 | 
					module Ameba::Rule
 | 
				
			||||||
 | 
					  # A rule that reports invalid Crystal syntax.
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  # For example, this syntax is invalid:
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  # ```
 | 
				
			||||||
 | 
					  # def hello
 | 
				
			||||||
 | 
					  #   do_something
 | 
				
			||||||
 | 
					  # rescue Exception => e
 | 
				
			||||||
 | 
					  # end
 | 
				
			||||||
 | 
					  # ```
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  # And should be properly written:
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  # ```
 | 
				
			||||||
 | 
					  # def hello
 | 
				
			||||||
 | 
					  #   do_something
 | 
				
			||||||
 | 
					  # rescue e : Exception
 | 
				
			||||||
 | 
					  # end
 | 
				
			||||||
 | 
					  # ```
 | 
				
			||||||
 | 
					  #
 | 
				
			||||||
 | 
					  struct Syntax < Base
 | 
				
			||||||
 | 
					    properties do
 | 
				
			||||||
 | 
					      enabled = true
 | 
				
			||||||
 | 
					      description = "Reports invalid Crystal syntax"
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test(source)
 | 
				
			||||||
 | 
					      source.ast
 | 
				
			||||||
 | 
					    rescue e : Crystal::SyntaxException
 | 
				
			||||||
 | 
					      location = source.location(e.line_number, e.column_number)
 | 
				
			||||||
 | 
					      source.error self, location, e.message.to_s
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue