Instagram / Instagram/LibCST

Extract properties from class defs to common superclass

Open
#427 3 comments 0 reactions 0 assignees View on GitHub
codemod
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

I've been looking at codemods and visitor examples but not finding a clear path to a solution. Any help would be appreciated. Assume I have already determined that a specific set of class names and property names need to be refactored. The refactoring involves adding the properties to a super class and removing the properties from the "source" classes.

For example:

before

```
class A(object):
foo = "foo value"
bar = "someothervalue"
other = "3rd other value"

class B(object):
baz = "someothervalue"
foo = "foo value"
bar = "someothervalue"
```

after

```
class common_class(object):
foo = "foo value"
bar = "someothervalue"

class A(object,common_class):
other = "3rd other value"

class B(object):
baz = "someothervalue"
```

Basically, I want to be able to extract properties to a common superclass. I'm thinking this is a codemod that takes a args for the superclass name, subclass names and property names to move

I am also thinking there are at least 3 passes to consider -
1. Identify which nodes are the property ASSIGN statements in the subclass targets and property names (which needs a lookup to check these). Also to remember if the soon-to-be subclasses already have a reference to the superclass name.
2. A pass to update or add the superclass block with one of the properties that match the subclass properties found in pass 1.
3. A pass to actually remove the properties from the sub classes where they were found (optionally comment out these)
4. A pass to update the subclass CLASSDEF to include the superclass name if not present

I'm just not sure what combination of metadata wrapper, transform and codemode (or matches) I need for this solution and I don't find a comprehensive example that does this sort of thing. I would have thought that subclass or member extraction refactoring was a common need in a refactoring tool.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.