11from enum import Enum
2- import re
32
43class SStubPattern (Enum ):
54
@@ -62,7 +61,9 @@ def classify_sstub(source_ast, target_ast):
6261 if source_name == target_name :
6362 classifier_fns .append (same_function_mod )
6463
65- if _query_path (source_ast , "if_statement" , "condition" ) or _query_path (source_ast , "elif_clause" , "condition" ):
64+ if (_query_path (source_ast , "if_statement" , "condition" )
65+ or _query_path (source_ast , "elif_clause" , "condition" )
66+ or _query_path (source_ast , "while_statement" , "condition" )):
6667 classifier_fns .append (change_if_statement )
6768
6869 if source_ast .type in ["tuple" , "list" , "dictionary" , "set" ]:
@@ -119,15 +120,15 @@ def pisomorph(A, B):
119120
120121# Binary operand ----------------------------------------------------------------
121122
122- def _is_binary_operand (source_ast , target_ast ):
123- return _query_path (source_ast , "binary_operator" , "left" ) or _query_path (source_ast , "binary_operator" , "right" )
124-
125- def is_boolean_operand (source_ast , target_ast ):
126- return _query_path (source_ast , "boolean_operator" , "left" ) or _query_path (source_ast , "boolean_operator" , "right" )
127-
128123
129124def is_binary_operand (source_ast , target_ast ):
130- return _is_binary_operand (source_ast , target_ast ) or is_boolean_operand (source_ast , target_ast )
125+
126+ for bin_op_type in ["binary_operator" , "comparison_operator" , "boolean_operator" ]:
127+ for direction in ["left" , "right" ]:
128+ if (_query_path (source_ast , bin_op_type , direction , depth = 1 )):
129+ return True
130+
131+ return False
131132
132133
133134
@@ -218,16 +219,19 @@ def change_attribute_used(source_ast, target_ast):
218219
219220
220221def change_identifier_used (source_ast , target_ast ):
221- return source_ast .type == "identifier"
222+
223+ # Following ManySStuBs we ignore the following Method declaration, Class Declaration, Variable Declaration
224+ if any (x in source_ast .parent .type for x in ["definition" , "declaration" ]):
225+ return False
226+
227+ return source_ast .type == "identifier" and target_ast .type == "identifier"
222228
223229
224230def change_binary_operator (source_ast , target_ast ):
225231
226- for operator in ["binary_operator" , "boolean_operator" , "comparison_operator" ]:
227- if _query_path (source_ast , operator , "*" , depth = 1 ):
228- if (not _query_path (source_ast , operator , "left" , depth = 1 )
229- and not _query_path (source_ast , operator , "right" , depth = 1 )):
230- return True
232+ if source_ast .parent .type in ["binary_operator" , "boolean_operator" , "comparison_operator" ]:
233+ bin_op = source_ast .parent
234+ return bin_op .children [1 ] == source_ast
231235
232236 return False
233237
@@ -332,12 +336,16 @@ def same_function_swap_args(source_ast, target_ast):
332336 if len (source_ast .children ) != len (target_ast .children ):
333337 return False
334338
335- arguments = source_ast .children
336- for arg in arguments :
337- if not any (pisomorph (t , arg ) for t in target_ast .children ):
338- return False
339+ src_arguments = source_ast .children
340+ target_arguments = target_ast .children
339341
340- return True
342+ diff_args = [i for i , src_arg in enumerate (src_arguments ) if not pisomorph (src_arg , target_arguments [i ])]
343+
344+ if len (diff_args ) != 2 : return False
345+
346+ swap_0 , swap_1 = diff_args
347+ return (pisomorph (src_arguments [swap_0 ], target_arguments [swap_1 ])
348+ and pisomorph (src_arguments [swap_1 ], target_arguments [swap_0 ]))
341349
342350
343351same_function_edits = {
@@ -364,11 +372,18 @@ def same_function_mod(source_ast, target_ast):
364372
365373
366374def more_specific_if (source_ast , target_ast ):
375+
376+ if not target_ast .type == "boolean_operator" : return False
377+ if target_ast .children [1 ].type != "and" : return False
378+
367379 return any (pisomorph (c , source_ast ) for c in target_ast .children )
368380
369381
370382def less_specific_if (source_ast , target_ast ):
371- return any (pisomorph (c , target_ast ) for c in source_ast .children )
383+ if not target_ast .type == "boolean_operator" : return False
384+ if target_ast .children [1 ].type != "or" : return False
385+
386+ return any (pisomorph (c , source_ast ) for c in target_ast .children )
372387
373388
374389def change_if_statement (source_ast , target_ast ):
@@ -413,7 +428,8 @@ def add_function_around_expression(source_ast, target_ast):
413428 if argument_list .type != "argument_list" :
414429 return False
415430
416- if len (argument_list .children ) != 3 : return False
431+ # It seems that adding arguments together with a function seems to be okay (see PySStuBs dataset)
432+ #if len(argument_list.children) != 3: return False
417433
418434 for arg in argument_list .children :
419435 if pisomorph (arg , source_ast ):
0 commit comments