Skip to content

Updated module_name regexp to not match incorrect indexes #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/javascript/controllers/special_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Sorry - no imports here!")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Sorry, nothing helpful here")
8 changes: 8 additions & 0 deletions test/importmap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down