-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: add script to list discourse JS deprecations
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.