Skip to content

Extract and Inline Function Refactorings #14

@rodrigo-brito

Description

@rodrigo-brito

I found some issues with function calls
The parser identifies the function calls very well, but this relationship is not defined in nodes.

To extract and inline function works, we need to create relationships with the nodes:

root.getRelationships().add(new CstNodeRelationship(CstNodeRelationshipType.USE, caller.getId(),
						nodeByAddress.get(functionCall).getId()));

In RefDiff we have two relationships to setup up (USE for call functions and SUBTYPE for inheritance)

  • USE relationship is necessary to identify and extract / moves / inlines. For example, an Extract Function moves a block of code, creates a new function and USE it in the old place (function call).
  • SUBTYPE is used for Push Down and Pull Up refactorings as well (if you want support for this)

We have the information to construct the USE relationship. But, it is difficult to identify nodes. The snippet above uses a map that contains all nodes found in the parser step, in which the KEY of the map should be a unique identifier for each node. For the following example https://github.com/rodrigo-brito/refactoring-python-example/blob/master/main.py, it should look like:

{
   /main.py: <CST Node of main.py>,
   /main.py/isEven: <CST Node of isEven function>,
   /main.py/printIsEven: <CST Node of printIsEven function>,
   /calculator.py: <CST Node of calculator.py>,
   /calculator.py/Calculator: <CST Node of Calculator Class>,
   /calculator.py/Calculator/summary: <CST Node of summary Function>,
   /calculator.py/Calculator/multiply: <CST Node of multiply Function>,
}

The KEY format is free, but it should be equal between commits. Then, a good way to construct this key could be joining Parent KEY + Node name.

And functionCalls map, should contain the relationship between these unique IDs. For example, in the example repository. The CST Node /main.py should contain function calls for summary, multiply, and printIsEven. Also, printIsEven should contains a function call for isEven function.

For this example, functionCalls must be something like this:

{
   /main.py: [
      /calculator.py/Calculator/summary,
      /calculator.py/Calculator/multiply,
      /main.py/printIsEven,
   ],
   /main.py/printIsEven: [
      /main.py/isEven
   ]
}

If it is done, I think all other refactorings will work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions