@@ -61,61 +61,6 @@ def visit
6161 end
6262 end
6363
64- # We don't use `alias` here because it breaks `super`
65- def self . make_visit_methods ( ast_node_class )
66- node_method = ast_node_class . visit_method
67- children_of_type = ast_node_class . children_of_type
68- child_visit_method = :"#{ node_method } _children"
69-
70- class_eval ( <<-RUBY , __FILE__ , __LINE__ + 1 )
71- # The default implementation for visiting an AST node.
72- # It doesn't _do_ anything, but it continues to visiting the node's children.
73- # To customize this hook, override one of its make_visit_methods (or the base method?)
74- # in your subclasses.
75- #
76- # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
77- # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
78- # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.
79- def #{ node_method } (node, parent)
80- if node.equal?(DELETE_NODE)
81- # This might be passed to `super(DELETE_NODE, ...)`
82- # by a user hook, don't want to keep visiting in that case.
83- [node, parent]
84- else
85- new_node = node
86- #{
87- if method_defined? ( child_visit_method )
88- "new_node = #{ child_visit_method } (new_node)"
89- elsif children_of_type
90- children_of_type . map do |child_accessor , child_class |
91- "node.#{ child_accessor } .each do |child_node|
92- new_child_and_node = #{ child_class . visit_method } _with_modifications(child_node, new_node)
93- # Reassign `node` in case the child hook makes a modification
94- if new_child_and_node.is_a?(Array)
95- new_node = new_child_and_node[1]
96- end
97- end"
98- end . join ( "\n " )
99- else
100- ""
101- end
102- }
103-
104- if new_node.equal?(node)
105- [node, parent]
106- else
107- [new_node, parent]
108- end
109- end
110- end
111-
112- def #{ node_method } _with_modifications(node, parent)
113- new_node_and_new_parent = #{ node_method } (node, parent)
114- apply_modifications(node, parent, new_node_and_new_parent)
115- end
116- RUBY
117- end
118-
11964 def on_document_children ( document_node )
12065 new_node = document_node
12166 document_node . children . each do |child_node |
@@ -216,6 +161,63 @@ def on_argument_children(new_node)
216161 new_node
217162 end
218163
164+ # rubocop:disable Development/NoEvalCop This eval takes static inputs at load-time
165+
166+ # We don't use `alias` here because it breaks `super`
167+ def self . make_visit_methods ( ast_node_class )
168+ node_method = ast_node_class . visit_method
169+ children_of_type = ast_node_class . children_of_type
170+ child_visit_method = :"#{ node_method } _children"
171+
172+ class_eval ( <<-RUBY , __FILE__ , __LINE__ + 1 )
173+ # The default implementation for visiting an AST node.
174+ # It doesn't _do_ anything, but it continues to visiting the node's children.
175+ # To customize this hook, override one of its make_visit_methods (or the base method?)
176+ # in your subclasses.
177+ #
178+ # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
179+ # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
180+ # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.
181+ def #{ node_method } (node, parent)
182+ if node.equal?(DELETE_NODE)
183+ # This might be passed to `super(DELETE_NODE, ...)`
184+ # by a user hook, don't want to keep visiting in that case.
185+ [node, parent]
186+ else
187+ new_node = node
188+ #{
189+ if method_defined? ( child_visit_method )
190+ "new_node = #{ child_visit_method } (new_node)"
191+ elsif children_of_type
192+ children_of_type . map do |child_accessor , child_class |
193+ "node.#{ child_accessor } .each do |child_node|
194+ new_child_and_node = #{ child_class . visit_method } _with_modifications(child_node, new_node)
195+ # Reassign `node` in case the child hook makes a modification
196+ if new_child_and_node.is_a?(Array)
197+ new_node = new_child_and_node[1]
198+ end
199+ end"
200+ end . join ( "\n " )
201+ else
202+ ""
203+ end
204+ }
205+
206+ if new_node.equal?(node)
207+ [node, parent]
208+ else
209+ [new_node, parent]
210+ end
211+ end
212+ end
213+
214+ def #{ node_method } _with_modifications(node, parent)
215+ new_node_and_new_parent = #{ node_method } (node, parent)
216+ apply_modifications(node, parent, new_node_and_new_parent)
217+ end
218+ RUBY
219+ end
220+
219221 [
220222 Language ::Nodes ::Argument ,
221223 Language ::Nodes ::Directive ,
@@ -256,6 +258,8 @@ def on_argument_children(new_node)
256258 make_visit_methods ( ast_node_class )
257259 end
258260
261+ # rubocop:enable Development/NoEvalCop
262+
259263 private
260264
261265 def apply_modifications ( node , parent , new_node_and_new_parent )
0 commit comments