Skip to content

Commit 697269c

Browse files
David Juddschneems
authored andcommitted
PathUtils: *always* prefer longer to shorter extension match
The older logic would *usually* prefer a longer to a shorter extension match, but not in the case with a three-or-more-component extension when one of the intermediate extensions wasn't recognized, e.g. ".coffee" would be preferred over ".js.jsx.coffee" unless ".jsx.coffee" was also a recognized extension. (".js.jsx.coffee" is the preferred Coffee+React extension with the react-rails gem)
1 parent dd4191a commit 697269c

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/sprockets/path_utils.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,19 @@ def path_extnames(path)
148148
#
149149
# Returns [String extname, Object value] or nil nothing matched.
150150
def match_path_extname(path, extensions)
151-
match, key = nil, ""
152-
path_extnames(path).reverse_each do |extname|
153-
key.prepend(extname)
154-
if value = extensions[key]
155-
match = [key.dup, value]
156-
elsif match
157-
break
151+
basename = File.basename(path)
152+
153+
i = basename.index('.'.freeze)
154+
while i && i < basename.length - 1
155+
extname = basename[i..-1]
156+
if value = extensions[extname]
157+
return extname, value
158158
end
159+
160+
i = basename.index('.'.freeze, i+1)
159161
end
160-
match
162+
163+
nil
161164
end
162165

163166
# Internal: Returns all parents for path

test/test_path_utils.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ def test_match_path_extname
170170
refute match_path_extname("jquery.map", extensions)
171171
refute match_path_extname("jquery.map.js", extensions)
172172
refute match_path_extname("jquery.map.css", extensions)
173+
174+
extensions = { ".coffee" => "application/coffeescript", ".js" => "application/javascript", ".js.jsx.coffee" => "application/jsx+coffee" }
175+
assert_equal [".js.jsx.coffee", "application/jsx+coffee"], match_path_extname("component.js.jsx.coffee", extensions)
173176
end
174177

175178
def test_path_parents

0 commit comments

Comments
 (0)