mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Refactor ScopeVisitor to ignore accessor macros
				
					
				
			This commit is contained in:
		
							parent
							
								
									9745637cf9
								
							
						
					
					
						commit
						1b661d633d
					
				
					 2 changed files with 30 additions and 13 deletions
				
			
		|  | @ -186,6 +186,16 @@ module Ameba::AST | ||||||
|       node.is_a?(Crystal::Def) |       node.is_a?(Crystal::Def) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     # Returns `true` if current scope is a class, `false` otherwise. | ||||||
|  |     def class_def? | ||||||
|  |       node.is_a?(Crystal::ClassDef) | ||||||
|  |     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? | ||||||
|       outer_scope.nil? || type_definition? |       outer_scope.nil? || type_definition? | ||||||
|  |  | ||||||
|  | @ -170,22 +170,29 @@ module Ameba::AST | ||||||
| 
 | 
 | ||||||
|     # :nodoc: |     # :nodoc: | ||||||
|     def visit(node : Crystal::Call) |     def visit(node : Crystal::Call) | ||||||
|       case |       scope = @current_scope | ||||||
|       when @current_scope.def? |  | ||||||
|         if node.name.in?(SPECIAL_NODE_NAMES) && node.args.empty? |  | ||||||
|           @current_scope.arguments.each do |arg| |  | ||||||
|             variable = arg.variable |  | ||||||
| 
 | 
 | ||||||
|             ref = variable.reference(variable.node, @current_scope) |       case | ||||||
|             ref.explicit = false |       when scope.top_level? && record_macro?(node)         then return false | ||||||
|           end |       when scope.type_definition? && record_macro?(node)   then return false | ||||||
|  |       when scope.type_definition? && accessor_macro?(node) then return false | ||||||
|  |       when scope.def? && special_node?(node) | ||||||
|  |         scope.arguments.each do |arg| | ||||||
|  |           variable = arg.variable | ||||||
|  | 
 | ||||||
|  |           ref = variable.reference(variable.node, scope) | ||||||
|  |           ref.explicit = false | ||||||
|         end |         end | ||||||
|         true |  | ||||||
|       when @current_scope.top_level? && record_macro?(node) |  | ||||||
|         false |  | ||||||
|       else |  | ||||||
|         true |  | ||||||
|       end |       end | ||||||
|  |       true | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     private def special_node?(node) | ||||||
|  |       node.name.in?(SPECIAL_NODE_NAMES) && node.args.empty? | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     private def accessor_macro?(node) | ||||||
|  |       node.name.matches? /^(class_)?(getter[?!]?|setter|property[?!]?)$/ | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     private def record_macro?(node) |     private def record_macro?(node) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue