diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index cf473b7d..0214676c 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -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 @@ -33,7 +33,7 @@ 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 @@ -41,7 +41,7 @@ jobs: 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 diff --git a/packages/java-parser/api.d.ts b/packages/java-parser/api.d.ts index ac035a8d..e04b3de4 100644 --- a/packages/java-parser/api.d.ts +++ b/packages/java-parser/api.d.ts @@ -171,6 +171,10 @@ export abstract class JavaCstVisitor implements ICstVisitor { 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; @@ -499,6 +503,10 @@ export abstract class JavaCstVisitorWithDefaults 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; @@ -1555,7 +1563,7 @@ export interface SimpleTypeNameCstNode extends CstNode { } export type SimpleTypeNameCtx = { - TypeIdentifier: TypeIdentifierCstNode[]; + typeIdentifier: TypeIdentifierCstNode[]; }; export interface ConstructorBodyCstNode extends CstNode { @@ -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; @@ -2238,7 +2256,7 @@ export interface ElementValueCstNode extends CstNode { } export type ElementValueCtx = { - expression?: ExpressionCstNode[]; + conditionalExpression?: ConditionalExpressionCstNode[]; elementValueArrayInitializer?: ElementValueArrayInitializerCstNode[]; annotation?: AnnotationCstNode[]; }; @@ -2469,8 +2487,8 @@ export interface SwitchBlockCstNode extends CstNode { export type SwitchBlockCtx = { LCurly: IToken[]; - switchBlockStatementGroup?: SwitchBlockStatementGroupCstNode[]; switchRule?: SwitchRuleCstNode[]; + switchBlockStatementGroup?: SwitchBlockStatementGroupCstNode[]; RCurly: IToken[]; }; @@ -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[]; @@ -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; } diff --git a/packages/java-parser/scripts/generate-signature.js b/packages/java-parser/scripts/generate-signature.js index 1cf2f49d..99f8cfbc 100644 --- a/packages/java-parser/scripts/generate-signature.js +++ b/packages/java-parser/scripts/generate-signature.js @@ -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 = {}; diff --git a/packages/java-parser/scripts/single-sample-runner.js b/packages/java-parser/scripts/single-sample-runner.js index b19f47ab..254d0ed0 100644 --- a/packages/java-parser/scripts/single-sample-runner.js +++ b/packages/java-parser/scripts/single-sample-runner.js @@ -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"); diff --git a/packages/java-parser/src/productions/classes.js b/packages/java-parser/src/productions/classes.js index 0a82db89..f688168e 100644 --- a/packages/java-parser/src/productions/classes.js +++ b/packages/java-parser/src/productions/classes.js @@ -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); @@ -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); + }); } diff --git a/packages/java-parser/test/classes-spec.js b/packages/java-parser/test/classes-spec.js new file mode 100644 index 00000000..62bdfddd --- /dev/null +++ b/packages/java-parser/test/classes-spec.js @@ -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(); + }); +}); diff --git a/packages/java-parser/test/pattern-matching/pattern-matching-spec.js b/packages/java-parser/test/pattern-matching/pattern-matching-spec.js index d02957e6..15069502 100644 --- a/packages/java-parser/test/pattern-matching/pattern-matching-spec.js +++ b/packages/java-parser/test/pattern-matching/pattern-matching-spec.js @@ -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(); + }); }); diff --git a/packages/prettier-plugin-java/src/printers/classes.ts b/packages/prettier-plugin-java/src/printers/classes.ts index 5cde0be6..107fb07e 100644 --- a/packages/prettier-plugin-java/src/printers/classes.ts +++ b/packages/prettier-plugin-java/src/printers/classes.ts @@ -56,6 +56,7 @@ import { FormalParameterListCtx, InstanceInitializerCtx, InterfaceTypeListCtx, + IsFollowingVariableDeclaratorCtx, IToken, MethodBodyCtx, MethodDeclarationCtx, @@ -1071,4 +1072,8 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter { isDims() { return "isDims"; } + + isFollowingVariableDeclarator() { + return "isFollowingVariableDeclarator"; + } }