Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3ad175

Browse files
committed
Added unit test for multi-ref files and fixed a bug the test revealed
1 parent 37b9319 commit e3ad175

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

lib/typescript/rails/template_handler.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,19 @@ def self.call(template)
7777
def self.replace_relative_references(ts_path, source)
7878
ts_dir = File.dirname(File.expand_path(ts_path))
7979
escaped_dir = ts_dir.gsub(/["\\]/, '\\\\\&') # "\"" => "\\\"", '\\' => '\\\\'
80-
%r!^///\s*<reference\s+path="([^"]+)"\s*/>\s*!.match(source) do |m|
81-
source = source.sub(m.captures[0], File.join(escaped_dir, m.captures[0]))
80+
81+
# Why don't we just use gsub? Because it display odd behavior with File.join on Ruby 2.0
82+
# So we go the long way around.
83+
output = ''
84+
source.each_line do |l|
85+
if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path="([^"]+)"\s*/>\s*!.match(l)).nil?
86+
l = l.sub(m.captures[0], File.join(escaped_dir, m.captures[0]))
87+
end
88+
89+
output = output + l + $/
8290
end
8391

84-
source
92+
output
8593
end
8694
end
8795
end

test/support/site/ref3_1.js.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="ref3_2.ts" />
2+
/// <reference path="ref3_3.ts" />
3+
4+
f1();
5+
f2();

test/support/site/ref3_2.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var f1 = function() {
2+
3+
};

test/support/site/ref3_3.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var f2 = function() {
2+
3+
};

test/template_handler_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SiteController < ActionController::Base
1313
get "site/ref1_2"
1414
get "site/ref2_1"
1515
get "site/ref2_2"
16+
get "site/ref3_1"
1617
end
1718

1819
class TemplateHandlerTest < ActiveSupport::TestCase
@@ -39,4 +40,10 @@ def app
3940
assert_match /f\(1, 2\);\s*/, last_response.body
4041
end
4142

43+
test "<reference> to multiple .ts files works" do
44+
get "/site/ref3_1.js"
45+
assert_match /var f1 = function \(\) \{\s\};\s*var f2 = function \(\) \{\s*\};\s*f1\(\);\s*f2\(\);\s*/,
46+
last_response.body
47+
end
48+
4249
end

0 commit comments

Comments
 (0)