From 6e9b809b2e79849820ae91466fdccf6d9da38997 Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Mon, 12 Feb 2024 00:56:48 -0700 Subject: [PATCH] fix: constructor and method receiver parameter --- packages/java-parser/api.d.ts | 2 ++ .../java-parser/src/productions/classes.js | 36 +++++++++++++------ packages/java-parser/test/bugs-spec.js | 15 ++++++++ .../src/printers/classes.ts | 22 ++++++------ .../test/unit-test/bug-fixes/_input.java | 25 +++++++++++++ .../test/unit-test/bug-fixes/_output.java | 35 ++++++++++++++++++ 6 files changed, 114 insertions(+), 21 deletions(-) diff --git a/packages/java-parser/api.d.ts b/packages/java-parser/api.d.ts index 1db05aea..13bb900c 100644 --- a/packages/java-parser/api.d.ts +++ b/packages/java-parser/api.d.ts @@ -1364,6 +1364,8 @@ export interface MethodDeclaratorCstNode extends CstNode { export type MethodDeclaratorCtx = { Identifier: IToken[]; LBrace: IToken[]; + receiverParameter?: ReceiverParameterCstNode[]; + Comma?: IToken[]; formalParameterList?: FormalParameterListCstNode[]; RBrace: IToken[]; dims?: DimsCstNode[]; diff --git a/packages/java-parser/src/productions/classes.js b/packages/java-parser/src/productions/classes.js index bdd012ed..8ed02e43 100644 --- a/packages/java-parser/src/productions/classes.js +++ b/packages/java-parser/src/productions/classes.js @@ -320,13 +320,22 @@ export function defineRules($, t) { ]); }); - // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-MethodDeclarator + // https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-MethodDeclarator $.RULE("methodDeclarator", () => { $.CONSUME(t.Identifier); $.CONSUME(t.LBrace); - $.OPTION(() => { - $.SUBRULE($.formalParameterList); - }); + $.OR([ + { + ALT: () => { + $.SUBRULE($.receiverParameter); + $.OPTION(() => { + $.CONSUME(t.Comma); + $.SUBRULE($.formalParameterList); + }); + } + }, + { ALT: () => $.OPTION1(() => $.SUBRULE1($.formalParameterList)) } + ]); $.CONSUME(t.RBrace); $.OPTION2(() => { $.SUBRULE($.dims); @@ -464,13 +473,18 @@ export function defineRules($, t) { }); $.SUBRULE($.simpleTypeName); $.CONSUME(t.LBrace); - $.OPTION2(() => { - $.SUBRULE($.receiverParameter); - $.CONSUME(t.Comma); - }); - $.OPTION3(() => { - $.SUBRULE($.formalParameterList); - }); + $.OR([ + { + ALT: () => { + $.SUBRULE($.receiverParameter); + $.OPTION1(() => { + $.CONSUME(t.Comma); + $.SUBRULE($.formalParameterList); + }); + } + }, + { ALT: () => $.OPTION2(() => $.SUBRULE1($.formalParameterList)) } + ]); $.CONSUME(t.RBrace); }); diff --git a/packages/java-parser/test/bugs-spec.js b/packages/java-parser/test/bugs-spec.js index 6a8485cc..58b3aebd 100644 --- a/packages/java-parser/test/bugs-spec.js +++ b/packages/java-parser/test/bugs-spec.js @@ -97,4 +97,19 @@ describe("The Java Parser fixed bugs", () => { expect(() => javaParser.parse(input, "statement")).to.not.throw(); }); }); + + it("issue #607 - should parse receiver parameter", () => { + [ + "class Currency { Currency(Currency this) {} }", + "class Currency { Currency(Currency this, Currency other) {} }", + "class Currency { Currency(@AnnotatedUsage Currency this, Currency other) {} }", + "class Currency { String getCode(Currency this) {} }", + "class Currency { int compareTo(Currency this, Currency other) {} }", + "class Currency { int compareTo(@AnnotatedUsage Currency this, Currency other) {} }", + "class Currency { class Inner { Inner(Currency Currency.this) {} } }", + "class Currency { class Inner { String getCode(Currency Currency.this) {} } }" + ].forEach(input => { + expect(() => javaParser.parse(input, "classDeclaration")).to.not.throw(); + }); + }); }); diff --git a/packages/prettier-plugin-java/src/printers/classes.ts b/packages/prettier-plugin-java/src/printers/classes.ts index 8e19acb8..78f3645e 100644 --- a/packages/prettier-plugin-java/src/printers/classes.ts +++ b/packages/prettier-plugin-java/src/printers/classes.ts @@ -570,13 +570,17 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { methodDeclarator(ctx: MethodDeclaratorCtx) { const identifier = printTokenWithComments(ctx.Identifier[0]); + const receiverParameter = this.visit(ctx.receiverParameter); const formalParameterList = this.visit(ctx.formalParameterList); const dims = this.visit(ctx.dims); return rejectAndConcat([ identifier, putIntoBraces( - formalParameterList, + rejectAndJoin(line, [ + rejectAndConcat([receiverParameter, ctx.Comma?.[0]]), + formalParameterList + ]), softline, ctx.LBrace[0], ctx.RBrace[0] @@ -588,15 +592,11 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { receiverParameter(ctx: ReceiverParameterCtx) { const annotations = this.mapVisit(ctx.annotation); const unannType = this.visit(ctx.unannType); - const identifier = ctx.Identifier - ? concat([ctx.Identifier[0], ctx.Dot![0]]) - : ""; - return rejectAndJoin("", [ - rejectAndJoin(" ", annotations), + return rejectAndJoin(" ", [ + ...annotations, unannType, - identifier, - ctx.This[0] + rejectAndConcat([ctx.Identifier?.[0], ctx.Dot?.[0], ctx.This[0]]) ]); } @@ -723,14 +723,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { const simpleTypeName = this.visit(ctx.simpleTypeName); const receiverParameter = this.visit(ctx.receiverParameter); const formalParameterList = this.visit(ctx.formalParameterList); - const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : []; return rejectAndJoin(" ", [ typeParameters, concat([ simpleTypeName, putIntoBraces( - rejectAndJoinSeps(commas, [receiverParameter, formalParameterList]), + rejectAndJoin(line, [ + rejectAndConcat([receiverParameter, ctx.Comma?.[0]]), + formalParameterList + ]), softline, ctx.LBrace[0], ctx.RBrace[0] diff --git a/packages/prettier-plugin-java/test/unit-test/bug-fixes/_input.java b/packages/prettier-plugin-java/test/unit-test/bug-fixes/_input.java index ce929b44..fb2b9ce6 100644 --- a/packages/prettier-plugin-java/test/unit-test/bug-fixes/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/bug-fixes/_input.java @@ -8,3 +8,28 @@ void process( @NonNull Map context ) {} } + +// Fix for https://github.com/jhipster/prettier-java/issues/607 +class Currency { + Currency(Currency this) {} + + Currency(Currency this, Currency other) {} + + Currency(@AnnotatedUsage Currency this, Currency other) {} + + Currency(@AnnotatedUsage Currency this, String aaaaaaaaaa, String bbbbbbbbbb) {} + + String getCode(Currency this) {} + + int compareTo(Currency this, Currency other) {} + + int compareTo(@AnnotatedUsage Currency this, Currency other) {} + + int compareTo(@AnnotatedUsage Currency this, String aaaaaaaaaa, String bbbbbbbbbb) {} + + class Inner { + Inner(Currency Currency.this) {} + + String getCode(Currency Currency.this) {} + } +} diff --git a/packages/prettier-plugin-java/test/unit-test/bug-fixes/_output.java b/packages/prettier-plugin-java/test/unit-test/bug-fixes/_output.java index 94283120..2a0a47a2 100644 --- a/packages/prettier-plugin-java/test/unit-test/bug-fixes/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/bug-fixes/_output.java @@ -11,3 +11,38 @@ void process( @NonNull Map context ) {} } + +// Fix for https://github.com/jhipster/prettier-java/issues/607 +class Currency { + + Currency(Currency this) {} + + Currency(Currency this, Currency other) {} + + Currency(@AnnotatedUsage Currency this, Currency other) {} + + Currency( + @AnnotatedUsage Currency this, + String aaaaaaaaaa, + String bbbbbbbbbb + ) {} + + String getCode(Currency this) {} + + int compareTo(Currency this, Currency other) {} + + int compareTo(@AnnotatedUsage Currency this, Currency other) {} + + int compareTo( + @AnnotatedUsage Currency this, + String aaaaaaaaaa, + String bbbbbbbbbb + ) {} + + class Inner { + + Inner(Currency Currency.this) {} + + String getCode(Currency Currency.this) {} + } +}