mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Merge pull request #443 from crystal-ameba/fix-issue-442
Do not report type declarations within `lib` definitions
This commit is contained in:
		
						commit
						734bb2a7f1
					
				
					 4 changed files with 43 additions and 14 deletions
				
			
		|  | @ -180,6 +180,28 @@ module Ameba::AST | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |   describe "#def?" do | ||||||
|  |     context "when check_outer_scopes: true" do | ||||||
|  |       it "returns true if outer scope is Crystal::Def" do | ||||||
|  |         nodes = as_nodes("def foo; 3.times {}; end") | ||||||
|  |         outer_scope = Scope.new nodes.def_nodes.first | ||||||
|  |         scope = Scope.new nodes.block_nodes.first, outer_scope | ||||||
|  |         scope.def?(check_outer_scopes: true).should be_true | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     it "returns true if Crystal::Def" do | ||||||
|  |       nodes = as_nodes("def foo; end") | ||||||
|  |       scope = Scope.new nodes.def_nodes.first | ||||||
|  |       scope.def?.should be_true | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     it "returns false otherwise" do | ||||||
|  |       scope = Scope.new as_node("a = 1") | ||||||
|  |       scope.def?.should be_false | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   describe "#in_macro?" do |   describe "#in_macro?" do | ||||||
|     it "returns true if Crystal::Macro" do |     it "returns true if Crystal::Macro" do | ||||||
|       nodes = as_nodes <<-CRYSTAL |       nodes = as_nodes <<-CRYSTAL | ||||||
|  |  | ||||||
|  | @ -998,6 +998,16 @@ module Ameba::Rule::Lint | ||||||
|           CRYSTAL |           CRYSTAL | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|  |       it "doesn't report if it's referenced in a lib" do | ||||||
|  |         expect_no_issues subject, <<-CRYSTAL | ||||||
|  |           lib LibFoo | ||||||
|  |             struct Foo | ||||||
|  |               a : Int32 | ||||||
|  |             end | ||||||
|  |           end | ||||||
|  |           CRYSTAL | ||||||
|  |       end | ||||||
|  | 
 | ||||||
|       it "doesn't report if it's referenced" do |       it "doesn't report if it's referenced" do | ||||||
|         expect_no_issues subject, <<-CRYSTAL |         expect_no_issues subject, <<-CRYSTAL | ||||||
|           def foo |           def foo | ||||||
|  |  | ||||||
|  | @ -180,20 +180,15 @@ module Ameba::AST | ||||||
|       @visibility || outer_scope.try(&.visibility) |       @visibility || outer_scope.try(&.visibility) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     # Returns `true` if current scope is a def, `false` otherwise. |     {% for type in %w[Def ClassDef ModuleDef EnumDef LibDef FunDef].map(&.id) %} | ||||||
|     def def? |       {% method_name = type.underscore %} | ||||||
|       node.is_a?(Crystal::Def) |       # Returns `true` if current scope is a {{ method_name[0..-5] }} def, `false` otherwise. | ||||||
|     end |       def {{ method_name }}?(*, check_outer_scopes = false) | ||||||
| 
 |         node.is_a?(Crystal::{{ type }}) || | ||||||
|     # Returns `true` if current scope is a class, `false` otherwise. |           !!(check_outer_scopes && | ||||||
|     def class_def? |             outer_scope.try(&.{{ method_name }}?(check_outer_scopes: true))) | ||||||
|       node.is_a?(Crystal::ClassDef) |       end | ||||||
|     end |     {% end %} | ||||||
| 
 |  | ||||||
|     # Returns `true` if current scope is a module, `false` otherwise. |  | ||||||
|     def module_def? |  | ||||||
|       node.is_a?(Crystal::ModuleDef) |  | ||||||
|     end |  | ||||||
| 
 | 
 | ||||||
|     # Returns `true` if this scope is a top level scope, `false` otherwise. |     # Returns `true` if this scope is a top level scope, `false` otherwise. | ||||||
|     def top_level? |     def top_level? | ||||||
|  |  | ||||||
|  | @ -39,6 +39,8 @@ module Ameba::Rule::Lint | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def test(source, node, scope : AST::Scope) |     def test(source, node, scope : AST::Scope) | ||||||
|  |       return if scope.lib_def?(check_outer_scopes: true) | ||||||
|  | 
 | ||||||
|       scope.variables.each do |var| |       scope.variables.each do |var| | ||||||
|         next if var.ignored? || var.used_in_macro? || var.captured_by_block? |         next if var.ignored? || var.used_in_macro? || var.captured_by_block? | ||||||
|         next if exclude_type_declarations? && scope.assigns_type_dec?(var.name) |         next if exclude_type_declarations? && scope.assigns_type_dec?(var.name) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue