@@ -76,67 +76,6 @@ def visit
7676 end
7777 end
7878
79- # We don't use `alias` here because it breaks `super`
80- def self . make_visit_methods ( ast_node_class )
81- node_method = ast_node_class . visit_method
82- children_of_type = ast_node_class . children_of_type
83- child_visit_method = :"#{ node_method } _children"
84-
85- class_eval ( <<-RUBY , __FILE__ , __LINE__ + 1 )
86- # The default implementation for visiting an AST node.
87- # It doesn't _do_ anything, but it continues to visiting the node's children.
88- # To customize this hook, override one of its make_visit_methods (or the base method?)
89- # in your subclasses.
90- #
91- # For compatibility, it calls hook procs, too.
92- # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
93- # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
94- # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.
95- def #{ node_method } (node, parent)
96- if node.equal?(DELETE_NODE)
97- # This might be passed to `super(DELETE_NODE, ...)`
98- # by a user hook, don't want to keep visiting in that case.
99- [node, parent]
100- else
101- # Run hooks if there are any
102- new_node = node
103- no_hooks = !@visitors.key?(node.class)
104- if no_hooks || begin_visit(new_node, parent)
105- #{
106- if method_defined? ( child_visit_method )
107- "new_node = #{ child_visit_method } (new_node)"
108- elsif children_of_type
109- children_of_type . map do |child_accessor , child_class |
110- "node.#{ child_accessor } .each do |child_node|
111- new_child_and_node = #{ child_class . visit_method } _with_modifications(child_node, new_node)
112- # Reassign `node` in case the child hook makes a modification
113- if new_child_and_node.is_a?(Array)
114- new_node = new_child_and_node[1]
115- end
116- end"
117- end . join ( "\n " )
118- else
119- ""
120- end
121- }
122- end
123- end_visit(new_node, parent) unless no_hooks
124-
125- if new_node.equal?(node)
126- [node, parent]
127- else
128- [new_node, parent]
129- end
130- end
131- end
132-
133- def #{ node_method } _with_modifications(node, parent)
134- new_node_and_new_parent = #{ node_method } (node, parent)
135- apply_modifications(node, parent, new_node_and_new_parent)
136- end
137- RUBY
138- end
139-
14079 def on_document_children ( document_node )
14180 new_node = document_node
14281 document_node . children . each do |child_node |
@@ -237,6 +176,68 @@ def on_argument_children(new_node)
237176 new_node
238177 end
239178
179+ # rubocop:disable Development/NoEvalCop This eval takes static inputs at load-time
180+
181+ def self . make_visit_methods ( ast_node_class )
182+ node_method = ast_node_class . visit_method
183+ children_of_type = ast_node_class . children_of_type
184+ child_visit_method = :"#{ node_method } _children"
185+
186+ class_eval ( <<-RUBY , __FILE__ , __LINE__ + 1 )
187+ # The default implementation for visiting an AST node.
188+ # It doesn't _do_ anything, but it continues to visiting the node's children.
189+ # To customize this hook, override one of its make_visit_methods (or the base method?)
190+ # in your subclasses.
191+ #
192+ # For compatibility, it calls hook procs, too.
193+ # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
194+ # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
195+ # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.
196+ def #{ node_method } (node, parent)
197+ if node.equal?(DELETE_NODE)
198+ # This might be passed to `super(DELETE_NODE, ...)`
199+ # by a user hook, don't want to keep visiting in that case.
200+ [node, parent]
201+ else
202+ # Run hooks if there are any
203+ new_node = node
204+ no_hooks = !@visitors.key?(node.class)
205+ if no_hooks || begin_visit(new_node, parent)
206+ #{
207+ if method_defined? ( child_visit_method )
208+ "new_node = #{ child_visit_method } (new_node)"
209+ elsif children_of_type
210+ children_of_type . map do |child_accessor , child_class |
211+ "node.#{ child_accessor } .each do |child_node|
212+ new_child_and_node = #{ child_class . visit_method } _with_modifications(child_node, new_node)
213+ # Reassign `node` in case the child hook makes a modification
214+ if new_child_and_node.is_a?(Array)
215+ new_node = new_child_and_node[1]
216+ end
217+ end"
218+ end . join ( "\n " )
219+ else
220+ ""
221+ end
222+ }
223+ end
224+ end_visit(new_node, parent) unless no_hooks
225+ if new_node.equal?(node)
226+ [node, parent]
227+ else
228+ [new_node, parent]
229+ end
230+ end
231+ end
232+ def #{ node_method } _with_modifications(node, parent)
233+ new_node_and_new_parent = #{ node_method } (node, parent)
234+ apply_modifications(node, parent, new_node_and_new_parent)
235+ end
236+ RUBY
237+ end
238+
239+
240+
240241 [
241242 Language ::Nodes ::Argument ,
242243 Language ::Nodes ::Directive ,
@@ -277,6 +278,8 @@ def on_argument_children(new_node)
277278 make_visit_methods ( ast_node_class )
278279 end
279280
281+ # rubocop:enable Development/NoEvalCop
282+
280283 private
281284
282285 def apply_modifications ( node , parent , new_node_and_new_parent )
0 commit comments