Skip to content

Commit 575d375

Browse files
fix(707): do not throw if the record-pattern contains dims
1 parent c3335c3 commit 575d375

File tree

8 files changed

+105
-18
lines changed

8 files changed

+105
-18
lines changed

.github/workflows/github-ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
strategy:
99
matrix:
1010
node_version:
11-
- 18.x
1211
- 20.x
12+
- 22.x
1313
steps:
1414
- uses: actions/checkout@v4
1515
- uses: actions/setup-node@v3
@@ -33,15 +33,15 @@ jobs:
3333
test_repository:
3434
- e2e-jhipster1
3535
- e2e-jhipster2
36-
node_version: [18.x]
36+
node_version: [22.x]
3737
steps:
3838
- uses: actions/checkout@v4
3939
- uses: actions/setup-node@v3
4040
with:
4141
node-version: ${{ matrix.node_version }}
4242
- uses: actions/setup-java@v3
4343
with:
44-
java-version: 17.x
44+
java-version: 21.x
4545
distribution: zulu
4646
- name: Install dependencies
4747
run: yarn

packages/java-parser/api.d.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
171171
param?: IN
172172
): OUT;
173173
isDims(ctx: IsDimsCtx, param?: IN): OUT;
174+
isFollowingVariableDeclarator(
175+
ctx: IsFollowingVariableDeclaratorCtx,
176+
param?: IN
177+
): OUT;
174178
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
175179
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
176180
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
@@ -499,6 +503,10 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
499503
param?: IN
500504
): OUT;
501505
isDims(ctx: IsDimsCtx, param?: IN): OUT;
506+
isFollowingVariableDeclarator(
507+
ctx: IsFollowingVariableDeclaratorCtx,
508+
param?: IN
509+
): OUT;
502510
compilationUnit(ctx: CompilationUnitCtx, param?: IN): OUT;
503511
ordinaryCompilationUnit(ctx: OrdinaryCompilationUnitCtx, param?: IN): OUT;
504512
modularCompilationUnit(ctx: ModularCompilationUnitCtx, param?: IN): OUT;
@@ -1555,7 +1563,7 @@ export interface SimpleTypeNameCstNode extends CstNode {
15551563
}
15561564

15571565
export type SimpleTypeNameCtx = {
1558-
TypeIdentifier: TypeIdentifierCstNode[];
1566+
typeIdentifier: TypeIdentifierCstNode[];
15591567
};
15601568

15611569
export interface ConstructorBodyCstNode extends CstNode {
@@ -1794,6 +1802,16 @@ export type IsDimsCtx = {
17941802
RBrace?: IToken[];
17951803
};
17961804

1805+
export interface IsFollowingVariableDeclaratorCstNode extends CstNode {
1806+
name: "isFollowingVariableDeclarator";
1807+
children: IsFollowingVariableDeclaratorCtx;
1808+
}
1809+
1810+
export type IsFollowingVariableDeclaratorCtx = {
1811+
Comma: IToken[];
1812+
variableDeclarator: VariableDeclaratorCstNode[];
1813+
};
1814+
17971815
export interface CompilationUnitCstNode extends CstNode {
17981816
name: "compilationUnit";
17991817
children: CompilationUnitCtx;
@@ -2238,7 +2256,7 @@ export interface ElementValueCstNode extends CstNode {
22382256
}
22392257

22402258
export type ElementValueCtx = {
2241-
expression?: ExpressionCstNode[];
2259+
conditionalExpression?: ConditionalExpressionCstNode[];
22422260
elementValueArrayInitializer?: ElementValueArrayInitializerCstNode[];
22432261
annotation?: AnnotationCstNode[];
22442262
};
@@ -2469,8 +2487,8 @@ export interface SwitchBlockCstNode extends CstNode {
24692487

24702488
export type SwitchBlockCtx = {
24712489
LCurly: IToken[];
2472-
switchBlockStatementGroup?: SwitchBlockStatementGroupCstNode[];
24732490
switchRule?: SwitchRuleCstNode[];
2491+
switchBlockStatementGroup?: SwitchBlockStatementGroupCstNode[];
24742492
RCurly: IToken[];
24752493
};
24762494

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

24932511
export type SwitchLabelCtx = {
24942512
Case?: IToken[];
2495-
Comma?: IToken[];
24962513
Null?: IToken[];
2514+
Comma?: IToken[];
24972515
Default?: IToken[];
24982516
casePattern?: CasePatternCstNode[];
24992517
guard?: GuardCstNode[];
@@ -2888,11 +2906,11 @@ export interface NormalLambdaParameterListCstNode extends CstNode {
28882906
}
28892907

28902908
export type NormalLambdaParameterListCtx = {
2891-
normalLambdaParameter: LambdaParameterCstNode[];
2909+
normalLambdaParameter: NormalLambdaParameterCstNode[];
28922910
Comma?: IToken[];
28932911
};
28942912

2895-
export interface LambdaParameterCstNode extends CstNode {
2913+
export interface NormalLambdaParameterCstNode extends CstNode {
28962914
name: "normalLambdaParameter";
28972915
children: LambdaParameterCtx;
28982916
}

packages/java-parser/scripts/generate-signature.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
import _ from "lodash";
33
import path from "path";
44
import fs from "fs";
5-
import JavaParser from "../src/parser";
5+
import { fileURLToPath } from "url";
6+
7+
import JavaParser from "../src/parser.js";
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
611

712
const parseRule = rule => {
813
const children = {};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/**
22
* This Script is used to debug the parsing of **small** code snippets.
33
*/
4-
import javaParserChev from "../src/index";
4+
import javaParserChev from "../src/index.js";
55

66
const input = `
7-
@Anno byte @Nullable ... test
7+
public class VariableTypeInference {
8+
9+
int foo = 0, bar = 1;
10+
}
11+
812
`;
913

10-
javaParserChev.parse(input, "variableArityParameter");
14+
javaParserChev.parse(input, "compilationUnit");

packages/java-parser/src/productions/classes.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ export function defineRules($, t) {
158158
$.SUBRULE($.variableDeclarator);
159159
$.MANY({
160160
// required to distinguish from patternList
161-
GATE: () =>
162-
!tokenMatcher(this.LA(3), t.Identifier) &&
163-
!tokenMatcher(this.LA(3), t.Underscore),
161+
GATE: () => this.BACKTRACK_LOOKAHEAD($.isFollowingVariableDeclarator),
164162
DEF: () => {
165163
$.CONSUME(t.Comma);
166164
$.SUBRULE2($.variableDeclarator);
@@ -728,8 +726,26 @@ export function defineRules($, t) {
728726
$.RULE("isDims", () => {
729727
$.MANY($.annotation);
730728
return (
731-
tokenMatcher(this.LA(1).tokenType, t.LSquare) &&
732-
tokenMatcher(this.LA(2).tokenType, t.RSquare)
729+
tokenMatcher(this.LA(1), t.LSquare) && tokenMatcher(this.LA(2), t.RSquare)
733730
);
734731
});
732+
733+
/*
734+
* The following sequence can either be a variable declarator or a component pattern if next token is a comma
735+
* This check if the following sequence is **not** a component pattern sequence
736+
*/
737+
$.RULE("isFollowingVariableDeclarator", () => {
738+
const hasDims =
739+
tokenMatcher(this.LA(3), t.LSquare) &&
740+
tokenMatcher(this.LA(4), t.RSquare);
741+
const offset = hasDims ? 2 : 0;
742+
if (
743+
tokenMatcher(this.LA(offset + 3), t.Identifier) ||
744+
tokenMatcher(this.LA(offset + 3), t.Underscore)
745+
) {
746+
return false;
747+
}
748+
749+
return !tokenMatcher(this.LA(3), t.LBrace);
750+
});
735751
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect } from "chai";
2+
import * as javaParser from "../src/index.js";
3+
4+
describe("The Java Parser fixed bugs", () => {
5+
it("should handle multiple variable declaration", () => {
6+
const input = `int foo, bar;`;
7+
expect(() => javaParser.parse(input, "fieldDeclaration")).to.not.throw();
8+
});
9+
});

packages/java-parser/test/pattern-matching/pattern-matching-spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,34 @@ describe("Pattern matching", () => {
5454
javaParser.parse(input, "componentPatternList")
5555
).to.not.throw();
5656
});
57+
58+
it("should parse pattern list with dims", () => {
59+
const input = `A a, B[] b`;
60+
expect(() =>
61+
javaParser.parse(input, "componentPatternList")
62+
).to.not.throw();
63+
});
64+
65+
it("should parse pattern list with nested records", () => {
66+
const input =
67+
"int a, Location (String name, GPSPoint(int latitude, int longitude))";
68+
expect(() =>
69+
javaParser.parse(input, "componentPatternList")
70+
).to.not.throw();
71+
});
72+
73+
it("should parse pattern list with var", () => {
74+
const input =
75+
"int a, Location (var name, GPSPoint(var latitude, var longitude))";
76+
expect(() =>
77+
javaParser.parse(input, "componentPatternList")
78+
).to.not.throw();
79+
});
80+
81+
it("should parse pattern list with nested records and annotation", () => {
82+
const input = "int a, @not Location (String name)";
83+
expect(() =>
84+
javaParser.parse(input, "componentPatternList")
85+
).to.not.throw();
86+
});
5787
});

packages/prettier-plugin-java/src/printers/classes.ts

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
FormalParameterListCtx,
5757
InstanceInitializerCtx,
5858
InterfaceTypeListCtx,
59+
IsFollowingVariableDeclaratorCtx,
5960
IToken,
6061
MethodBodyCtx,
6162
MethodDeclarationCtx,
@@ -1071,4 +1072,8 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
10711072
isDims() {
10721073
return "isDims";
10731074
}
1075+
1076+
isFollowingVariableDeclarator() {
1077+
return "isFollowingVariableDeclarator";
1078+
}
10741079
}

0 commit comments

Comments
 (0)