Skip to content

Commit

Permalink
fix: constructor and method receiver parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Feb 12, 2024
1 parent 983a09d commit 6e9b809
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
36 changes: 25 additions & 11 deletions packages/java-parser/src/productions/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});

Expand Down
15 changes: 15 additions & 0 deletions packages/java-parser/test/bugs-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
22 changes: 12 additions & 10 deletions packages/prettier-plugin-java/src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]])
]);
}

Expand Down Expand Up @@ -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]
Expand Down
25 changes: 25 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/bug-fixes/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,28 @@ void process(
@NonNull Map<String, Object> 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) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,38 @@ void process(
@NonNull Map<String, Object> 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) {}
}
}

0 comments on commit 6e9b809

Please sign in to comment.