Skip to content

Commit de78697

Browse files
committed
Merge pull request #11 from brainling/master
Updated with two fixes for Rails 4 and Ruby 2.0.
2 parents 46ebcd4 + b67eea9 commit de78697

File tree

9 files changed

+39
-6
lines changed

9 files changed

+39
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea/*
12
*.gem
23
*.rbc
34
.bundle

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
rvm:
22
- 1.9.2
33
- 1.9.3
4+
- 2.0.0
45
- ree
56
- jruby
67
- rbx

lib/typescript/rails/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Railtie < ::Rails::Railtie
44
config.before_initialize do |app|
55
require 'typescript-rails'
66

7-
if app.config.assets.enabled
7+
if ::Rails::VERSION::MAJOR >= 4 || app.config.assets.enabled
88
require 'sprockets'
99
require 'sprockets/engines'
1010
Sprockets.register_engine '.ts', Typescript::Rails::TypeScriptTemplate

lib/typescript/rails/template_handler.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +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-
source.gsub(%r!(^///\s*<reference\s+path=")([^/"][^"]+)("\s*/>)!, '\1' + File.join(escaped_dir, '\2') + '\3')
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 + $/
90+
end
91+
92+
output
8193
end
8294
end
8395
end

lib/typescript/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Typescript
22
module Rails
3-
VERSION = "0.1.2"
3+
VERSION = "0.1.3"
44
end
55
end

test/support/site/ref3_1.js.ts

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var f1 = function() {
2+
3+
};

test/support/site/ref3_3.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var f2 = function() {
2+
3+
};

test/template_handler_test.rb

Lines changed: 11 additions & 3 deletions
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
@@ -25,17 +26,24 @@ def app
2526
test "typescript views are served as javascript" do
2627
get "/site/index.js"
2728

28-
assert_match "var x = 5;\r\n", last_response.body
29+
s = last_response.body
30+
assert_match /var x = 5;\s*/, last_response.body
2931
end
3032

3133
test "<reference> to other .ts file works" do
3234
get "/site/ref1_2.js"
33-
assert_match "var f = function (x, y) {\r\n return x + y;\r\n};\r\nf(1, 2);\r\n", last_response.body
35+
assert_match /var f = function \(x, y\) \{\s*return x \+ y;\s*\};\s*f\(1, 2\);\s*/, last_response.body
3436
end
3537

3638
test "<reference> to other .d.ts file works" do
3739
get "/site/ref2_2.js"
38-
assert_match "f(1, 2);\r\n", last_response.body
40+
assert_match /f\(1, 2\);\s*/, last_response.body
41+
end
42+
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
3947
end
4048

4149
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy