From 72d3a947b2db19e992ce9b7cc14c02bed338033f Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Sat, 16 May 2026 15:52:07 -0600 Subject: [PATCH] fix: obey arrowParens=always on singular inferred lambda parameters --- src/printers/expressions.ts | 5 + .../arrow-parens-always/.prettierrc.json | 3 + .../{ => arrow-parens-always}/_input.java | 0 .../lambda/arrow-parens-always/_output.java | 656 ++++++++++++++++++ .../arrow-parens-avoid/.prettierrc.json | 3 + .../lambda/arrow-parens-avoid/_input.java | 417 +++++++++++ .../{ => arrow-parens-avoid}/_output.java | 0 test/unit-test/lambda/lambda-spec.ts | 11 +- 8 files changed, 1093 insertions(+), 2 deletions(-) create mode 100644 test/unit-test/lambda/arrow-parens-always/.prettierrc.json rename test/unit-test/lambda/{ => arrow-parens-always}/_input.java (100%) create mode 100644 test/unit-test/lambda/arrow-parens-always/_output.java create mode 100644 test/unit-test/lambda/arrow-parens-avoid/.prettierrc.json create mode 100644 test/unit-test/lambda/arrow-parens-avoid/_input.java rename test/unit-test/lambda/{ => arrow-parens-avoid}/_output.java (100%) diff --git a/src/printers/expressions.ts b/src/printers/expressions.ts index fe04000b..412692a2 100644 --- a/src/printers/expressions.ts +++ b/src/printers/expressions.ts @@ -682,6 +682,11 @@ function printLambdaExpressionSignature( } else { parts.push(parameters); } + + if (path.node.parametersNode.type === SyntaxType.Identifier) { + parts.unshift("("); + parts.push(")"); + } } const dangling = printDanglingComments(path); diff --git a/test/unit-test/lambda/arrow-parens-always/.prettierrc.json b/test/unit-test/lambda/arrow-parens-always/.prettierrc.json new file mode 100644 index 00000000..131fb143 --- /dev/null +++ b/test/unit-test/lambda/arrow-parens-always/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "arrowParens": "always" +} diff --git a/test/unit-test/lambda/_input.java b/test/unit-test/lambda/arrow-parens-always/_input.java similarity index 100% rename from test/unit-test/lambda/_input.java rename to test/unit-test/lambda/arrow-parens-always/_input.java diff --git a/test/unit-test/lambda/arrow-parens-always/_output.java b/test/unit-test/lambda/arrow-parens-always/_output.java new file mode 100644 index 00000000..f04a93c1 --- /dev/null +++ b/test/unit-test/lambda/arrow-parens-always/_output.java @@ -0,0 +1,656 @@ +public class Lambda { + + public void singleArgumentWithParens() { + call((x) -> { + System.out.println(x); + System.out.println(x); + }); + } + + public void singleArgumentWithoutParens() { + call((x) -> { + System.out.println(x); + System.out.println(x); + }); + } + + public void multiArguments() { + call((x, y) -> { + System.out.println(x); + System.out.println(y); + }); + } + + public void multiParameters() { + call((Object x, final String y) -> { + System.out.println(x); + System.out.println(y); + }); + } + + public void emptyArguments() { + call(() -> { + System.out.println(); + System.out.println(); + }); + } + + public void onlyOneMethodInBodyWithCurlyBraces() { + call((x) -> { + System.out.println(x); + }); + } + + public void onlyOneMethodInBody() { + call((x) -> System.out.println(x)); + } + + public void lambdaWithoutBracesWhichBreak() { + call( + (x) -> + foo.isVeryVeryVeryLongConditionTrue() && + foo.isAnotherVeryVeryLongConditionTrue() + ); + aaaaaaaaaa( + (bbbbbbbbbb) -> "123456789012345678901234567890123456789012345678" + ); + aaaaaaaaaa((bbbbbbbbbb) -> + cccccccccc("123456789012345678901234567890123456") + ); + } + + public void chainCallWithLambda() { + Stream.of(1, 2) + .map((n) -> { + // testing method + return n * 2; + }) + .collect(Collectors.toList()); + } + + public void lambdaWithLongListOfParameters() { + final List values = Stream.of(1, 2) + .map( + ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + } + ) + .collect(Collectors.toList()); + + final List values = Stream.of(1, 2) + .map( + (aVeryLongListOfParameter, aVeryLongListOfParameter, aParameterTha) -> { + // testing method + return n * 2; + } + ) + .collect(Collectors.toList()); + } + + public void shortLambdaAssignation() { + V t = (t) -> toto(); + } + + public void longLambdaAssignation() { + V t = ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParaa + ) -> { + // testing method + return n * 2; + }; + } + + public void callWithLambdaAndExtraParameter() { + CompletableFuture.supplyAsync(() -> { + // some processing + return 2; + }, executor); + } + + public void testConstructor() { + new Value((x) -> { + // testing method + return n * 2; + }); + + new Value((aVeryLongListOfParameter, aVeryLongListOfParameter) -> { + // testing method + return n * 2; + }); + + new Value( + ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + } + ); + } + + private static Function, T> createInstance( + Group entity + ) { + return (ctor) -> + Try.of(a, () -> { + @SuppressWarnings("unchecked") + var ng = (T) ctor.newInstance( + entity.getId(), + entity.getSystemGenerated(), + entity.getVersionKey() + ); + return ng; + }).getOrElseThrow((ex) -> new RuntimeException(ex)); + } + + void singleLambdaWithBlockLastArgument() { + a.of(b, c, d, (e) -> { + return f; + }); + } + + void singleLambdaWithBlockLastArgumentAndLongArguments() { + a.of( + aaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + (e) -> { + return f; + } + ); + + this.a( + aaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + (e) -> { + return f; + } + ); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArgument() { + a.of( + b, + c, + d, + (eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee) -> { + return f; + } + ); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArguments() { + a.of( + b, + ( + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + eeeeeeeeeeeeeeeeeeeeeeeeee + ) -> { + return f; + } + ); + } + + void multipleLambdaArguments() { + a.of( + b, + (c) -> d, + (e) -> { + return f; + } + ); + } + + void argumentAfterLambdaWithBlock() { + a.of( + b, + c, + d, + (e) -> { + return f; + }, + g + ); + } + + void huggableArguments() { + A.b().c(() -> { + return d; + }); + + aaaaaaaaaaaaaaaaaaaaaaaa( + ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + a.b( + (c) -> (d) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b((c) -> + d && eeeeeeeeee.ffffffffff() + ? g && hhhhhhhhhh.iiiiiiiiii() + : j && kkkkkkkkkk.llllllllll() + ); + + a.b( + (c) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > 0 + ); + + a.b( + c, + (c0, c1) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > 0 + ); + + a.b( + (c) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > 0 + ); + + a.b( + c, + (c0, c1) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > 0 + ); + + a.b( + (c) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b( + c, + (c0, c1) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b((c) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b((c) -> { + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ); + }); + + a.b((c0, c1) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b(c, (c0, c1) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a( + // comment + (b, c, d) -> e.f() + ); + + a( + ( + // comment + b, + c, + d + ) -> e.f() + ); + + a( + ( + b, // comment + c, + d + ) -> e.f() + ); + + a( + ( + b, + c, + d // comment + ) -> e.f() + ); + + a( + ( + b, + c, + d + // comment + ) -> e.f() + ); + + a(/* comment */ (b, c, d) -> e.f()); + + a((/* comment */ b, c, d) -> e.f()); + + a((b, /* comment */ c, d) -> e.f()); + + a((b, c, d /* comment */) -> e.f()); + + a( + ( + b, + c, + d + /* comment */ + ) -> e.f() + ); + + aaaaaaaaaaaaaaaaaaaaaaaa( + ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + // comment + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + aaaaaaaaaaaaaaaaaaaaaaaa( + /* comment */ + ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + aaaaaaaaaaaaaaaaaaaaaaaa( + /* comment */ ( + bbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccc, + dddddddddddddddddddddddd + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff() + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > 0 + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) > 0 + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + d && + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b( + ( + c0, + c1 + // comment + ) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + + a.b( + c, + ( + c0, + c1 + // comment + ) -> + eeeeeeeeee.ffffffffff( + gggggggggg, + hhhhhhhhhh, + iiiiiiiiii, + jjjjjjjjjj, + kkkkkkkkkk + ) + ); + } + + void lambdaWithLeadingComments() { + System.out.println( + List.of(1, 2, 3) + .stream() + .map( + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + (v) -> v * 2 + ) + .collect(Collectors.summingInt((v) -> v)) + ); + } + + void lambdaWithTrailingComments() { + System.out.println( + List.of(1, 2, 3) + .stream() + .map( + (v) -> v * 2 + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + ) + .collect(Collectors.summingInt((v) -> v)) + ); + } + + void lambdaInParentheses() { + ((aaaaaaaaaa) -> + bbbbbbbbbb.cccccccccc().dddddddddd().eeeeeeeeee().ffffffffff()); + } +} + +class T { + + T() { + super((x) -> { + // testing method + return n * 2; + }); + } + + T() { + super((x, y) -> { + // testing method + return n * 2; + }); + } + + T() { + super( + ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + } + ); + } + + T() { + super( + (aVeryLongListOfParameter, aVeryLongListOfParameter, aParameterThatS) -> { + // testing method + return n * 2; + } + ); + } + + T() { + super(a, () -> { + return b; + }); + } +} + +enum Enum { + VALUE((x) -> { + // testing method + return n * 2; + }), + VALUE((x, y) -> { + // testing method + return n * 2; + }), + VALUE( + ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + } + ), + VALUE( + (aVeryLongListOfParameter, aVeryLongListOfParameter, aParameterThatS) -> { + // testing method + return n * 2; + } + ), + VALUE((x) -> { + // testing method + return n * 2; + }, other), +} diff --git a/test/unit-test/lambda/arrow-parens-avoid/.prettierrc.json b/test/unit-test/lambda/arrow-parens-avoid/.prettierrc.json new file mode 100644 index 00000000..d68aa739 --- /dev/null +++ b/test/unit-test/lambda/arrow-parens-avoid/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "arrowParens": "avoid" +} diff --git a/test/unit-test/lambda/arrow-parens-avoid/_input.java b/test/unit-test/lambda/arrow-parens-avoid/_input.java new file mode 100644 index 00000000..e00ad9a0 --- /dev/null +++ b/test/unit-test/lambda/arrow-parens-avoid/_input.java @@ -0,0 +1,417 @@ +public class Lambda { + + public void singleArgumentWithParens() { + call((x) -> { + System.out.println(x); + System.out.println(x); + }); + } + + public void singleArgumentWithoutParens() { + call(x -> { + System.out.println(x); + System.out.println(x); + }); + } + + public void multiArguments() { + call((x, y) -> { + System.out.println(x); + System.out.println(y); + }); + } + + public void multiParameters() { + call((Object x, final String y) -> { + System.out.println(x); + System.out.println(y); + }); + } + + public void emptyArguments() { + call(() -> { + System.out.println(); + System.out.println(); + }); + } + + public void onlyOneMethodInBodyWithCurlyBraces() { + call(x -> { + System.out.println(x); + }); + } + + public void onlyOneMethodInBody() { + call(x -> System.out.println(x)); + } + + public void lambdaWithoutBracesWhichBreak() { + call(x -> foo.isVeryVeryVeryLongConditionTrue() && + foo.isAnotherVeryVeryLongConditionTrue()); + aaaaaaaaaa(bbbbbbbbbb -> "123456789012345678901234567890123456789012345678"); + aaaaaaaaaa(bbbbbbbbbb -> cccccccccc("123456789012345678901234567890123456")); + } + + public void chainCallWithLambda() { + Stream + .of(1, 2) + .map(n -> { + // testing method + return n * 2; + }) + .collect(Collectors.toList()); + } + + public void lambdaWithLongListOfParameters() { + final List values = Stream + .of(1, 2) + .map(( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + }) + .collect(Collectors.toList()); + + final List values = Stream + .of(1, 2) + .map(( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aParameterTha + ) -> { + // testing method + return n * 2; + }) + .collect(Collectors.toList()); + } + + public void shortLambdaAssignation() { + V t = t -> toto(); + } + + public void longLambdaAssignation() { + V t = ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParaa + ) -> { + // testing method + return n * 2; + }; + } + + public void callWithLambdaAndExtraParameter() { + CompletableFuture.supplyAsync( + () -> { + // some processing + return 2; + }, + executor + ); + } + + public void testConstructor() { + new Value( + ( + x + + ) -> { + // testing method + return n * 2; + } + ); + + new Value( + ( + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + } + ); + + new Value( + ( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter + ) -> { + // testing method + return n * 2; + } + ); + } + + private static Function, T> createInstance( + Group entity + ) { + return ctor -> + Try.of(a, () -> { + @SuppressWarnings("unchecked") + var ng = (T) ctor.newInstance( + entity.getId(), + entity.getSystemGenerated(), + entity.getVersionKey() + ); + return ng; + }).getOrElseThrow(ex -> new RuntimeException(ex)); + } + + void singleLambdaWithBlockLastArgument() { + a.of(b, c, d, e -> { + return f; + }); + } + + void singleLambdaWithBlockLastArgumentAndLongArguments() { + a.of( + aaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + e -> { + return f; + } + ); + + this.a( + aaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbb, + cccccccccccccccccccccccccc, + dddddddddddddddddddddddddd, + e -> { + return f; + } + ); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArgument() { + a.of(b, c, d, eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee -> { + return f; + }); + } + + void singleLambdaWithBlockLastArgumentAndLongLambdaArguments() { + a.of(b, (cccccccccccccccccccccccccc, dddddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeee) -> { + return f; + }); + } + + void multipleLambdaArguments() { + a.of(b, c -> d, e -> { + return f; + }); + } + + void argumentAfterLambdaWithBlock() { + a.of(b, c, d, e -> { + return f; + }, g); + } + + void huggableArguments() { + A.b().c(() -> { + return d; + }); + + aaaaaaaaaaaaaaaaaaaaaaaa((bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + a.b(c -> d -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c -> d && eeeeeeeeee.ffffffffff() ? g && hhhhhhhhhh.iiiiiiiiii() : j && kkkkkkkkkk.llllllllll()); + + a.b(c -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c, (c0, c1) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c -> { + eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk); + }); + + a.b((c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c, (c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a( // comment + (b, c, d) -> e.f()); + + a(( // comment + b, c, d) -> e.f()); + + a((b, // comment + c, d) -> e.f()); + + a((b, c, d // comment + ) -> e.f()); + + a((b, c, d + // comment + ) -> e.f()); + + a( /* comment */ (b, c, d) -> e.f()); + + a(( /* comment */ + b, c, d) -> e.f()); + + a((b, /* comment */ c, d) -> e.f()); + + a((b, c, d /* comment */ + ) -> e.f()); + + a((b, c, d + /* comment */ + ) -> e.f()); + + aaaaaaaaaaaaaaaaaaaaaaaa((bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd + // comment + ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + aaaaaaaaaaaaaaaaaaaaaaaa( /* comment */ + (bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + aaaaaaaaaaaaaaaaaaaaaaaa( /* comment */ (bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()); + + a.b(c, (c0, c1 + // comment + ) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1 + // comment + ) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0); + + a.b(c, (c0, c1 + // comment + ) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b((c0, c1 + // comment + ) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + + a.b(c, (c0, c1 + // comment + ) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk)); + } + + void lambdaWithLeadingComments() { + System.out.println( + List.of(1, 2, 3).stream().map( + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + v -> v * 2 + ).collect(Collectors.summingInt(v -> v)) + ); + } + + void lambdaWithTrailingComments() { + System.out.println( + List.of(1, 2, 3).stream().map( + v -> v * 2 + // a very long comment which explains the beatifullness of multiplication by 2 + // yes this is very important + ).collect(Collectors.summingInt(v -> v)) + ); + } + + void lambdaInParentheses() { + (aaaaaaaaaa -> bbbbbbbbbb.cccccccccc().dddddddddd().eeeeeeeeee().ffffffffff()); + } +} + +class T { + T() { + super(x -> { + // testing method + return n * 2; + }); + } + + T() { + super((x,y) -> { + // testing method + return n * 2; + }); + } + + T() { + super((aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter) -> { + // testing method + return n * 2; + }); + } + + T() { + super(( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aParameterThatS + ) -> { + // testing method + return n * 2; + }); + } + + T() { + super(a, () -> { + return b; + }); + } +} + +enum Enum { + VALUE(x -> { + // testing method + return n * 2; + }), + VALUE((x,y) -> { + // testing method + return n * 2; + }), + VALUE((aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aVeryLongListOfParameter) -> { + // testing method + return n * 2; + }), + VALUE(( + aVeryLongListOfParameter, + aVeryLongListOfParameter, + aParameterThatS + ) -> { + // testing method + return n * 2; + }), + VALUE(x -> { + // testing method + return n * 2; + }, other) +} diff --git a/test/unit-test/lambda/_output.java b/test/unit-test/lambda/arrow-parens-avoid/_output.java similarity index 100% rename from test/unit-test/lambda/_output.java rename to test/unit-test/lambda/arrow-parens-avoid/_output.java diff --git a/test/unit-test/lambda/lambda-spec.ts b/test/unit-test/lambda/lambda-spec.ts index 7b86c1f9..bbbbdbf5 100644 --- a/test/unit-test/lambda/lambda-spec.ts +++ b/test/unit-test/lambda/lambda-spec.ts @@ -1,9 +1,16 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; -import { testSample } from "../../test-utils.ts"; +import { testSample, testSampleWithOptions } from "../../test-utils.ts"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); describe("prettier-java", () => { - testSample(__dirname); + testSampleWithOptions({ + testFolder: path.resolve(__dirname, "arrow-parens-always") + }); + + testSampleWithOptions({ + testFolder: path.resolve(__dirname, "arrow-parens-avoid") + }); + testSample(path.resolve(__dirname, "arrow-parens-avoid")); });