peripheryapp / periphery

A tool to identify unused code in Swift projects.

Repository from Github https://github.comperipheryapp/peripheryRepository from Github https://github.comperipheryapp/periphery

Parse connections from Interface Builder files.

ileitch opened this issue · comments

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

commented

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.

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.

commented

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?

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?

commented

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

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.

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.

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?