hypermod-community icon indicating copy to clipboard operation
hypermod-community copied to clipboard

Feature: Allow adding a TS 4.5 style type import

Open IanVS opened this issue 3 years ago • 7 comments

I'm trying to write a codemod that changes from the old style of type imports:

import type {MyType} from './types';

to the newer style:

import {type MyType} from './types';

I saw you have some import helpers, but don't see anything that allows creation of an ImportSpecifier with importKind: "type", which I think is what is needed. Maybe you could consider adding it?

Thanks!

IanVS avatar Feb 08 '22 15:02 IanVS

Actually, I wonder if this is even possible. I opened a request at https://github.com/facebook/jscodeshift/issues/481, because I think jscodeshift does not allow this kind of creation.

IanVS avatar Feb 08 '22 15:02 IanVS

Yeah, in fact i think this is actually a recast thing! Please LMK if you manage to find a solution :)

danieldelcore avatar Mar 09 '22 23:03 danieldelcore

I was able to find a way to accomplish what I was trying to do, and published an npm package: https://www.npmjs.com/package/type-import-codemod

It didn't quite seem to fit with the goals of this project (it's not something intended to use with a particular library/package), but I did start off down that road at first and the scaffolding from the cli was helpful, so thanks!

IanVS avatar Mar 15 '22 22:03 IanVS

That's actually really cool lib, thanks for sharing!

I can actually see this being incorporated into this library in a few ways!

  • As a util: https://www.codeshiftcommunity.com/docs/utils
  • A guide: https://www.codeshiftcommunity.com/docs/import-manipulation
  • A stand-alone codemod in the community folder: https://www.codeshiftcommunity.com/docs/atlaskit__avatar
  • Or even all of the above!

Just for context, the goal of this project is to do the following:

  1. Provide documentation for writing and publishing codemods
  2. Provide tooling & utilities to make it easier to write and run codemods
  3. Provide a library of community codemods, similar to definitelytyped

Would love to pair with you to add this if you're interested in contributing 👍

danieldelcore avatar Mar 15 '22 23:03 danieldelcore

I haven't forgotten about this issue, I've just been preoccupied with other things lately. But, I'm giving a talk at a meetup about codemods using my project above as an example, and will mention CodeshiftCommunity as well. I hope to come back to this once things have settled down a bit.

IanVS avatar May 29 '22 03:05 IanVS

That sounds fantastic! All the best with your talk!! Send me a link to the recording, would love to watch!

danieldelcore avatar May 29 '22 23:05 danieldelcore

Hey @IanVS, how'd your talk go!? Is there a recording or something I can watch!

danieldelcore avatar Jun 16 '22 06:06 danieldelcore