Skip to content

Commit

Permalink
DEV: add script to list discourse JS deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
tyb-talks committed May 27, 2024
1 parent 351c371 commit 49a73fa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions scripts/list_discourse_deprecations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true
require "find"
require "json"

# TODO: ids not found by this script:
# discourse.pretty-text.registerOption

class DiscourseDeprecationFinder
EXCLUDED_DIR_PATTERNS =
%r{(/app/assets/javascripts/discourse/tests/unit/|/discourse/tmp/|/discourse/node_modules/|/discourse/dist/|/discourse/vendor/|/discourse/public/|/discourse/spec/)}
DEPRECATED_FUNCTION_PATTERN = /deprecated\([^)]+\{[^}]*id:\s*"([^"]+)"[^}]*\}/
DEPRECATED_FUNCTION_PATTERN_2 = /deprecated\(\s*`[^`]*`\s*,\s*\{[^}]*id:\s*"([^"]+)"[^}]*\}/m
DEPRECATED_FUNCTION_INDIRECT_PATTERN = /deprecated\(\.\.\.([A-Za-z0-9_]+)\)/ # see discourse.d-button-action-string

def initialize(repo)
@repo = repo
@matched_ids = []
@alt_matched_ids = []
@indirect_cases = []
end

def find_deprecations
puts "Starting search for deprecations"

Find.find(@repo) do |path|
if path =~ EXCLUDED_DIR_PATTERNS
Find.prune
next
end

next unless path.end_with?(".js", ".gjs") && File.file?(path)

file_content = File.read(path)

file_content.scan(DEPRECATED_FUNCTION_PATTERN) { |match| @matched_ids << match.first }

file_content.scan(DEPRECATED_FUNCTION_PATTERN_2) { |match| @alt_matched_ids << match.first }

file_content.scan(DEPRECATED_FUNCTION_INDIRECT_PATTERN) do |match|
@indirect_cases << match.first
end
end

# Output the unique matched id values
puts "Found #{@matched_ids.length} ids:"
puts @matched_ids.uniq.sort

# Output diff from alt matching pattern:
diff_ids = @alt_matched_ids.uniq - @matched_ids.uniq
puts "\nFound additional #{diff_ids.length} ids with alt pattern"
puts diff_ids.sort

# Output the indirect cases for further troubleshooting since these would be
# variables used to store the actual deprecation key
unless @indirect_cases.empty?
puts "\nIndirect deprecated function calls for further troubleshooting:"
puts @indirect_cases.uniq.sort
end
end
end

if ARGV.empty?
puts "Usage: ruby list_discourse_deprecations.rb <CODEBASE_DIR>"
exit
end
DiscourseDeprecationFinder.new(ARGV[0]).find_deprecations

# TO REFACTOR: from CI workflow, add into autogenerated PR description
# a reminder to check for indirect pattern and
# if theere's any keys without discourse prefix, to fix that in core
File renamed without changes.

0 comments on commit 49a73fa

Please sign in to comment.