Fix iterating over namespaces imported using '*' (star) reporting unused classes#898
Conversation
…sed classes Using ``import * as classes from...``, and then doing an iteration ``for (... in classes)`` erroneously marks the imported classes as 'not used'. The import-star path leads to a namespace being added, and the reference to the namespace due to iteration was not being considered, although Object.keys-type accesses were. While fixing the ...in case, I realized the ...of iteration case was also potentially broken, although it was very difficult to build a test for that particular edge case ... it is highly dependent on the tsconfig options.
|
Thanks for the pull request, @heystewart
For assertions, including empty entries, there are
The tests are pretty much integration/end-to-end tests in the sense that also references to binaries in Any chance you could minimize this PR to its essentials? I'm interested in merging that :) As a side note, I refer to adding more supported cases as additional features, not things that were bugs or broken. Knip as a whole essentially is heuristics and patterns to maximize output value and thus minimize false positives |
I'm not quite sure I understand! The code is producing the 'correct' issues, but it creates an entry for a package that has only ignored errors, and therefore shows an empty list. I am removing the empty list so that the output is consistent with what would occur if the ignored binaries weren't even mentioned. |
|
Probably best to remove unrelated things from this PR so we can focus on its main purpose :) |
🤔 are you referring to the ignore binaries, or the infer error? This is a false positive ("You can delete this code!" when it is not true), so I am guessing this would be considered a bug. If you are referring to the ignore binaries, I can revert the noIssuesReported and all that; I consider it a positive effect, since it leads to standardization and completeness of the tests, but it is not as critical as the actual import-star fix. |
|
Would be great if we could leave in only what's specifically required for the feature you'd like to add. Then we can discuss, track, squash-merge and have release notes generated from (merge) commits separately. |
|
PTALA @webpro |
To clarify, the binary was in the fixture package intentionally to ensure that the fixture code was generating the expected output. |
commit: |
|
Thanks a lot for bearing with me @heystewart! I might make minor edits. Planning to release this tomorrow. |
|
🚀 This pull request is included in v5.42.0. See Release 5.42.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
Using
import * as classes from..., and then doing an iterationfor (... in classes)erroneously marks the imported classes as 'not used'.The import-star path leads to a namespace being added, and the reference to the namespace due to iteration was not being considered, although Object.keys-type accesses were.
While fixing the ...in case, I realized the ...of iteration case was also potentially broken, although it was very difficult to build a test for that particular edge case ... it is highly dependent on the tsconfig options.
Drive-bys:
if (files) continuepattern. Open to suggestions on this one...