public
Authored by avatar Jérome Perrin

Rewrite activate() calls to pass activity=SQLDict explicitly

Edited
rewrite_activate_default_activity_SQLDict.py 1.01 KiB
import sys

from bowler import Query
from bowler.imr import FunctionArgument, FunctionSpec
from fissix.fixer_util import Name


def addActivityDefaultValue(node, capture, filename):
  if 'PaypalService.py' in filename:
    # XXX moreorless.patch.PatchException: Failed to apply with offset at 103
    return False

  spec = FunctionSpec.build(node, capture)
  for arg in spec.arguments:
    if arg.name == 'activity':
      return False
    if arg.star:
      print(f"Warning: skipping because ** used: {filename}:{arg.star.lineno}")
      return False

  value = Name("'SQLDict'")
  prefix = None
  # try to keep indentation for multi lines arguments
  if spec.arguments and spec.arguments[-1].prefix:
    prefix = spec.arguments[0].prefix
  fa = FunctionArgument('activity', value, '')
  spec.arguments.insert(0, fa)
  spec.arguments[0].prefix = prefix
  spec.explode()
  return True
  
(
    Query(sys.argv[1], python_version=2)
    .select_method('activate')
    .is_call()
    .filter(addActivityDefaultValue)
    .write()
)
  • dirty patch we could use to keep the trailing ,

    
    diff --git a/bowler/imr.py b/bowler/imr.py
    index 8731e19..b36b10d 100644
    --- a/bowler/imr.py
    +++ b/bowler/imr.py
    @@ -235,6 +235,8 @@ class FunctionSpec:
                 )
     
             if arguments:
    +            if self.capture['function_arguments'][0].children[-1].value == ',':
    +                arguments.append_child(self.capture['function_arguments'][0].children[-1])
                 parameters.insert_child(1, arguments)
     
             self.capture["function_parameters"].replace(parameters)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment