Respect useLineBreaks for union/intersect toString (#487)

* Respect useLineBreaks for union/intersect toString

* Apply suggestions from code review

Co-authored-by: Andy Friesen <andy.friesen@gmail.com>

Co-authored-by: Andy Friesen <andy.friesen@gmail.com>
This commit is contained in:
JohnnyMorganz 2022-05-17 19:22:54 +01:00 committed by GitHub
parent ab4bb355a3
commit f2191b9e4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -745,7 +745,10 @@ struct TypeVarStringifier
for (std::string& ss : results)
{
if (!first)
state.emit(" | ");
{
state.newline();
state.emit("| ");
}
state.emit(ss);
first = false;
}
@ -798,7 +801,10 @@ struct TypeVarStringifier
for (std::string& ss : results)
{
if (!first)
state.emit(" & ");
{
state.newline();
state.emit("& ");
}
state.emit(ss);
first = false;
}

View File

@ -126,6 +126,39 @@ TEST_CASE_FIXTURE(Fixture, "functions_are_always_parenthesized_in_unions_or_inte
CHECK_EQ(toString(&itv), "((number, string) -> (string, number)) & ((string, number) -> (number, string))");
}
TEST_CASE_FIXTURE(Fixture, "intersections_respects_use_line_breaks")
{
CheckResult result = check(R"(
local a: ((string) -> string) & ((number) -> number)
)");
ToStringOptions opts;
opts.useLineBreaks = true;
//clang-format off
CHECK_EQ("((number) -> number)\n"
"& ((string) -> string)",
toString(requireType("a"), opts));
//clang-format on
}
TEST_CASE_FIXTURE(Fixture, "unions_respects_use_line_breaks")
{
CheckResult result = check(R"(
local a: string | number | boolean
)");
ToStringOptions opts;
opts.useLineBreaks = true;
//clang-format off
CHECK_EQ("boolean\n"
"| number\n"
"| string",
toString(requireType("a"), opts));
//clang-format on
}
TEST_CASE_FIXTURE(Fixture, "quit_stringifying_table_type_when_length_is_exceeded")
{
TableTypeVar ttv{};