in compiler/crates/relay-codegen/src/printer.rs [312:384]
fn print_ast(&mut self, f: &mut String, key: AstKey, indent: usize, is_dedupe_var: bool) {
// Only use variable references at depth beyond the top level.
if indent > 0 && self.duplicates.contains(&key) {
let v = if self.variable_definitions.contains_key(&key) {
self.variable_definitions.get_full(&key).unwrap().0
} else {
let mut variable = String::new();
self.print_ast(&mut variable, key, 0, true);
let v = self.variable_definitions.len();
self.variable_definitions.insert(key, variable);
v
};
return write!(f, "(v{}/*: any*/)", v).unwrap();
}
let ast = self.builder.lookup(key);
match ast {
Ast::Object(object) => {
if object.is_empty() {
f.push_str("{}");
} else {
let next_indent = indent + 1;
f.push('{');
for ObjectEntry { key, value } in object {
match value {
Primitive::SkippableNull if self.skip_printing_nulls => continue,
_ => {}
}
f.push('\n');
print_indentation(f, next_indent);
write!(f, "\"{}\": ", key).unwrap();
self.print_primitive(f, value, next_indent, is_dedupe_var)
.unwrap();
f.push(',');
}
f.pop();
f.push('\n');
print_indentation(f, indent);
f.push('}');
}
}
Ast::Array(array) => {
if array.is_empty() {
if is_dedupe_var {
// Empty arrays can only have one inferred flow type and then conflict if
// used in different places, this is unsound if we would write to them but
// this whole module is based on the idea of a read only JSON tree.
f.push_str("([]/*: any*/)");
} else {
f.push_str("[]");
}
} else {
f.push('[');
let next_indent = indent + 1;
for value in array {
match value {
Primitive::SkippableNull if self.skip_printing_nulls => continue,
_ => {}
}
f.push('\n');
print_indentation(f, next_indent);
self.print_primitive(f, value, next_indent, is_dedupe_var)
.unwrap();
f.push(',');
}
f.pop();
f.push('\n');
print_indentation(f, indent);
f.push(']');
}
}
}
}