Skip to content

Commit 9f97a06

Browse files
committed
- Cleaned up jsx_test and moved JSXTransformer test to its own class
- Cleaned up Babel transformer test - Refactored and cleaned up Babel Transformer
1 parent 8eb1dd6 commit 9f97a06

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

lib/react/jsx/babel_transformer.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@ module React
33
module JSX
44
class BabelTransformer
55
DEPRECATED_OPTIONS = [:harmony, :strip_types, :asset_path]
6-
6+
DEFAULT_TRANSFORM_OPTIONS = { blacklist: ['spec.functionName', 'validation.react'] }
77
def initialize(options)
8-
_options = options.dup
9-
has_old_opts = DEPRECATED_OPTIONS.map do |option|
10-
_options.delete option
11-
end.any?
12-
13-
if has_old_opts
8+
if (options.keys & DEPRECATED_OPTIONS).any?
149
ActiveSupport::Deprecation.warn("Setting config.react.jsx_transform_options for :harmony, :strip_types, and :asset_path keys is now deprecated and has no effect with the default Babel Transformer."+
1510
"Please use new Babel Transformer options :whitelist, :plugin instead.")
1611
end
1712

18-
@transform_options = {
19-
blacklist: ['spec.functionName', 'validation.react']
20-
}.merge(_options)
13+
DEFAULT_TRANSFORM_OPTIONS.merge(options)
2114
end
2215

2316
def transform(code)
24-
puts @transform_options
2517
Babel::Transpiler.transform(code)['code']
2618
end
2719
end

test/react/jsx_test.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
# Sprockets is inserting a newline after the docblock for some reason...
55
EXPECTED_JS = <<eos
6+
"use strict";
7+
68
React.createElement("div", null);
79
eos
810

911
EXPECTED_JS_2 = <<eos
12+
"use strict";
13+
1014
(function() {
1115
var Component;
1216
@@ -30,18 +34,19 @@ def transform(code)
3034
class JSXTransformTest < ActionDispatch::IntegrationTest
3135
setup do
3236
clear_sprockets_cache
37+
React::JSX.transformer_class = React::JSX::BabelTransformer
38+
React::JSX.transform_options = {}
3339
end
3440

3541
teardown do
3642
clear_sprockets_cache
37-
React::JSX.transformer_class = React::JSX::JSXTransformer
38-
React::JSX.transform_options = {}
3943
end
4044

4145
test 'asset pipeline should transform JSX' do
4246
get '/assets/example.js'
4347
assert_response :success
44-
assert_equal EXPECTED_JS, @response.body
48+
49+
assert_equal EXPECTED_JS.gsub(/\s/, ''), @response.body.gsub(/\s/, '')
4550
end
4651

4752
test 'asset pipeline should transform JSX + Coffeescript' do
@@ -51,9 +56,31 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
5156
# as some version inserts an extra "\n" at the beginning.
5257
# Because appraisal is used, multiple versions of coffee-script are treated
5358
# together. Remove all spaces to make test pass.
59+
# puts @response.body
5460
assert_equal EXPECTED_JS_2.gsub(/\s/, ''), @response.body.gsub(/\s/, '')
5561
end
5662

63+
test 'use a custom transformer' do
64+
React::JSX.transformer_class = NullTransformer
65+
manually_expire_asset('example2.js')
66+
get '/assets/example2.js'
67+
assert_equal "TRANSFORMED CODE!;\n", @response.body
68+
end
69+
70+
end
71+
72+
class JSXTransformerTest < ActionDispatch::IntegrationTest
73+
74+
setup do
75+
clear_sprockets_cache
76+
React::JSX.transformer_class = React::JSX::JSXTransformer
77+
React::JSX.transform_options = {}
78+
end
79+
80+
teardown do
81+
clear_sprockets_cache
82+
end
83+
5784
test 'can use dropped-in version of JSX transformer' do
5885
hidden_path = Rails.root.join("vendor/assets/react/JSXTransformer__.js")
5986
replacing_path = Rails.root.join("vendor/assets/react/JSXTransformer.js")
@@ -89,7 +116,6 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
89116
replacing_path = custom_path.join("CustomTransformer.js")
90117

91118
React::JSX.transform_options = {asset_path: "custom/CustomTransformer.js"}
92-
React::JSX.transformer_class = React::JSX::JSXTransformer
93119

94120
FileUtils.mkdir_p(custom_path)
95121
FileUtils.cp(hidden_path, replacing_path)
@@ -100,10 +126,4 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
100126
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
101127
end
102128

103-
test 'use a custom transformer' do
104-
React::JSX.transformer_class = NullTransformer
105-
manually_expire_asset('example2.js')
106-
get '/assets/example2.js'
107-
assert_equal "TRANSFORMED CODE!;\n", @response.body
108-
end
109129
end

0 commit comments

Comments
 (0)