Skip to content

Commit f81c0ba

Browse files
committed
Merge pull request #9 from tkawachi/expand-relative
Expand relative paths in /// <reference path="..." /> before compile
2 parents 4356a70 + 6381707 commit f81c0ba

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Diff for: lib/typescript/rails/template_handler.rb

+21-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def prepare
4141
end
4242

4343
def evaluate(scope, locals, &block)
44-
@output ||= TypeScript::Node.compile(data)
44+
source = Typescript::Rails.replace_relative_references(file, data)
45+
@output ||= TypeScript::Node.compile(source)
4546
end
4647

4748
def allows_script?
@@ -57,9 +58,27 @@ def self.erb_handler
5758

5859
def self.call(template)
5960
compiled_source = erb_handler.call(template)
60-
"TypeScript::Node.compile(begin;#{compiled_source};end)"
61+
escaped_path = template.identifier.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\'
62+
<<-EOS
63+
TypeScript::Node.compile(
64+
Typescript::Rails.replace_relative_references(
65+
'#{escaped_path}', (begin;#{compiled_source};end)
66+
)
67+
)
68+
EOS
6169
end
6270
end
71+
72+
# Replace relative paths specified in /// <reference path="..." /> with absolute paths.
73+
#
74+
# @param [String] ts_path Source .ts path
75+
# @param [String] ts source. It might be pre-processed by erb.
76+
# @return [String] replaces source
77+
def self.replace_relative_references(ts_path, source)
78+
ts_dir = File.dirname(File.expand_path(ts_path))
79+
escaped_dir = ts_dir.gsub(/["\\]/, '\\\\\&') # "\"" => "\\\"", '\\' => '\\\\'
80+
source.gsub(%r!(^///\s*<reference\s+path=")([^/"][^"]+)("\s*/>)!, '\1' + File.join(escaped_dir, '\2') + '\3')
81+
end
6382
end
6483
end
6584

Diff for: test/support/site/ref1_2.js.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
/// <reference path="<%= File.join(File.dirname(__FILE__), "ref1_1.js.ts") %>" />
1+
/// <reference path="ref1_1.js.ts" />
22

33
f(1, 2)

Diff for: test/support/site/ref2_2.js.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
/// <reference path="<%= File.join(File.dirname(__FILE__), "ref2_1.d.ts") %>" />
1+
/// <reference path="./ref2_1.d.ts" />
22

33
f(1, 2)

0 commit comments

Comments
 (0)