Skip to content

Commit

Permalink
fix(707): do not throw if the record-pattern contains dims
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Dec 22, 2024
1 parent d5fd515 commit f16b22c
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
strategy:
matrix:
node_version:
- 18.x
- 20.x
- 22.x
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
Expand All @@ -33,15 +33,15 @@ jobs:
test_repository:
- e2e-jhipster1
- e2e-jhipster2
node_version: [18.x]
node_version: [22.x]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
- uses: actions/setup-java@v3
with:
java-version: 17.x
java-version: 21.x
distribution: zulu
- name: Install dependencies
run: yarn
Expand Down
30 changes: 24 additions & 6 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
param?: IN
): OUT;
isDims(ctx: IsDimsCtx, param?: IN): OUT;
isFollowingVariableDeclarator(
ctx: IsFollowingVariableDeclaratorCtx,
param?: IN
): OUT;
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
Expand Down Expand Up @@ -499,6 +503,10 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
param?: IN
): OUT;
isDims(ctx: IsDimsCtx, param?: IN): OUT;
isFollowingVariableDeclarator(
ctx: IsFollowingVariableDeclaratorCtx,
param?: IN
): OUT;
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
Expand Down Expand Up @@ -1555,7 +1563,7 @@ export interface SimpleTypeNameCstNode extends CstNode {
}

export type SimpleTypeNameCtx = {
TypeIdentifier: TypeIdentifierCstNode[];
typeIdentifier: TypeIdentifierCstNode[];
};

export interface ConstructorBodyCstNode extends CstNode {
Expand Down Expand Up @@ -1794,6 +1802,16 @@ export type IsDimsCtx = {
RBrace?: IToken[];
};

export interface IsFollowingVariableDeclaratorCstNode extends CstNode {
name: "isFollowingVariableDeclarator";
children: IsFollowingVariableDeclaratorCtx;
}

export type IsFollowingVariableDeclaratorCtx = {
Comma: IToken[];
variableDeclarator: VariableDeclaratorCstNode[];
};

export interface CompilationUnitCstNode extends CstNode {
name: "compilationUnit";
children: CompilationUnitCtx;
Expand Down Expand Up @@ -2238,7 +2256,7 @@ export interface ElementValueCstNode extends CstNode {
}

export type ElementValueCtx = {
expression?: ExpressionCstNode[];
conditionalExpression?: ConditionalExpressionCstNode[];
elementValueArrayInitializer?: ElementValueArrayInitializerCstNode[];
annotation?: AnnotationCstNode[];
};
Expand Down Expand Up @@ -2469,8 +2487,8 @@ export interface SwitchBlockCstNode extends CstNode {

export type SwitchBlockCtx = {
LCurly: IToken[];
switchBlockStatementGroup?: SwitchBlockStatementGroupCstNode[];
switchRule?: SwitchRuleCstNode[];
switchBlockStatementGroup?: SwitchBlockStatementGroupCstNode[];
RCurly: IToken[];
};

Expand All @@ -2492,8 +2510,8 @@ export interface SwitchLabelCstNode extends CstNode {

export type SwitchLabelCtx = {
Case?: IToken[];
Comma?: IToken[];
Null?: IToken[];
Comma?: IToken[];
Default?: IToken[];
casePattern?: CasePatternCstNode[];
guard?: GuardCstNode[];
Expand Down Expand Up @@ -2888,11 +2906,11 @@ export interface NormalLambdaParameterListCstNode extends CstNode {
}

export type NormalLambdaParameterListCtx = {
normalLambdaParameter: LambdaParameterCstNode[];
normalLambdaParameter: NormalLambdaParameterCstNode[];
Comma?: IToken[];
};

export interface LambdaParameterCstNode extends CstNode {
export interface NormalLambdaParameterCstNode extends CstNode {
name: "normalLambdaParameter";
children: LambdaParameterCtx;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/java-parser/scripts/generate-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import _ from "lodash";
import path from "path";
import fs from "fs";
import JavaParser from "../src/parser";
import { fileURLToPath } from "url";

import JavaParser from "../src/parser.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const parseRule = rule => {
const children = {};
Expand Down
10 changes: 7 additions & 3 deletions packages/java-parser/scripts/single-sample-runner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/**
* This Script is used to debug the parsing of **small** code snippets.
*/
import javaParserChev from "../src/index";
import javaParserChev from "../src/index.js";

const input = `
@Anno byte @Nullable ... test
public class VariableTypeInference {
int foo = 0, bar = 1;
}
`;

javaParserChev.parse(input, "variableArityParameter");
javaParserChev.parse(input, "compilationUnit");
23 changes: 18 additions & 5 deletions packages/java-parser/src/productions/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ export function defineRules($, t) {
$.SUBRULE($.variableDeclarator);
$.MANY({
// required to distinguish from patternList
GATE: () =>
!tokenMatcher(this.LA(3), t.Identifier) &&
!tokenMatcher(this.LA(3), t.Underscore),
// TODO: complexify lookahed logic to see if we can avoid backtracking
GATE: () => this.BACKTRACK_LOOKAHEAD($.isFollowingVariableDeclarator),
DEF: () => {
$.CONSUME(t.Comma);
$.SUBRULE2($.variableDeclarator);
Expand Down Expand Up @@ -728,8 +727,22 @@ export function defineRules($, t) {
$.RULE("isDims", () => {
$.MANY($.annotation);
return (
tokenMatcher(this.LA(1).tokenType, t.LSquare) &&
tokenMatcher(this.LA(2).tokenType, t.RSquare)
tokenMatcher(this.LA(1), t.LSquare) && tokenMatcher(this.LA(2), t.RSquare)
);
});

$.RULE("isFollowingVariableDeclarator", () => {
const hasDims =
tokenMatcher(this.LA(3), t.LSquare) &&
tokenMatcher(this.LA(4), t.RSquare);
const offset = hasDims ? 2 : 0;
if (
tokenMatcher(this.LA(offset + 3), t.Identifier) ||
tokenMatcher(this.LA(offset + 3), t.Underscore)
) {
return false;
}

return !tokenMatcher(this.LA(3), t.LBrace);
});
}
9 changes: 9 additions & 0 deletions packages/java-parser/test/classes-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect } from "chai";
import * as javaParser from "../src/index.js";

describe("The Java Parser fixed bugs", () => {
it("should handle multiple variable declaration", () => {
const input = `int foo, bar;`;
expect(() => javaParser.parse(input, "fieldDeclaration")).to.not.throw();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,34 @@ describe("Pattern matching", () => {
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});

it("should parse pattern list with dims", () => {
const input = `A a, B[] b`;
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});

it("should parse pattern list with nested records", () => {
const input =
"int a, Location (String name, GPSPoint(int latitude, int longitude))";
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});

it("should parse pattern list with var", () => {
const input =
"int a, Location (var name, GPSPoint(var latitude, var longitude))";
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});

it("should parse pattern list with nested records and annotation", () => {
const input = "int a, @not Location (String name)";
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});
});
5 changes: 5 additions & 0 deletions packages/prettier-plugin-java/src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
FormalParameterListCtx,
InstanceInitializerCtx,
InterfaceTypeListCtx,
IsFollowingVariableDeclaratorCtx,
IToken,
MethodBodyCtx,
MethodDeclarationCtx,
Expand Down Expand Up @@ -1071,4 +1072,8 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
isDims() {
return "isDims";
}

isFollowingVariableDeclarator() {
return "isFollowingVariableDeclarator";
}
}

0 comments on commit f16b22c

Please sign in to comment.