MCPcopy Index your code
hub / github.com/SourceCode-AI/aura / replace_string

Method replace_string

aura/analyzers/python/rewrite_ast.py:289–330  ·  view source on GitHub ↗

Rewrites an expression `"some_string".replace("s", "a")` AST structure: :: aura.analyzers.python.nodes.Call( func=aura.analyzers.python.nodes.Attribute( source=aura.analyzers.python.nodes.String(value='some_string'),

(self, context)

Source from the content-addressed store, hash-verified

287 return
288
289 def replace_string(self, context): # TODO: add test
290 """
291 Rewrites an expression `"some_string".replace("s", "a")`
292 AST structure:
293
294 ::
295 aura.analyzers.python.nodes.Call(
296 func=aura.analyzers.python.nodes.Attribute(
297 source=aura.analyzers.python.nodes.String(value='some_string'),
298 attr='replace',
299 action='Load'
300 ),
301 args=[
302 aura.analyzers.python.nodes.String(value='s'),
303 aura.analyzers.python.nodes.String(value='a')
304 ],
305 kwargs={}
306 )
307 """
308 # We are looking for a function call
309 if type(context.node) != Call:
310 return
311 # Function target is an attribute with `replace` attribute name
312 func: Attribute = context.node.func
313 if not (type(func) == Attribute and func.attr == "replace"):
314 return
315
316 replace_source = func.source
317 # Source of the replace must be String
318 if type(replace_source) != String:
319 return
320
321 # Check that replace args are also strings
322 if len(context.node.args) < 2 or type(context.node.args[0]) != String or type(context.node.args[1]) != String:
323 return
324
325 # Rewrite the node by applying the replace operation
326 # TODO: check docs if replace takes additional (kw)arguments
327 data = str(replace_source).replace(str(context.node.args[0]), str(context.node.args[1]))
328 new_node = String(value=data)
329 new_node.enrich_from_previous(context.node)
330 context.replace(new_node)
331
332 def unary_op(self, context):
333 if not type(context.node) in (dict, OrderedDict):

Callers

nothing calls this directly

Calls 2

StringClass · 0.85
enrich_from_previousMethod · 0.80

Tested by

no test coverage detected