Skip to content

Commit 1f61137

Browse files
justin808claude
andcommitted
Add Slim template support to async detection
Extended the async detection functionality to support Slim templates in addition to ERB and HAML: - Added SLIM_COMMENT_PATTERN constant for Slim comment syntax (/) - Added app/views/**/*.slim to view file scanning patterns - Included Slim comment filtering in content_has_only_commented_async? - Added comprehensive test cases for Slim files: * Slim files with uncommented :async (should detect) * Slim files with commented :async (should not detect) - Added slim stub mocks to all existing test cases for consistency This ensures users of Slim templates get the same async detection warnings as ERB and HAML users. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent eb0b353 commit 1f61137

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/react_on_rails/doctor.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ def safe_display_config_value(label, config, method_name)
11511151
# Comment patterns used for filtering out commented async usage
11521152
ERB_COMMENT_PATTERN = /<%\s*#.*javascript_pack_tag/
11531153
HAML_COMMENT_PATTERN = /^\s*-#.*javascript_pack_tag/
1154+
SLIM_COMMENT_PATTERN = %r{^\s*/.*javascript_pack_tag}
11541155
HTML_COMMENT_PATTERN = /<!--.*javascript_pack_tag/
11551156

11561157
def check_async_usage
@@ -1186,7 +1187,7 @@ def check_async_usage
11861187
end
11871188

11881189
def scan_view_files_for_async_pack_tag
1189-
view_patterns = ["app/views/**/*.erb", "app/views/**/*.haml"]
1190+
view_patterns = ["app/views/**/*.erb", "app/views/**/*.haml", "app/views/**/*.slim"]
11901191
files_with_async = view_patterns.flat_map { |pattern| scan_pattern_for_async(pattern) }
11911192
files_with_async.compact
11921193
rescue Errno::ENOENT, Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError => e
@@ -1235,6 +1236,7 @@ def content_has_only_commented_async?(content)
12351236
uncommented_lines = content.each_line.reject do |line|
12361237
line.match?(ERB_COMMENT_PATTERN) ||
12371238
line.match?(HAML_COMMENT_PATTERN) ||
1239+
line.match?(SLIM_COMMENT_PATTERN) ||
12381240
line.match?(HTML_COMMENT_PATTERN)
12391241
end
12401242

spec/lib/react_on_rails/doctor_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
before do
221221
allow(Dir).to receive(:glob).with("app/views/**/*.erb").and_return(["app/views/layouts/application.html.erb"])
222222
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
223+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
223224
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
224225
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
225226
.and_return('<%= javascript_pack_tag "application", :async %>')
@@ -279,6 +280,7 @@
279280
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
280281
.and_return(["app/views/layouts/application.html.erb"])
281282
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
283+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
282284
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
283285
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
284286
.and_return('<%= javascript_pack_tag "app", :async %>')
@@ -297,6 +299,7 @@
297299
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
298300
.and_return(["app/views/layouts/application.html.erb"])
299301
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
302+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
300303
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
301304
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
302305
.and_return('<%= javascript_pack_tag "app", async: true %>')
@@ -315,6 +318,7 @@
315318
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
316319
.and_return(["app/views/layouts/application.html.erb"])
317320
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
321+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
318322
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
319323
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
320324
.and_return('<%= javascript_pack_tag "app", defer: "async" %>')
@@ -331,6 +335,7 @@
331335
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
332336
.and_return(["app/views/layouts/application.html.erb"])
333337
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
338+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
334339
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
335340
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
336341
.and_return('<%= javascript_pack_tag "app" %>')
@@ -347,6 +352,7 @@
347352
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
348353
.and_return(["app/views/layouts/application.html.erb"])
349354
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
355+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
350356
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
351357
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
352358
.and_return('<%= javascript_pack_tag "app", defer: true %>
@@ -364,6 +370,7 @@
364370
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
365371
.and_return(["app/views/layouts/application.html.erb"])
366372
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
373+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
367374
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
368375
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
369376
.and_return('<%# javascript_pack_tag "app", :async %>')
@@ -380,6 +387,7 @@
380387
allow(Dir).to receive(:glob).with("app/views/**/*.erb").and_return([])
381388
allow(Dir).to receive(:glob).with("app/views/**/*.haml")
382389
.and_return(["app/views/layouts/application.html.haml"])
390+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
383391
allow(File).to receive(:exist?).with("app/views/layouts/application.html.haml").and_return(true)
384392
allow(File).to receive(:read).with("app/views/layouts/application.html.haml")
385393
.and_return('-# javascript_pack_tag "app", :async')
@@ -396,6 +404,8 @@
396404
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
397405
.and_return(["app/views/layouts/application.html.erb"])
398406
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
407+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
408+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
399409
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
400410
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
401411
.and_return('<!-- <%= javascript_pack_tag "app", :async %> -->')
@@ -407,11 +417,50 @@
407417
end
408418
end
409419

420+
context "when async is only in Slim comments" do
421+
before do
422+
allow(Dir).to receive(:glob).with("app/views/**/*.erb").and_return([])
423+
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
424+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
425+
allow(Dir).to receive(:glob).with("app/views/**/*.slim")
426+
.and_return(["app/views/layouts/application.html.slim"])
427+
allow(File).to receive(:exist?).with("app/views/layouts/application.html.slim").and_return(true)
428+
allow(File).to receive(:read).with("app/views/layouts/application.html.slim")
429+
.and_return('/ = javascript_pack_tag "app", :async')
430+
end
431+
432+
it "returns empty array" do
433+
files = doctor.send(:scan_view_files_for_async_pack_tag)
434+
expect(files).to be_empty
435+
end
436+
end
437+
438+
context "when view files contain Slim javascript_pack_tag with :async" do
439+
before do
440+
allow(Dir).to receive(:glob).with("app/views/**/*.erb").and_return([])
441+
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
442+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
443+
allow(Dir).to receive(:glob).with("app/views/**/*.slim")
444+
.and_return(["app/views/layouts/application.html.slim"])
445+
allow(File).to receive(:exist?).with("app/views/layouts/application.html.slim").and_return(true)
446+
allow(File).to receive(:read).with("app/views/layouts/application.html.slim")
447+
.and_return('= javascript_pack_tag "app", :async')
448+
allow(doctor).to receive(:relativize_path).with("app/views/layouts/application.html.slim")
449+
.and_return("app/views/layouts/application.html.slim")
450+
end
451+
452+
it "returns files with async" do
453+
files = doctor.send(:scan_view_files_for_async_pack_tag)
454+
expect(files).to include("app/views/layouts/application.html.slim")
455+
end
456+
end
457+
410458
context "when javascript_pack_tag spans multiple lines" do
411459
before do
412460
allow(Dir).to receive(:glob).with("app/views/**/*.erb")
413461
.and_return(["app/views/layouts/application.html.erb"])
414462
allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([])
463+
allow(Dir).to receive(:glob).with("app/views/**/*.slim").and_return([])
415464
allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true)
416465
allow(File).to receive(:read).with("app/views/layouts/application.html.erb")
417466
.and_return("<%= javascript_pack_tag \"app\",\n :async %>")

0 commit comments

Comments
 (0)