peripheryapp / periphery

A tool to identify unused code in Swift projects.
MIT License
5.13k stars 184 forks source link

Parse connections from Interface Builder files. #212

Open ileitch opened 3 years ago

ileitch commented 3 years ago

This is need so that we can identify individual unused @IB* declarations.

5sw commented 3 years ago

I would like to see that as well. Not just for actions and outlets, but also for classes.

Maybe even distinguishing between top level objects and views nested deeper in the hierarchy. If a class referenced by a top level object from a nib is otherwise unused the chances are good that we can remove the whole class and the nib.

ileitch commented 3 years ago

Classes referenced by nibs are already parsed out, this issue is only for the IB declarations. Currently what Periphery does is find all classes referenced in a nib, and then blindly retains all of its IB declarations. So if you've a view controller no longer referenced by a nib, Periphery should detect that it's unused, provided it's not also referenced by other code.

5sw commented 3 years ago

I see. Currently I have this case: I have a UIView subclass that is placed inside a xib file, but not referenced by code at all. There are also no IBActions or IBOutlets. Periphery marks this class as unused. If periphery is parsing IB files anyways it should be easy to mark this class as used too. Or do you think this should be a separate issue?

ileitch commented 3 years ago

Yes that sounds like a bug. Can you show me the contents of the xib? Also, is the xib a member of the build targets passed to --targets?

5sw commented 3 years ago

That was it, I forgot to add the resource bundle target. This also resolved my issues from #234.

monchote commented 3 years ago

If periphery is parsing IB files anyways it should be easy to mark this class as used too.

Is there a way to get the opposite behaviour?

I'm working on a project where every UIView or UIViewController has an associated xib file. For example: as the UIView subclass declared in MyView.swift is referenced by MyView.xib, Periphery doesn't mark MyView as unused, even though it never gets instantiated anywhere else in the codebase.

I've tried --index-exclude "*.xib" but it didn't help.

ileitch commented 3 years ago

Yeah index excluding is only hooked up for Swift files currently. Could you open a new issue for this, and I'll implement it when I get a chance.

KRUBERLICK commented 1 day ago

I have an SPM target with a .xib and a corresponding View class as the nib's File Owner. All of @IBActions are marked as unused after the scan. All targets are included via the all choice in periphery scan --setup. Can anyone help?