From eae70a3c98b9e8453c85844d7726adf12035564e Mon Sep 17 00:00:00 2001 From: Caleb Owens Date: Sat, 27 Jan 2024 18:19:19 +0000 Subject: [PATCH] Updated module_name regexp to not match incorrect indexes --- lib/importmap/map.rb | 12 +++++++++++- .../app/javascript/controllers/special_index.js | 1 + .../app/javascript/helpers/requests/special_index.js | 1 + test/importmap_test.rb | 8 ++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/dummy/app/javascript/controllers/special_index.js create mode 100644 test/dummy/app/javascript/helpers/requests/special_index.js diff --git a/lib/importmap/map.rb b/lib/importmap/map.rb index 6aa4f4d..43f0421 100644 --- a/lib/importmap/map.rb +++ b/lib/importmap/map.rb @@ -141,7 +141,17 @@ def expand_directories_into(paths) end def module_name_from(filename, mapping) - [ mapping.under, filename.to_s.remove(filename.extname).remove(/\/?index$/).presence ].compact.join("/") + # Regex explanation: + # (?:\/|^) # Matches either / OR the start of the string + # index # Matches the word index + # $ # Matches the end of the string + # + # Sample matches + # index + # folder/index + index_regex = /(?:\/|^)index$/ + + [ mapping.under, filename.to_s.remove(filename.extname).remove(index_regex).presence ].compact.join("/") end def module_path_from(filename, mapping) diff --git a/test/dummy/app/javascript/controllers/special_index.js b/test/dummy/app/javascript/controllers/special_index.js new file mode 100644 index 0000000..5b418da --- /dev/null +++ b/test/dummy/app/javascript/controllers/special_index.js @@ -0,0 +1 @@ +console.log("Sorry - no imports here!") diff --git a/test/dummy/app/javascript/helpers/requests/special_index.js b/test/dummy/app/javascript/helpers/requests/special_index.js new file mode 100644 index 0000000..0758b5a --- /dev/null +++ b/test/dummy/app/javascript/helpers/requests/special_index.js @@ -0,0 +1 @@ +console.log("Sorry, nothing helpful here") diff --git a/test/importmap_test.rb b/test/importmap_test.rb index bf1bd1e..15c2290 100644 --- a/test/importmap_test.rb +++ b/test/importmap_test.rb @@ -44,10 +44,18 @@ def setup assert_match %r|assets/controllers/index.*\.js|, generate_importmap_json["imports"]["controllers"] end + test "directory pin mounted under matching subdir doesn't map *_index as root" do + assert_match %r|assets/controllers/special_index.*\.js|, generate_importmap_json["imports"]["controllers/special_index"] + end + test "directory pin mounted under matching subdir maps index as root at second depth" do assert_match %r|assets/helpers/requests/index.*\.js|, generate_importmap_json["imports"]["helpers/requests"] end + test "directory pin mounted under matching subdir doesn't map *_index as root at second depth" do + assert_match %r|assets/helpers/requests/special_index.*\.js|, generate_importmap_json["imports"]["helpers/requests/special_index"] + end + test "directory pin under custom asset path" do assert_match %r|assets/spina/controllers/another_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/another_controller"] assert_match %r|assets/spina/controllers/deeper/again_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/deeper/again_controller"]