@@ -539,6 +539,75 @@ def leave_selection_set(*args):
539
539
["leave" , "selection_set" , None ],
540
540
]
541
541
542
+ def visit_nodes_with_unknown_kinds_but_does_not_traverse_deeper ():
543
+ custom_ast = parse ("{ a }" )
544
+
545
+ class CustomFieldNode (SelectionNode ):
546
+ __slots__ = "name" , "selection_set"
547
+
548
+ name : NameNode
549
+ selection_set : Optional [SelectionSetNode ]
550
+
551
+ custom_selection_set = cast (FieldNode , custom_ast .definitions [0 ]).selection_set
552
+ assert custom_selection_set is not None
553
+ custom_selection_set .selections = custom_selection_set .selections + [
554
+ CustomFieldNode (
555
+ name = NameNode (value = "NameNodeToBeSkipped" ),
556
+ selection_set = SelectionSetNode (
557
+ selections = CustomFieldNode (
558
+ name = NameNode (value = "NameNodeToBeSkipped" )
559
+ )
560
+ ),
561
+ )
562
+ ]
563
+
564
+ visited = []
565
+
566
+ class TestVisitor (Visitor ):
567
+ @staticmethod
568
+ def enter (node , * _args ):
569
+ visited .append (["enter" , node .kind , get_value (node )])
570
+
571
+ @staticmethod
572
+ def leave (node , * _args ):
573
+ visited .append (["leave" , node .kind , get_value (node )])
574
+
575
+ visit (custom_ast , TestVisitor ())
576
+ assert visited == [
577
+ ["enter" , "document" , None ],
578
+ ["enter" , "operation_definition" , None ],
579
+ ["enter" , "selection_set" , None ],
580
+ ["enter" , "field" , None ],
581
+ ["enter" , "name" , "a" ],
582
+ ["leave" , "name" , "a" ],
583
+ ["leave" , "field" , None ],
584
+ ["enter" , "custom_field" , None ],
585
+ ["leave" , "custom_field" , None ],
586
+ ["leave" , "selection_set" , None ],
587
+ ["leave" , "operation_definition" , None ],
588
+ ["leave" , "document" , None ],
589
+ ]
590
+
591
+ def cannot_define_visitor_with_unknown_ast_nodes ():
592
+ with raises (TypeError ) as exc_info :
593
+
594
+ class VisitorWithNonExistingNode (Visitor ):
595
+ def enter_field (self , * _args ):
596
+ pass
597
+
598
+ def leave_garfield (self , * _args ):
599
+ pass
600
+
601
+ assert str (exc_info .value ) == "Invalid AST node kind: garfield."
602
+
603
+ with raises (TypeError ) as exc_info :
604
+
605
+ class VisitorWithUnspecificNode (Visitor ):
606
+ def enter_type_system_extension (self , * _args ):
607
+ pass
608
+
609
+ assert str (exc_info .value ) == "Invalid AST node kind: type_system_extension."
610
+
542
611
def legacy_visits_variables_defined_in_fragments ():
543
612
ast = parse (
544
613
"fragment a($v: Boolean = false) on t { f }" ,
@@ -970,75 +1039,6 @@ def leave(*args):
970
1039
]
971
1040
972
1041
973
- def describe_support_for_custom_ast_nodes ():
974
- custom_ast = parse ("{ a }" )
975
-
976
- class CustomFieldNode (SelectionNode ):
977
- __slots__ = "name" , "selection_set"
978
-
979
- name : NameNode
980
- selection_set : Optional [SelectionSetNode ]
981
-
982
- custom_selection_set = cast (FieldNode , custom_ast .definitions [0 ]).selection_set
983
- assert custom_selection_set is not None
984
- custom_selection_set .selections = custom_selection_set .selections + [
985
- CustomFieldNode (
986
- name = NameNode (value = "b" ),
987
- selection_set = SelectionSetNode (
988
- selections = CustomFieldNode (name = NameNode (value = "c" ))
989
- ),
990
- )
991
- ]
992
-
993
- def does_not_traverse_unknown_node_kinds ():
994
- visited = []
995
-
996
- class TestVisitor (Visitor ):
997
- @staticmethod
998
- def enter (node , * _args ):
999
- visited .append (["enter" , node .kind , get_value (node )])
1000
-
1001
- @staticmethod
1002
- def leave (node , * _args ):
1003
- visited .append (["leave" , node .kind , get_value (node )])
1004
-
1005
- visit (custom_ast , TestVisitor ())
1006
- assert visited == [
1007
- ["enter" , "document" , None ],
1008
- ["enter" , "operation_definition" , None ],
1009
- ["enter" , "selection_set" , None ],
1010
- ["enter" , "field" , None ],
1011
- ["enter" , "name" , "a" ],
1012
- ["leave" , "name" , "a" ],
1013
- ["leave" , "field" , None ],
1014
- ["enter" , "custom_field" , None ],
1015
- ["leave" , "custom_field" , None ],
1016
- ["leave" , "selection_set" , None ],
1017
- ["leave" , "operation_definition" , None ],
1018
- ["leave" , "document" , None ],
1019
- ]
1020
-
1021
- def cannot_define_visitor_with_unknown_ast_nodes ():
1022
- with raises (TypeError ) as exc_info :
1023
-
1024
- class VisitorWithNonExistingNode (Visitor ):
1025
- def enter_field (self , * _args ):
1026
- pass
1027
-
1028
- def leave_garfield (self , * _args ):
1029
- pass
1030
-
1031
- assert str (exc_info .value ) == "Invalid AST node kind: garfield."
1032
-
1033
- with raises (TypeError ) as exc_info :
1034
-
1035
- class VisitorWithUnspecificNode (Visitor ):
1036
- def enter_type_system_extension (self , * _args ):
1037
- pass
1038
-
1039
- assert str (exc_info .value ) == "Invalid AST node kind: type_system_extension."
1040
-
1041
-
1042
1042
def describe_visit_in_parallel ():
1043
1043
@mark .parametrize ("skip_action" , (SKIP , False ), ids = ("SKIP" , "False" ))
1044
1044
def allows_skipping_a_sub_tree (skip_action ):
0 commit comments