1
1
from enum import Enum
2
- import re
3
2
4
3
class SStubPattern (Enum ):
5
4
@@ -62,7 +61,9 @@ def classify_sstub(source_ast, target_ast):
62
61
if source_name == target_name :
63
62
classifier_fns .append (same_function_mod )
64
63
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" )):
66
67
classifier_fns .append (change_if_statement )
67
68
68
69
if source_ast .type in ["tuple" , "list" , "dictionary" , "set" ]:
@@ -119,15 +120,15 @@ def pisomorph(A, B):
119
120
120
121
# Binary operand ----------------------------------------------------------------
121
122
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
-
128
123
129
124
def 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
131
132
132
133
133
134
@@ -218,16 +219,19 @@ def change_attribute_used(source_ast, target_ast):
218
219
219
220
220
221
def 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"
222
228
223
229
224
230
def change_binary_operator (source_ast , target_ast ):
225
231
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
231
235
232
236
return False
233
237
@@ -332,12 +336,16 @@ def same_function_swap_args(source_ast, target_ast):
332
336
if len (source_ast .children ) != len (target_ast .children ):
333
337
return False
334
338
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
339
341
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 ]))
341
349
342
350
343
351
same_function_edits = {
@@ -364,11 +372,18 @@ def same_function_mod(source_ast, target_ast):
364
372
365
373
366
374
def 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
+
367
379
return any (pisomorph (c , source_ast ) for c in target_ast .children )
368
380
369
381
370
382
def 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 )
372
387
373
388
374
389
def change_if_statement (source_ast , target_ast ):
@@ -413,7 +428,8 @@ def add_function_around_expression(source_ast, target_ast):
413
428
if argument_list .type != "argument_list" :
414
429
return False
415
430
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
417
433
418
434
for arg in argument_list .children :
419
435
if pisomorph (arg , source_ast ):
0 commit comments