Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,32 @@ export default {
const throwsIndex = path.node.namedChildren.findIndex(
({ type }) => type === SyntaxType.Throws
);
const hasBody = hasChild(path, "bodyNode");

if (throwsIndex !== -1) {
declaration.push(
group(indent([line, path.call(print, "namedChildren", throwsIndex)]))
);
const hasMultipleExceptions =
path.node.namedChildren[throwsIndex].namedChildren.length > 1;
const throws = path.call(print, "namedChildren", throwsIndex);
const bodySeparator = hasBody ? " " : "";

if (hasMultipleExceptions) {
const hasBodyContent =
hasBody && path.node.bodyNode.namedChildren.length > 0;
declaration.push(
group([indent([line, throws]), hasBodyContent ? line : bodySeparator])
);
} else {
declaration.push(group([" ", throws, bodySeparator]));
}
} else if (hasBody) {
declaration.push(" ");
}

return hasChild(path, "bodyNode")
? [modifiers, group(declaration), " ", path.call(print, "bodyNode")]
: [modifiers, group(declaration), ";"];
return [
modifiers,
declaration,
hasBody ? path.call(print, "bodyNode") : ";"
];
},

receiver_parameter(path, print) {
Expand Down Expand Up @@ -220,7 +237,10 @@ export default {
},

throws(path, print) {
return ["throws ", ...join(", ", path.map(print, "namedChildren"))];
const exceptions = path.map(print, "namedChildren");
return exceptions.length === 1
? ["throws ", exceptions[0]]
: ["throws", group(indent([line, ...join([",", line], exceptions)]))];
},

static_initializer(path, print) {
Expand All @@ -244,12 +264,19 @@ export default {
({ type }) => type === SyntaxType.Throws
);
if (throwsIndex !== -1) {
declaration.push(
group(indent([line, path.call(print, "namedChildren", throwsIndex)]))
);
const hasMultipleExceptions =
path.node.namedChildren[throwsIndex].namedChildren.length > 1;
const throws = path.call(print, "namedChildren", throwsIndex);
if (hasMultipleExceptions) {
declaration.push(group([indent([line, throws]), line]));
} else {
declaration.push(" ", throws, " ");
}
} else {
declaration.push(" ");
}

return [modifiers, group(declaration), " ", path.call(print, "bodyNode")];
return [modifiers, declaration, path.call(print, "bodyNode")];
},

constructor_body(path, print) {
Expand Down
20 changes: 20 additions & 0 deletions test/unit-test/throws/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,23 @@ public Throws(String string1, String string2, String string3, String string4, St
System.out.println("Constructor with throws that should wrap");
}
}

interface Example {
void example1(String arg1, String arg2) throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;

void example2(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) throws RuntimeException, RuntimeException, RuntimeException;

void example3(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;

default void example1(String arg1, String arg2) throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}

default void example2(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) throws RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}

default void example3(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}
}
125 changes: 112 additions & 13 deletions test/unit-test/throws/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@ void throwException2(String string) throws RuntimeException {
throw new RuntimeException();
}

void throwException3(String string1, String string2, String string3)
throws RuntimeException {
void throwException3(
String string1,
String string2,
String string3
) throws RuntimeException {
throw new RuntimeException();
}

void throwException4()
throws RuntimeException, RuntimeException, RuntimeException {
throws RuntimeException, RuntimeException, RuntimeException
{
throw new RuntimeException();
}

void throwException5(String string)
throws RuntimeException, RuntimeException, RuntimeException {
throws RuntimeException, RuntimeException, RuntimeException
{
throw new RuntimeException();
}

void throwException6(String string1, String string2, String string3)
throws RuntimeException, RuntimeException, RuntimeException {
throws RuntimeException, RuntimeException, RuntimeException
{
throw new RuntimeException();
}

Expand Down Expand Up @@ -52,18 +58,29 @@ void throwException9(
String string3,
String string4
)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException
{
throw new RuntimeException();
}

void aVeryLongNameForAMethodWichShouldBreakTheExpression()
throws aVeryLongException {}
void aVeryLongNameForAMethodWichShouldBreakTheExpression() throws aVeryLongException {}

void aVeryLongNameForAMethodWichShouldBreakTheExpression()
throws aVeryLongException, aVeryLongException {}

void aVeryLongNameForAMethodWichShouldBreakTheExpression()
throws Exception, Exception, Exception, Exception, Exception, Exception, Exception {}
throws
Exception,
Exception,
Exception,
Exception,
Exception,
Exception,
Exception {}

abstract void absThrowException1() throws RuntimeException;

Expand Down Expand Up @@ -100,14 +117,21 @@ abstract void absThrowException8(
String string3,
String string4
)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException;

public Throws(String string1) throws RuntimeException {
System.out.println("Constructor with throws that should not wrap");
}

public Throws(String string1, String string2, String string3)
throws RuntimeException {
public Throws(
String string1,
String string2,
String string3
) throws RuntimeException {
System.out.println("Constructor with throws that should wrap");
}

Expand All @@ -128,7 +152,82 @@ public Throws(
String string4,
String string5
)
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException
{
System.out.println("Constructor with throws that should wrap");
}
}

interface Example {
void example1(String arg1, String arg2)
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException;

void example2(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
) throws RuntimeException, RuntimeException, RuntimeException;

void example3(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
)
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException;

default void example1(String arg1, String arg2)
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException
{
throw new RuntimeException();
}

default void example2(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
) throws RuntimeException, RuntimeException, RuntimeException {
throw new RuntimeException();
}

default void example3(
String arg1,
String arg2,
String arg3,
String arg4,
String arg5,
String arg6
)
throws
RuntimeException,
RuntimeException,
RuntimeException,
RuntimeException
{
throw new RuntimeException();
}
}
Loading