Skip to content

Commit

Permalink
fix: separate binary expression from pattern
Browse files Browse the repository at this point in the history
closes #605
  • Loading branch information
jtkiesel committed Sep 17, 2023
1 parent a7f9a5f commit ec0e028
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
2 changes: 0 additions & 2 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3498,8 +3498,6 @@ export interface PatternCstNode extends CstNode {

export type PatternCtx = {
primaryPattern: PrimaryPatternCstNode[];
AndAnd?: IToken[];
binaryExpression?: BinaryExpressionCstNode[];
};

export interface PrimaryPatternCstNode extends CstNode {
Expand Down
4 changes: 0 additions & 4 deletions packages/java-parser/src/productions/blocks-and-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ function defineRules($, t) {
$.OR([
{ ALT: () => $.CONSUME(t.Null) },
{ ALT: () => $.CONSUME(t.Default) },
{
GATE: () => this.BACKTRACK_LOOKAHEAD($.pattern),
ALT: () => $.SUBRULE($.pattern)
},
{
GATE: () => tokenMatcher($.LA(1).tokenType, t.Null) === false,
ALT: () => $.SUBRULE($.caseConstant)
Expand Down
18 changes: 10 additions & 8 deletions packages/java-parser/src/productions/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,20 @@ function defineRules($, t) {
});

$.RULE("binaryExpression", () => {
$.SUBRULE($.unaryExpression);
$.OR([
{
GATE: () => this.BACKTRACK_LOOKAHEAD($.pattern),
ALT: () => $.SUBRULE($.pattern)
},
{ ALT: () => $.SUBRULE($.unaryExpression) }
])
$.MANY(() => {
$.OR({
$.OR1({
DEF: [
{
ALT: () => {
$.CONSUME(t.Instanceof);
$.OR1([
$.OR2([
{
GATE: () => this.BACKTRACK_LOOKAHEAD($.pattern),
ALT: () => $.SUBRULE($.pattern)
Expand All @@ -150,7 +156,7 @@ function defineRules($, t) {
tokenMatcher($.LA(2).tokenType, t.Less) ||
tokenMatcher($.LA(2).tokenType, t.Greater),
ALT: () => {
$.OR2([
$.OR3([
{
GATE: () => $.LA(1).startOffset + 1 === $.LA(2).startOffset,
ALT: () => {
Expand Down Expand Up @@ -563,10 +569,6 @@ function defineRules($, t) {

$.RULE("pattern", () => {
$.SUBRULE($.primaryPattern);
$.OPTION(() => {
$.CONSUME(t.AndAnd);
$.SUBRULE($.binaryExpression);
});
});

$.RULE("primaryPattern", () => {
Expand Down
22 changes: 3 additions & 19 deletions packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,9 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
return rejectAndConcat([keyWord, typeArguments]);
}

parenthesisExpression(
ctx: ParenthesisExpressionCtx,
params?: { addParenthesisToWrapStatement?: boolean }
) {
parenthesisExpression(ctx: ParenthesisExpressionCtx) {
const expression = this.visit(ctx.expression);
const separator = params?.addParenthesisToWrapStatement ? softline : "";
return putIntoBraces(expression, separator, ctx.LBrace[0], ctx.RBrace[0]);
return putIntoBraces(expression, softline, ctx.LBrace[0], ctx.RBrace[0]);
}

castExpression(ctx: CastExpressionCtx) {
Expand Down Expand Up @@ -715,19 +711,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
}

pattern(ctx: PatternCtx) {
const primaryPattern = this.visit(ctx.primaryPattern);
if (ctx.AndAnd === undefined) {
return primaryPattern;
}

const binaryExpression = this.visit(ctx.binaryExpression);
return rejectAndConcat([
primaryPattern,
" ",
ctx.AndAnd[0],
line,
binaryExpression
]);
return this.visit(ctx.primaryPattern);
}

primaryPattern(ctx: PrimaryPatternCtx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ static String formatter(Object o) {
return formatted;
}

public boolean test(final Object obj) {
return obj instanceof final Integer x && (x == 5 || x == 6 || x == 7 || x == 8 || x == 9 || x == 10 || x == 11);
}

void test(Buyer other) {
return switch (other) {
case null -> true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ static String formatter(Object o) {
return formatted;
}

public boolean test(final Object obj) {
return (
obj instanceof final Integer x &&
(x == 5 || x == 6 || x == 7 || x == 8 || x == 9 || x == 10 || x == 11)
);
}

void test(Buyer other) {
return switch (other) {
case null -> true;
Expand Down

0 comments on commit ec0e028

Please sign in to comment.