Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore _Previews and add new command line parameters #1

Closed
wants to merge 1 commit into from
Closed
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
83 changes: 57 additions & 26 deletions unused.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def modifiers
@modifiers = match.captures[0].split(" ")
end
return @modifiers
end
end

def name
def name
@name
end
end

def file
@file
end
end

def to_s
serialize
Expand All @@ -40,13 +40,13 @@ def to_str

def full_file_path
Dir.pwd + '/' + @file
end
end

def serialize
"Item< #{@type.to_s.green} #{@name.to_s.yellow} [#{modifiers.join(" ").cyan}] from: #{@file}:#{@at}:0>"
end
end

def to_xcode
def to_xcode
"#{full_file_path}:#{@at}:0: warning: #{@type.to_s} #{@name.to_s} is unused"
end

Expand All @@ -69,7 +69,7 @@ def find
# Usage within the file
if private_items.length > 0
find_usages_in_files([my_text_file], [], private_items)
end
end

}

Expand All @@ -84,12 +84,41 @@ def find
storyboards = Dir.glob("**/*.storyboard")

find_usages_in_files(all_files, xibs + storyboards, items)
end

def ignore_names_with_regexps(files, regexps)
files.select { |f| regexps.all? { |r| Regexp.new(r).match(f.name).nil? } }
end

end
def ignoring_regexp_names_from_command_line_args
regexps = []
should_skip_predefined_ignore_names = false

arguments = ARGV.clone
until arguments.empty?
item = arguments.shift
if item == "--ignore-name"
regex = arguments.shift
regexps += [regex]
end

if item == "--skip-predefined-ignore-names"
should_skip_predefined_ignore_names = true
end
end

if not should_skip_predefined_ignore_names
regexps += [
"_Previews",
]
end

regexps
end

def ignore_files_with_regexps(files, regexps)
files.select { |f| regexps.all? { |r| Regexp.new(r).match(f.file).nil? } }
end
end

def ignoring_regexps_from_command_line_args
regexps = []
Expand All @@ -101,12 +130,12 @@ def ignoring_regexps_from_command_line_args
if item == "--ignore"
regex = arguments.shift
regexps += [regex]
end
end

if item == "--skip-predefined-ignores"
should_skip_predefined_ignores = true
end
end
end
end

if not should_skip_predefined_ignores
regexps += [
Expand All @@ -116,10 +145,10 @@ def ignoring_regexps_from_command_line_args
"Spec.swift$",
"Tests/"
]
end
end

regexps
end
end

def find_usages_in_files(files, xibs, items_in)
items = items_in
Expand All @@ -131,13 +160,13 @@ def find_usages_in_files(files, xibs, items_in)

wf = Hash[*words_arrray]

items.each_with_index { |f, i|
items.each_with_index { |f, i|
usages[i] += (wf[f.name] || 0)
}
# Remove all items which has usage 2+
indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i }

# reduce usage array if we found some functions already
# reduce usage array if we found some functions already
indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) }
}

Expand All @@ -149,42 +178,44 @@ def find_usages_in_files(files, xibs, items_in)

wf = Hash[*classes_array]

items.each_with_index { |f, i|
items.each_with_index { |f, i|
usages[i] += (wf[f.name] || 0)
}
# Remove all items which has usage 2+
indexes = usages.each_with_index.select { |u, i| u >= 2 }.map { |f, i| i }

# reduce usage array if we found some functions already
# reduce usage array if we found some functions already
indexes.reverse.each { |i| usages.delete_at(i) && items.delete_at(i) }

}

regexps = ignoring_regexps_from_command_line_args()

items = ignore_files_with_regexps(items, regexps)

regexpnames = ignoring_regexp_names_from_command_line_args()
items = ignore_names_with_regexps(items, regexpnames)

if items.length > 0
if ARGV[0] == "xcode"
$stderr.puts "#{items.map { |e| e.to_xcode }.join("\n")}"
else
puts "#{items.map { |e| e.to_s }.join("\n ")}"
end
end
end
end
end

def grab_items(file)
lines = File.readlines(file).map {|line| line.gsub(/^\s*\/\/.*/, "") }
items = lines.each_with_index.select { |line, i| line[/(func|let|var|class|enum|struct|protocol)\s+\w+/] }.map { |line, i| Item.new(file, line, i)}
end
end

def filter_items(items)
items.select { |f|
items.select { |f|
!f.name.start_with?("test") && !f.modifiers.include?("@IBAction") && !f.modifiers.include?("override") && !f.modifiers.include?("@objc") && !f.modifiers.include?("@IBInspectable")
}
end

end
end

class String
def black; "\e[30m#{self}\e[0m" end
Expand Down Expand Up @@ -213,4 +244,4 @@ def reverse_color; "\e[7m#{self}\e[27m" end
end


Unused.new.find
Unused.new.find