Method: GraphQL::Language::Visitor.make_visit_methods
- Defined in:
- lib/graphql/language/visitor.rb
.make_visit_methods(ast_node_class) ⇒ Object
We don't use alias here because it breaks super
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/graphql/language/visitor.rb', line 167 def self.make_visit_methods(ast_node_class) node_method = ast_node_class.visit_method children_of_type = ast_node_class.children_of_type child_visit_method = :"#{node_method}_children" class_eval(" # The default implementation for visiting an AST node.\n # It doesn't _do_ anything, but it continues to visiting the node's children.\n # To customize this hook, override one of its make_visit_methods (or the base method?)\n # in your subclasses.\n #\n # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited\n # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.\n # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.\n def \#{node_method}(node, parent)\n if node.equal?(DELETE_NODE)\n # This might be passed to `super(DELETE_NODE, ...)`\n # by a user hook, don't want to keep visiting in that case.\n [node, parent]\n else\n new_node = node\n \#{\n if method_defined?(child_visit_method)\n \"new_node = \#{child_visit_method}(new_node)\"\n elsif children_of_type\n children_of_type.map do |child_accessor, child_class|\n \"node.\#{child_accessor}.each do |child_node|\n new_child_and_node = \#{child_class.visit_method}_with_modifications(child_node, new_node)\n # Reassign `node` in case the child hook makes a modification\n if new_child_and_node.is_a?(Array)\n new_node = new_child_and_node[1]\n end\n end\"\n end.join(\"\\n\")\n else\n \"\"\n end\n }\n\n if new_node.equal?(node)\n [node, parent]\n else\n [new_node, parent]\n end\n end\n end\n\n def \#{node_method}_with_modifications(node, parent)\n new_node_and_new_parent = \#{node_method}(node, parent)\n apply_modifications(node, parent, new_node_and_new_parent)\n end\n RUBY\nend\n", __FILE__, __LINE__ + 1) |