Skip to content

Commit 7855d4c

Browse files
committed
Make generated castJsonResult.ts files undefined-safe
1 parent 4c091f9 commit 7855d4c

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/remote_generator/services/command_cast_result_generator.rb

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,24 @@ def cast_json_result_function_body(
116116
value: "element")
117117
result << "})"
118118
elsif child_cast_tree.is_a?(::Hash)
119-
# TODO: either test this codepath or delete it
119+
# TODO: either test this code path or delete it
120120
# :nocov:
121121
property = path_part =~ /\A\d+\z/ ? path_part.to_i : "\"#{path_part}\""
122122

123123
result << cast_json_result_function_body(child_cast_tree,
124124
parent:,
125125
property:,
126-
value: "#{parent}.#{path_part}")
126+
value: "#{parent}?.#{path_part}")
127127
# :nocov:
128128
elsif child_cast_tree.is_a?(CastTree)
129129
result << cast_json_result_function_body(child_cast_tree.children,
130-
parent: "#{parent}.#{path_part}")
130+
parent: "#{parent}?.#{path_part}")
131131

132132
property = path_part =~ /\A\d+\z/ ? path_part.to_i : "\"#{path_part}\""
133133
result << _ts_cast_expression(child_cast_tree,
134134
parent:,
135135
property:,
136-
value: "#{parent}.#{path_part}")
136+
value: "#{parent}?.#{path_part}")
137137
else
138138
# :nocov:
139139
raise "Not sure how to handle a #{cast_tree.class}: #{cast_tree}"
@@ -164,9 +164,9 @@ def _ts_cast_expression(cast_tree, value:, parent: nil, property: nil)
164164
if property.nil?
165165
parent
166166
elsif property =~ /\A"(.*)"\z/
167-
"#{parent}.#{$1}"
167+
"#{parent}?.#{$1}"
168168
else
169-
"#{parent}[#{property}]"
169+
"#{parent}?.[#{property}]"
170170
end
171171
else
172172
# TODO: either test this path or raise
@@ -183,21 +183,27 @@ def _ts_cast_expression(cast_tree, value:, parent: nil, property: nil)
183183

184184
type_symbol = type.type_symbol
185185

186-
value ||= lvalue
186+
value ||= lvalue.dup
187+
lvalue = lvalue.gsub("?.[", "[").gsub("?.", ".")
188+
present_value = value.gsub("?.[", "[").gsub("?.", ".")
187189

188-
if type_symbol == :date || type_symbol == :datetime
189-
"#{lvalue} = new Date(#{value})"
190-
elsif type.model?
191-
ts_model_name = model_to_ts_model_name(type,
192-
association_depth:,
193-
initial: cast_tree.initial)
190+
expression = "if (#{value} !== undefined) {\n"
194191

195-
"#{lvalue} = new #{ts_model_name}(#{value})"
196-
else
197-
# :nocov:
198-
raise "Not sure how to cast type #{type} to a Typescript expression"
199-
# :nocov:
200-
end
192+
expression += if type_symbol == :date || type_symbol == :datetime
193+
"#{lvalue} = new Date(#{present_value})\n"
194+
elsif type.model?
195+
ts_model_name = model_to_ts_model_name(type,
196+
association_depth:,
197+
initial: cast_tree.initial)
198+
199+
"#{lvalue} = new #{ts_model_name}(#{present_value})\n"
200+
else
201+
# :nocov:
202+
raise "Not sure how to cast type #{type} to a Typescript expression"
203+
# :nocov:
204+
end
205+
206+
expression += "}"
201207
end
202208

203209
def _construct_cast_tree(type_declaration, initial: false)

0 commit comments

Comments
 (0)