Skip to content

Commit b689919

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

File tree

6 files changed

+52
-13
lines changed

6 files changed

+52
-13
lines changed

.github/workflows/github-ci.yml

Lines changed: 3 additions & 3 deletions
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

Lines changed: 24 additions & 6 deletions
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

Lines changed: 6 additions & 1 deletion
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 = {};

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,8 @@ 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+
// TODO: complexify lookahed logic to see if we can avoid backtracking
162+
GATE: () => this.BACKTRACK_LOOKAHEAD($.isFollowingVariableDeclarator),
164163
DEF: () => {
165164
$.CONSUME(t.Comma);
166165
$.SUBRULE2($.variableDeclarator);
@@ -732,4 +731,9 @@ export function defineRules($, t) {
732731
tokenMatcher(this.LA(2).tokenType, t.RSquare)
733732
);
734733
});
734+
735+
$.RULE("isFollowingVariableDeclarator", () => {
736+
$.CONSUME(t.Comma);
737+
$.SUBRULE2($.variableDeclarator);
738+
});
735739
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ 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+
});
5764
});

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

Lines changed: 5 additions & 0 deletions
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)