Skip to content

Commit cda9a31

Browse files
justin808claude
andcommitted
Refactor doctor async checks: extract constants and improve logging
Extract comment pattern regexes to named constants for better maintainability and reusability. Add defensive log_debug helper method to safely handle Rails.logger access. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 4cdff17 commit cda9a31

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/react_on_rails/doctor.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,11 @@ def safe_display_config_value(label, config, method_name)
11481148
end
11491149
end
11501150

1151+
# Comment patterns used for filtering out commented async usage
1152+
ERB_COMMENT_PATTERN = /<%\s*#.*javascript_pack_tag/
1153+
HAML_COMMENT_PATTERN = /^\s*-#.*javascript_pack_tag/
1154+
HTML_COMMENT_PATTERN = /<!--.*javascript_pack_tag/
1155+
11511156
def check_async_usage
11521157
# When Pro is installed, async is fully supported and is the default behavior
11531158
# No need to check for async usage in this case
@@ -1186,7 +1191,7 @@ def scan_view_files_for_async_pack_tag
11861191
files_with_async.compact
11871192
rescue Errno::ENOENT, Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError => e
11881193
# Log the error if Rails logger is available
1189-
Rails.logger.debug("Error scanning view files for async: #{e.message}") if defined?(Rails) && Rails.logger
1194+
log_debug("Error scanning view files for async: #{e.message}")
11901195
[]
11911196
end
11921197

@@ -1232,12 +1237,12 @@ def content_has_only_commented_async?(content)
12321237
# Skip lines that don't contain javascript_pack_tag
12331238
next unless line.include?("javascript_pack_tag")
12341239

1235-
# Skip ERB comments (<%# ... %>) - matches ERB comment opening with optional whitespace
1236-
next if line.match?(/<%\s*#.*javascript_pack_tag/)
1237-
# Skip HAML comments (-# ...) - matches line-starting HAML comments
1238-
next if line.match?(/^\s*-#.*javascript_pack_tag/)
1239-
# Skip HTML comments (<!-- ... -->) - matches complete HTML comment blocks
1240-
next if line.match?(/<!--.*javascript_pack_tag/)
1240+
# Skip ERB comments (<%# ... %>)
1241+
next if line.match?(ERB_COMMENT_PATTERN)
1242+
# Skip HAML comments (-# ...)
1243+
next if line.match?(HAML_COMMENT_PATTERN)
1244+
# Skip HTML comments (<!-- ... -->)
1245+
next if line.match?(HTML_COMMENT_PATTERN)
12411246

12421247
# If we reach here, this line has an uncommented javascript_pack_tag
12431248
true
@@ -1263,9 +1268,15 @@ def config_has_async_loading_strategy?
12631268
end
12641269
rescue Errno::ENOENT, Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError => e
12651270
# Log the error if Rails logger is available
1266-
Rails.logger.debug("Error checking async loading strategy: #{e.message}") if defined?(Rails) && Rails.logger
1271+
log_debug("Error checking async loading strategy: #{e.message}")
12671272
false
12681273
end
1274+
1275+
def log_debug(message)
1276+
return unless defined?(Rails.logger) && Rails.logger
1277+
1278+
Rails.logger.debug(message)
1279+
end
12691280
end
12701281
# rubocop:enable Metrics/ClassLength
12711282
end

0 commit comments

Comments
 (0)