pbomb / flow-immutable-models

Generates model classes from Flow types using Immutable.js
42 stars 8 forks source link

Automatically include necessary imports if missing from a model file #4

Open object88 opened 7 years ago

object88 commented 7 years ago

The current code requires that ImmutableModel must exist in a model file for it to be functional. It would be nice to include that import in the generated portion of the file if it is missing.

pbomb commented 7 years ago

Agreed. The same is true for the immutable import. I feel like a pull request might be coming. Yeah? 😉

object88 commented 7 years ago

Feel free to assign it to me, and I'll poke at it.

object88 commented 7 years ago

Working through this. When I first looked at this, I had assumed that the comment block was free-floating, but I now see that comment blocks pretty much have to be associated with a code block (as per issues in the ast-types project). (Which also means that because it is possible to have more than one ModelType in a file, there will be more than one comment block.)

I have a very rudimentary pass at this which pops an import at the end of a file.

Before proceeding further, I'd like to do another refactor which will prepend the comment block only to the first added code block. This will have the side benefit of making it easier to handle pre-existing added code blocks -- look through all top-level code blocks in sequential order and remove anything at or after the block which has out delimiting comment. This is Good(tm) because if we add the ImmutableModel import, I'd rather not see that leak into the original code.

Thoughts or feedback?