From 551bf23b161591afd122e620d30b892b9ab68c90 Mon Sep 17 00:00:00 2001 From: Adam Garrett-Harris Date: Mon, 31 Oct 2022 14:29:38 -0600 Subject: [PATCH] Ignore _Previews and add new command line parameters --- unused.rb | 83 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/unused.rb b/unused.rb index 579f2c2..c32d23e 100755 --- a/unused.rb +++ b/unused.rb @@ -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 @@ -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 @@ -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 } @@ -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 = [] @@ -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 += [ @@ -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 @@ -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) } } @@ -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 @@ -213,4 +244,4 @@ def reverse_color; "\e[7m#{self}\e[27m" end end -Unused.new.find \ No newline at end of file +Unused.new.find