speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

Don't rewire TSTypeReference #219

Closed rosswarren closed 3 years ago

rosswarren commented 5 years ago

Fixes: https://github.com/speedskater/babel-plugin-rewire/issues/218

Update: due to this not being merged for a long time I have published a fork as babel-plugin-rewire-ts to unblock myself. You are welcome to use it too. You can contribute any further fixes here https://github.com/rosswarren/babel-plugin-rewire-ts. https://www.npmjs.com/package/babel-plugin-rewire-ts.

I understand you probably want tests for this change, but I have no idea how we can actually do that because the rest of the tests haven't been updated for babel 7 and I can't run @babel/preset-typescript against babel 6.

cprass commented 5 years ago

Can't we just check for !(parent.type.slice(0,2) === 'TS')? With your proposed change it fixes that specific error for me, but I get more TS-type related errors after it.

rosswarren commented 5 years ago

@ChrisGitter I'm concerned that it could stop the plugin from rewiring certain things that could actually be handled? Is it actually safe to ignore everything prefixed with TS?

cprass commented 5 years ago

@rosswarren I have no idea if it is safe. I'm way too unfamiliar with the babel transpiling process and all the babel types. You're probably right with the assumption that some of the TS prefixed stuff shouldn't be left out. I'll check if I can reproduce the follow-up errors I got with the TS-related types and post them here.

dzcpy commented 5 years ago

Will it be merged? Typescript is getting more and more popular now. It would be nice to see a perfectly working Typescript + rewire module

damaon commented 5 years ago

Thanks for posting this pull request!

I had to change to:

      !(parent.type === 'TSTypeQuery') &&
      !(parent.type === 'TSTypeReference')

to make it work for me.

philippelesaux commented 5 years ago

Thanks!!! I needed to add TSQualifiedName to the list as well.

rosswarren commented 5 years ago

Thanks @Machiaweliczny, @philippelesaux I have added both TSTypeQuery and TSQualifiedName.

NilSet commented 5 years ago

I also needed to exclude TSExpressionWithTypeArguments and TSDeclareFunction

jljorgenson18 commented 5 years ago

Any updates on when this could be merged in? We are seeing this issue after moving to typescript. It seems like a quick fix and it should be safe.

willcaul commented 5 years ago

Are there any updates on this? I'm also getting the following error when trying to use this plugin with the @babel/typescript preset: TypeError: Property left of TSQualifiedName expected node to be of a type ["TSEntityName"] but instead got "CallExpression"

willcaul commented 5 years ago

@rosswarren's fix is working for me when I also exclude the types that @NilSet identified (TSExpressionWithTypeArguments and TSDeclareFunction)

agrasley commented 5 years ago

I also ran into an issue with default exports and function overloads. You can fix it by adding

if (!wasProcessed(path) && !t.isTSDeclareFunction(path.node.declaration)) {

to line 106 of your PR.

yinzara commented 5 years ago

Any chance this could have some traction. Makes the plugin useless for TypeScript projects which is specifically what it's good for.

rosswarren commented 4 years ago

Because this has not been merged, I have published this branch as babel-plugin-rewire-ts version 1.3.0. I will happily accept any contributions there to improve compatibility. https://github.com/rosswarren/babel-plugin-rewire-ts https://www.npmjs.com/package/babel-plugin-rewire-ts

karladler commented 4 years ago

It's June 2020. Are there any updates on this? Why isn't it merged yet?