Code Monkey home page Code Monkey logo

Comments (6)

webpro avatar webpro commented on September 22, 2024 1

Closing this with v5.17.0, feel free to discuss further or open a new issue.

from knip.

webpro avatar webpro commented on September 22, 2024

This is an interesting case, could be a challenge to resolve it.

Assuming the intention of allFixtures.order1 is allFixtures.orderFixtures.order1.

And I'm afraid this is a "bridge too far" currently for Knip, the chain breaks here:

import * as orderFixtures from './orderFixtures';
export const allFixtures = { orderFixtures };

Interestingly, trying "Find all references" in my IDE on the order1 export does not lead me to src/App.jsx. Also TS itself has trouble here, this surprises me. the other way around is fine, from usage to implementation.

The issue here is that it's a property on a new export. It's not an explicit or even implicit re-export:

// explicit
export * from './orderFixtures';

// implicit
import * as orderFixtures from './orderFixtures';
export { orderFixtures };

This kind of works, would come close:

import * as orderFixtures from './orderFixtures';
export { orderFixtures as allFixtures };

Obviously not what you want, but just to show what Knip does support technically. In this last case all exports on the orderFixtures namespace are now considered used, which is something that could/should be improved on. In this scenario I also see that TS "Find all references" leads me to src/App.jsx so this scenario is something we could try and fix in Knip.

Your use case is totally valid JS/TS, it's just not something I see easily fixed in Knip.

If you're interested in working on improving this last case I'm happy to elaborate on the whereabouts of related things :)

from knip.

webpro avatar webpro commented on September 22, 2024

Couldn't resist and went ahead on this myself.

This use case should be fixed in v5.14.0 and should properly handle exports individually (before, it assumed all members of the namespace as referenced):

import * as orderFixtures from './orderFixtures';
export { orderFixtures as allFixtures };

During the refactor I went ahead and tried whether supporting the original use case is feasible. I've added support in 5.15.0-keyedreexport.0. Not 100% sure yet I'll add it, but let's see where this goes. Any chance you could give the tagged keyedreexport version a shot on your workload? You can install using e.g.

npm install -D knip@keyedreexport

Happy to hear your feedback, @glemiron!

from knip.

webpro avatar webpro commented on September 22, 2024

From TS compiler internals Discord:

I've just come across a case where I think findReferences might have a bug:

from knip.

glemiron avatar glemiron commented on September 22, 2024

Unfortunately it seems that the fix doesn't work. In order to eliminate set up problems I've created a demo repository here.

I also found the second use case, I think it's related and you can also check out it in the repository.

And another observation: WebStorm is detects unused code correctly, so it seems as solvable problem.

from knip.

webpro avatar webpro commented on September 22, 2024

Thanks for the demo repo! This is super helpful. Let's break it down a little bit.

  1. First case:
import * as orderFixtures from './orderFixtures'
export const allFixtures = { orderFixtures: orderFixtures };  // NOT supported (yet?)
export const allFixtures = { orderFixtures };                 // Supported in @keyedreexport

To this I mentioned that I was "assuming allFixtures.orderFixtures.order1", and the last line in this example is the case that I think is fixed in the keyedreexport release.

  1. Next case. This should be fixed and works as expected in main/v5.14.0 (and keyedreexport as well):
import * as orderFixtures from './orderFixtures';
export { orderFixtures as allFixtures };
  1. The case that's brought in is this one:
import * as orderFixtures from './orderFixtures'
export const allFixtures = orderFixtures

While valid, but this could also (should?) be written like so:

import * as orderFixtures from './orderFixtures'
export { orderFixtures as allFixtures }

This case worked fine in main prior to opening this issue. Or maybe there's additional code/syntax missing that we've not seen/covered yet?

  1. Last but not least you've introduced this case:
import { ValuesType } from 'utility-types'
import * as FEATURES from './constants'
export type Feature = ValuesType<typeof FEATURES>

This is a case that wasn't covered yet and is now added in v5.15.1.

And another observation: WebStorm is detects unused code correctly, so it seems as solvable problem.

Interesting, didn't know about this feature. Do you mean this Find unused code with coverage? That would be a bit different: Knip is a static analysis tool, and I think this feature is instrumenting code and then doing the analysis more dynamically in runtime.

That said, it's a solvable problem indeed. For now I'm going to keep it short here by saying that doing "Find all references" for each export takes too long in large projects (also see Slim down to speed up), but this can still be enabled by using --include-libs (which was added for a different feature and thus is unclear in this context and should be renamed).

from knip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.