From e925965330d6a1deae3bd340e777344ffaf8fc8b Mon Sep 17 00:00:00 2001 From: Kerri Miller Date: Wed, 6 Dec 2023 15:50:03 -0800 Subject: [PATCH 1/7] Add linter to find unused helper methods This script attempts to detect unused helper methods. --- scripts/lint/unused_helper_methods.rb | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 scripts/lint/unused_helper_methods.rb diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb new file mode 100755 index 00000000000000..908d16300168b4 --- /dev/null +++ b/scripts/lint/unused_helper_methods.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'parallel' + +start = Time.now + +# Build an array of filename globs to process. +# Only search file types that might use or define a helper. +# +extensions = %w[rb haml erb].map { |ext| "{ee/,}app/**/*.#{ext}" } + +# Build a hash of all the source files to search. +# Key is filename, value is an array of the lines. +# +source_files = {} + +Dir.glob(extensions).each do |filename| + source_files[filename] = File.readlines(filename) +end + +# Build an array of {method, file} hashes defined in app/helper/* files. +# +helpers = source_files.keys.grep(%r{app/helpers}).flat_map do |filename| + source_files[filename].flat_map do |line| + line =~ /def ([^(\s]+)/ ? [{ method: Regexp.last_match(1).chomp, file: filename }] : [] + end +end + +puts "Scanning #{source_files.size} files for #{helpers.size} helpers..." + +# Combine all the source code into one big string, because regex are fast. +# +source_code = source_files.values.flatten.join + +# Iterate over all the helpers and reject any that appear anywhere in the complete source. +# +unused = Parallel.flat_map(helpers, progress: 'Checking helpers') do |helper| + /(? Date: Thu, 7 Dec 2023 10:35:47 -0800 Subject: [PATCH 2/7] Add attribute to OSS source --- scripts/lint/unused_helper_methods.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb index 908d16300168b4..e9e5ecea926d42 100755 --- a/scripts/lint/unused_helper_methods.rb +++ b/scripts/lint/unused_helper_methods.rb @@ -1,6 +1,9 @@ #!/usr/bin/env ruby # frozen_string_literal: true +# Inspired in part by https://gist.github.com/kylefox/617b0bead5f53dc53a224a8651328c92 +# + require 'parallel' start = Time.now -- GitLab From 7f80aa9f1a82d8729f4293b063c7e5278ed96da2 Mon Sep 17 00:00:00 2001 From: Peter Leitzen Date: Mon, 11 Dec 2023 22:56:55 +0000 Subject: [PATCH 3/7] Fence method and source names --- scripts/lint/unused_helper_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb index e9e5ecea926d42..a2f1782ebc0953 100755 --- a/scripts/lint/unused_helper_methods.rb +++ b/scripts/lint/unused_helper_methods.rb @@ -46,7 +46,7 @@ if unused puts "\nFound #{unused.size} unused helpers:\n\n" - unused.each { |helper| puts " - [ ] #{helper[:file]}: #{helper[:method]}" } + unused.each { |helper| puts " - [ ] `#{helper[:file]}`: `#{helper[:method]}`" } puts "\n" else puts 'No unused helpers were found.' -- GitLab From cb71d635a4d0d683a4fefa8cf71bd4e18fee0bc7 Mon Sep 17 00:00:00 2001 From: Kerri Miller Date: Thu, 21 Dec 2023 10:41:57 -0800 Subject: [PATCH 4/7] Use Process for timing --- scripts/lint/unused_helper_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb index a2f1782ebc0953..2cebff688a7aa1 100755 --- a/scripts/lint/unused_helper_methods.rb +++ b/scripts/lint/unused_helper_methods.rb @@ -6,7 +6,7 @@ require 'parallel' -start = Time.now +start = Process.clock_gettime(Process::CLOCK_MONOTONIC) # Build an array of filename globs to process. # Only search file types that might use or define a helper. @@ -42,7 +42,7 @@ /(? Date: Thu, 21 Dec 2023 11:56:08 -0800 Subject: [PATCH 5/7] Escape \? to improve matching --- scripts/lint/unused_helper_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb index 2cebff688a7aa1..a00a36c9da127b 100755 --- a/scripts/lint/unused_helper_methods.rb +++ b/scripts/lint/unused_helper_methods.rb @@ -39,7 +39,7 @@ # Iterate over all the helpers and reject any that appear anywhere in the complete source. # unused = Parallel.flat_map(helpers, progress: 'Checking helpers') do |helper| - /(? Date: Thu, 21 Dec 2023 12:01:25 -0800 Subject: [PATCH 6/7] Remove 'self.' from method name when scanning for usage --- scripts/lint/unused_helper_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb index a00a36c9da127b..69fc3ca4813cfe 100755 --- a/scripts/lint/unused_helper_methods.rb +++ b/scripts/lint/unused_helper_methods.rb @@ -39,7 +39,7 @@ # Iterate over all the helpers and reject any that appear anywhere in the complete source. # unused = Parallel.flat_map(helpers, progress: 'Checking helpers') do |helper| - /(? Date: Thu, 21 Dec 2023 12:20:39 -0800 Subject: [PATCH 7/7] More exact match for method name --- scripts/lint/unused_helper_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint/unused_helper_methods.rb b/scripts/lint/unused_helper_methods.rb index 69fc3ca4813cfe..b7b651614ef005 100755 --- a/scripts/lint/unused_helper_methods.rb +++ b/scripts/lint/unused_helper_methods.rb @@ -39,7 +39,7 @@ # Iterate over all the helpers and reject any that appear anywhere in the complete source. # unused = Parallel.flat_map(helpers, progress: 'Checking helpers') do |helper| - /(?