Don't have docs print unnecessary parens or spaces

This commit is contained in:
Richard Feldman 2022-09-12 20:32:15 -04:00
parent 0a3a9584c4
commit 791340e2ae
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B

View file

@ -239,7 +239,7 @@ fn render_module_documentation(
}
}
type_annotation_to_html(0, &mut content, type_ann);
type_annotation_to_html(0, &mut content, type_ann, false);
buf.push_str(
html_to_string(
@ -477,10 +477,18 @@ fn new_line(buf: &mut String) {
}
// html is written to buf
fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &TypeAnnotation) {
fn type_annotation_to_html(
indent_level: usize,
buf: &mut String,
type_ann: &TypeAnnotation,
needs_parens: bool,
) {
let is_multiline = should_be_multiline(type_ann);
match type_ann {
TypeAnnotation::TagUnion { tags, extension } => {
if tags.is_empty() {
buf.push_str("[]");
} else {
let tags_len = tags.len();
let tag_union_indent = indent_level + 1;
@ -508,7 +516,7 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
for type_value in &tag.values {
buf.push(' ');
type_annotation_to_html(next_indent_level, buf, type_value);
type_annotation_to_html(next_indent_level, buf, type_value, true);
}
if is_multiline {
@ -525,8 +533,9 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
}
buf.push(']');
}
type_annotation_to_html(indent_level, buf, extension);
type_annotation_to_html(indent_level, buf, extension, true);
}
TypeAnnotation::BoundVariable(var_name) => {
buf.push_str(var_name);
@ -535,18 +544,26 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
if parts.is_empty() {
buf.push_str(name);
} else {
if needs_parens {
buf.push('(');
}
buf.push_str(name);
for part in parts {
buf.push(' ');
type_annotation_to_html(indent_level, buf, part);
type_annotation_to_html(indent_level, buf, part, true);
}
if needs_parens {
buf.push(')');
}
}
}
TypeAnnotation::Record { fields, extension } => {
if fields.is_empty() {
buf.push_str("{}");
} else {
let fields_len = fields.len();
let record_indent = indent_level + 1;
if is_multiline {
@ -582,13 +599,13 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
type_annotation, ..
} => {
buf.push_str(" : ");
type_annotation_to_html(next_indent_level, buf, type_annotation);
type_annotation_to_html(next_indent_level, buf, type_annotation, false);
}
RecordField::OptionalField {
type_annotation, ..
} => {
buf.push_str(" ? ");
type_annotation_to_html(next_indent_level, buf, type_annotation);
type_annotation_to_html(next_indent_level, buf, type_annotation, false);
}
RecordField::LabelOnly { .. } => {}
}
@ -609,8 +626,9 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
}
buf.push('}');
}
type_annotation_to_html(indent_level, buf, extension);
type_annotation_to_html(indent_level, buf, extension, true);
}
TypeAnnotation::Function { args, output } => {
let mut peekable_args = args.iter().peekable();
@ -622,7 +640,7 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
indent(buf, indent_level + 1);
}
type_annotation_to_html(indent_level, buf, arg);
type_annotation_to_html(indent_level, buf, arg, false);
if peekable_args.peek().is_some() {
buf.push_str(", ");
@ -642,7 +660,7 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
next_indent_level += 1;
}
type_annotation_to_html(next_indent_level, buf, output);
type_annotation_to_html(next_indent_level, buf, output, false);
}
TypeAnnotation::Ability { members: _ } => {
// TODO(abilities): fill me in