Skip to content

Commit c2d96d2

Browse files
committed
initial jsx error tests
1 parent cfaf471 commit c2d96d2

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

lib/ruby2js/jsx.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ def self.jsx2_rb(string)
1414
attr_name = ''
1515
attr_value = ''
1616

17+
backtrace = ''
18+
1719
for c in string.chars
20+
if c == "\n"
21+
backtrace = ''
22+
else
23+
backtrace += c
24+
end
25+
1826
case state
1927
when :text
2028
if c == '<'
@@ -58,7 +66,7 @@ def self.jsx2_rb(string)
5866
elsif c =~ /^\w$/
5967
element += c
6068
elsif c != ' '
61-
raise SyntaxError.new('invalid character in element: "/"')
69+
raise SyntaxError.new("invalid character in element: #{c.inspect}")
6270
end
6371

6472
when :void
@@ -138,13 +146,30 @@ def self.jsx2_rb(string)
138146
else
139147
raise RangeError.new("internal state error in JSX: #{state.inspect}")
140148
end
149+
141150
end
142151

143152
case state
144153
when :text
145154
result << "_ #{text.strip.inspect}\n" unless text.strip.empty?
155+
156+
when :element, :attr_name, :attr_value
157+
raise SyntaxError.new("unclosed element #{element.inspect}")
158+
159+
when :dquote, :squote
160+
raise SyntaxError.new("unclosed quote in #{element.inspect}")
161+
162+
when :attr_expr
163+
raise SyntaxError.new("unclosed value in #{element.inspect}")
164+
165+
else
166+
raise RangeError.new("internal state error in JSX: #{state.inspect}")
146167
end
147168

148169
result.join("\n")
170+
171+
rescue => e
172+
e.set_backtrace backtrace
173+
raise e
149174
end
150175
end

spec/jsx_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,59 @@ def to_rb(string)
5050
to_rb( '<><h1/><h2/></>' ).must_equal(
5151
['_ do', '_h1', '_h2', 'end'].join("\n"))
5252
end
53+
54+
describe "errors" do
55+
it "should detect invalid element name" do
56+
_(assert_raises {to_rb '<<'}.message).
57+
must_equal 'invalid character in element name: "<"'
58+
end
59+
60+
it "should detect invalid character after element close" do
61+
_(assert_raises {to_rb '</->'}.message).
62+
must_equal 'invalid character in element: "-"'
63+
end
64+
65+
it "should detect invalid character after void element close" do
66+
_(assert_raises {to_rb '<a/a>'}.message).
67+
must_equal 'invalid character in element: "/"'
68+
end
69+
70+
it "should detect invalid attribute name" do
71+
_(assert_raises {to_rb '<a b/>'}.message).
72+
must_equal 'invalid character in attribute name: "/"'
73+
end
74+
75+
it "should detect missing attribute value" do
76+
_(assert_raises {to_rb '<a b>'}.message).
77+
must_equal 'missing "=" after attribute "b" in element "a"'
78+
end
79+
80+
it "should detect missing attribute value quotes" do
81+
_(assert_raises {to_rb '<a b=1>'}.message).
82+
must_equal 'invalid value for attribute "b" in element "a"'
83+
end
84+
85+
it "should detect unclosed element" do
86+
_(assert_raises {to_rb '<a'}.message).
87+
must_equal 'unclosed element "a"'
88+
_(assert_raises {to_rb '<a b'}.message).
89+
must_equal 'unclosed element "a"'
90+
_(assert_raises {to_rb '<a b='}.message).
91+
must_equal 'unclosed element "a"'
92+
end
93+
94+
it "should detect unclosed string" do
95+
_(assert_raises {to_rb '<a b="'}.message).
96+
must_equal 'unclosed quote in "a"'
97+
_(assert_raises {to_rb "<a b='"}.message).
98+
must_equal 'unclosed quote in "a"'
99+
end
100+
101+
it "should detect unclosed value" do
102+
_(assert_raises {to_rb '<a b={x'}.message).
103+
must_equal 'unclosed value in "a"'
104+
end
105+
end
53106
end
54107

55108
describe "ruby/wunderbar to JSX" do

0 commit comments

Comments
 (0)