facebookincubator / retrie

Retrie is a powerful, easy-to-use codemodding tool for Haskell.
MIT License
502 stars 30 forks source link

Steps to change a data contructor and associated types name? #32

Open bontaq opened 3 years ago

bontaq commented 3 years ago

Hi, I've been using retrie for doing some refactoring in a project and it's quite nice.

I'm wondering what steps one would take to do something like:

data MyThing = MyThing { words :: [String] } -> data TheirThing = TheirThing { words :: [String] }

across a whole project, including imports. Is that possible? :pray:

xich commented 3 years ago

Not automagically, but (without trying it), I would approach it something like this:

You have:

module A where

data MyThing = MyThing { words :: [String] }

Add a new module:

module B where

data TheirThing = MyThing { words :: [String] }

Then edit module A:

module A where

import B

type MyThing = TheirThing

Typecheck your program to make sure this didn't break anything.

Then run retrie: retrie --type-forward A.MyThing --import "import B"

Which will change all the type-level MyThings to TheirThings and add the import to B wherever that edit is made.

Then typecheck to make sure everything still builds.

Then run retrie again: retrie --adhoc "MyThing = TheirThing"

Which should edit the constructors.

Then delete module A and all the imports of A (latter with grep/sed if needed). (#33 is open to add more complex import edits eventually.)