Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Your contribution here.

* [#28](https://github.com/dblock/fui/pull/28): Added support for finding global imports (bracket notation). Added ability to turn off global or local import checks through `-g`, `--ignore-global-imports` or `-l`, `--ignore-local-imports`.- [@jeffctown](https://github.com/jeffctown).

### 0.4.1 (8/16/2017)

* [#24](https://github.com/dblock/fui/pull/24): Support .mm files - [@shachlan](https://github.com/Shachlan).
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,31 @@ fui --path=~/source/project/Name find

#### Find Unused Classes in a Path Skipping Interface Builder (.xib) Files

ForRunning `fui` with `-x` (or `--ignorexib`) will, for example, mark `Foo.h` as unused when `Foo.xib` holds a reference to the `Foo` class and no other references to Foo.h exist.
ForRunning `fui` with `-x` (or `--ignore-xib-files`) will, for example, mark `Foo.h` as unused when `Foo.xib` holds a reference to the `Foo` class and no other references to Foo.h exist.

```
fui -x --path=~/source/project/Name find
```

#### Find Unused Classes in a Path Ignoring Local (quotation syntax) Imports

For Running `fui` with `-l` (or `--ignore-local-imports`) will, for example, mark `Foo.h` as unused when `Bar.h` contains a local import of `Foo.h` (`#import Foo.h`)

```
fui -l --path=~/source/project/Name find

```

#### Find Unused Classes in a Path Ignoring Global (bracket syntax) Imports

For Running `fui` with `-g` (or `--ignore-global-imports`) will, for example, mark `Foo.h` as unused when `Bar.h` contains a global import of `Foo.h` (`#import <Framework/Foo.h>`)

```
fui -g --path=~/source/project/Name find

```


#### Delete All Unused Class Files w/ Prompt

```
Expand Down
4 changes: 3 additions & 1 deletion bin/fui
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ program_desc 'Find unused imports in an Objective-C codebase.'

flag [:p, :path], desc: 'Path to search.', default_value: Dir.pwd
switch [:v, :verbose], desc: 'Produce verbose output.', default_value: false
switch [:x, :ignorexib], desc: 'Ignore interface builder (.xib) files.', default_value: false
switch [:x, :'ignore-xib-files'], desc: 'Ignore interface builder (.xib) files.', default_value: false
switch [:g, :'ignore-global-imports'], desc: 'Ignores imports using a global (angle bracket) format.', default_value: false
switch [:l, :'ignore-local-imports'], desc: 'Ignores imports using a local (quote) format.', default_value: false

default_command :find

Expand Down
20 changes: 18 additions & 2 deletions lib/fui/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,33 @@ def process_code(references, path)
filename = File.basename(path)
headers.each do |header|
filename_without_extension = File.basename(path, File.extname(path))
references[header] << path if filename_without_extension != header.filename_without_extension && File.read(file).include?("#import \"#{header.filename}\"")
file_contents = File.read(file)
global_import_exists = global_imported(file_contents, header)
local_import_exists = local_imported(file_contents, header)
references[header] << path if filename_without_extension != header.filename_without_extension && (local_import_exists || global_import_exists)
end
end
end

def local_imported(file_contents, header)
return false if options['ignore-local-imports']
file_contents.include?("#import \"#{header.filename}\"")
end

def global_imported(file_contents, header)
return false if options['ignore-global-imports']
escaped_header = Regexp.quote(header.filename)
regex = '(#import\s{1}<.+\/' + escaped_header + '>)'
file_contents.match(regex)
end

def process_xml(references, path)
File.open(path) do |file|
yield path if block_given?
headers.each do |header|
filename_without_extension = File.basename(path, File.extname(path))
references[header] << path if (!options['ignorexib'] || filename_without_extension != header.filename_without_extension) && File.read(file).include?("customClass=\"#{header.filename_without_extension}\"")
check_xibs = !options['ignore-xib-files']
references[header] << path if (check_xibs || filename_without_extension != header.filename_without_extension) && File.read(file).include?("customClass=\"#{header.filename_without_extension}\"")
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/global_import/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import <framework/used_class.h>
8 changes: 8 additions & 0 deletions spec/fixtures/global_import/unused_class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// UnusedClass.h
// FUI
//

@interface UnusedClass

@end
10 changes: 10 additions & 0 deletions spec/fixtures/global_import/unused_class.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// UnusedClass.m
// FUI
//

#import "unused_class.h"

@implementation UnusedClass

@end
10 changes: 10 additions & 0 deletions spec/fixtures/global_import/used_class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// ImageView.h
// FUI
//

#import <UIKit/UIKit.h>

@interface ImageView
- (NSString *)fooForBar;
@end
13 changes: 13 additions & 0 deletions spec/fixtures/global_import/used_class.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// UsedClass.m
// FUI
//

#import "used_class.h"

@implementation UsedClass
- (NSString *)fooForBar
{

}
@end
28 changes: 25 additions & 3 deletions spec/fui/finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
end
end
end
context 'ignorexib option set to false' do
context 'ignore-xib-files option set to false' do
before :each do
@fixtures_dir = File.expand_path(File.join(__FILE__, '../../fixtures/nibself'))
end
Expand All @@ -112,15 +112,37 @@
end
end
end
context 'ignorexib option set to true' do
context 'ignore-xib-files option set to true' do
before :each do
@fixtures_dir = File.expand_path(File.join(__FILE__, '../../fixtures/nibself'))
end
describe '#unsed_references' do
it 'finds one unused references' do
finder = Fui::Finder.new(@fixtures_dir, 'ignorexib' => true)
finder = Fui::Finder.new(@fixtures_dir, 'ignore-xib-files' => true)
expect(finder.unused_references.count).to eq(1)
end
end
end
context 'ignore global imports option set to true' do
before :each do
@fixtures_dir = File.expand_path(File.join(__FILE__, '../../fixtures/global_import'))
end
describe '#unused_references' do
it 'finds one unused global reference' do
finder = Fui::Finder.new(@fixtures_dir, 'ignore-global-imports' => false)
expect(Hash[finder.unused_references.map { |k, v| [k.filename, v.count] }]).to eq('header.h' => 0, 'unused_class.h' => 0)
end
end
end
context 'ignore global imports option set to false' do
before :each do
@fixtures_dir = File.expand_path(File.join(__FILE__, '../../fixtures/global_import'))
end
describe '#unused_references' do
it 'finds no unused global references' do
finder = Fui::Finder.new(@fixtures_dir, 'ignore-global-imports' => true)
expect(Hash[finder.unused_references.map { |k, v| [k.filename, v.count] }]).to eq('header.h' => 0, 'unused_class.h' => 0, 'used_class.h' => 0)
end
end
end
end