diff --git a/packages/java-parser/package.json b/packages/java-parser/package.json index 2bd2222e..4f892dee 100644 --- a/packages/java-parser/package.json +++ b/packages/java-parser/package.json @@ -7,7 +7,7 @@ "license": "Apache-2.0", "types": "./api.d.ts", "dependencies": { - "chevrotain": "6.5.0", + "chevrotain": "9.0.2", "lodash": "4.17.21" }, "scripts": { diff --git a/packages/java-parser/src/parser.js b/packages/java-parser/src/parser.js index 7342164e..14bcd592 100644 --- a/packages/java-parser/src/parser.js +++ b/packages/java-parser/src/parser.js @@ -1,5 +1,5 @@ "use strict"; -const { Parser, isRecognitionException } = require("chevrotain"); +const { CstParser, isRecognitionException } = require("chevrotain"); const { allTokens, tokens: t } = require("./tokens"); const lexicalStructure = require("./productions/lexical-structure"); const typesValuesVariables = require("./productions/types-values-and-variables"); @@ -36,7 +36,7 @@ const { shouldNotFormat } = require("./comments"); * TODO: document guide lines for using back tracking * */ -class JavaParser extends Parser { +class JavaParser extends CstParser { constructor() { super(allTokens, { maxLookahead: 1, @@ -77,8 +77,8 @@ class JavaParser extends Parser { } cstPostNonTerminal(ruleCstResult, ruleName) { - super.cstPostNonTerminal(ruleCstResult, ruleName); if (this.isBackTracking() === false) { + super.cstPostNonTerminal(ruleCstResult, ruleName); this.mostEnclosiveCstNodeByStartOffset[ ruleCstResult.location.startOffset ] = ruleCstResult; diff --git a/packages/java-parser/src/productions/expressions.js b/packages/java-parser/src/productions/expressions.js index 2f96ead9..41a65000 100644 --- a/packages/java-parser/src/productions/expressions.js +++ b/packages/java-parser/src/productions/expressions.js @@ -309,12 +309,7 @@ function defineRules($, t) { $.SUBRULE($.annotation); }); - $.OPTION({ - NAME: "$methodTypeArguments", - DEF: () => { - $.SUBRULE2($.typeArguments); - } - }); + $.OPTION(() => $.SUBRULE2($.typeArguments)); $.SUBRULE($.fqnOrRefTypePartCommon); }); @@ -335,7 +330,6 @@ function defineRules($, t) { } $.OPTION2({ - NAME: "$classTypeArguments", // unrestricted typeArguments here would create an ambiguity with "LessThan" operator // e.g: "var x = a < b;" // The "<" would be parsed as the beginning of a "typeArguments" diff --git a/packages/prettier-plugin-java/src/printers/expressions.js b/packages/prettier-plugin-java/src/printers/expressions.js index b6e0ca0c..007f01ef 100644 --- a/packages/prettier-plugin-java/src/printers/expressions.js +++ b/packages/prettier-plugin-java/src/printers/expressions.js @@ -453,23 +453,11 @@ class ExpressionsPrettierVisitor { const annotation = this.mapVisit(ctx.annotation); const fqnOrRefTypeCommon = this.visit(ctx.fqnOrRefTypePartCommon); - let fqnOrRefTypePart$methodTypeArguments = ""; - if ( - ctx.$methodTypeArguments && - ctx.$methodTypeArguments[0].children && - ctx.$methodTypeArguments[0].children.typeArguments - ) { - fqnOrRefTypePart$methodTypeArguments = this.visit( - ctx.$methodTypeArguments - ); - } + const typeArguments = this.visit(ctx.typeArguments); return rejectAndJoin(" ", [ rejectAndJoin(" ", annotation), - rejectAndConcat([ - fqnOrRefTypePart$methodTypeArguments, - fqnOrRefTypeCommon - ]) + rejectAndConcat([typeArguments, fqnOrRefTypeCommon]) ]); } @@ -481,24 +469,9 @@ class ExpressionsPrettierVisitor { keyWord = ctx.Super[0]; } - let fqnOrRefTypePart$classTypeArguments = ""; - if ( - ctx.$classTypeArguments && - ctx.$classTypeArguments[0].children && - ctx.$classTypeArguments[0].children.typeArguments - ) { - fqnOrRefTypePart$classTypeArguments = this.visit(ctx.$classTypeArguments); - } - - return rejectAndConcat([keyWord, fqnOrRefTypePart$classTypeArguments]); - } - - fqnOrRefTypePartRest$methodTypeArguments(ctx) { - return this.visitSingle(ctx); - } + const typeArguments = this.visit(ctx.typeArguments); - fqnOrRefTypePartCommon$classTypeArguments(ctx) { - return this.visitSingle(ctx); + return rejectAndConcat([keyWord, typeArguments]); } parenthesisExpression(ctx) { diff --git a/packages/prettier-plugin-java/test/unit-test/expressions/_input.java b/packages/prettier-plugin-java/test/unit-test/expressions/_input.java index 31e77d8d..9904a001 100644 --- a/packages/prettier-plugin-java/test/unit-test/expressions/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/expressions/_input.java @@ -185,5 +185,9 @@ public void longFullyQualifiedName() { public void unannTypePrimitiveWithMethodReferenceSuffix(String[] args) { List.of(new double[][] { 1,2,3,4.1,5.6846465}, new double[][] { 1,2,3,4.1,5.6846465}, new double[][] { 1,2,3,4.1,5.6846465}).toArray(double[][]::new); } + + public void t() { + var t = ImmutableMap.of::a(); + } } diff --git a/packages/prettier-plugin-java/test/unit-test/expressions/_output.java b/packages/prettier-plugin-java/test/unit-test/expressions/_output.java index ea959ee8..1dbba2a6 100644 --- a/packages/prettier-plugin-java/test/unit-test/expressions/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/expressions/_output.java @@ -245,4 +245,8 @@ public void unannTypePrimitiveWithMethodReferenceSuffix(String[] args) { ) .toArray(double[][]::new); } + + public void t() { + var t = ImmutableMap.of::a(); + } } diff --git a/yarn.lock b/yarn.lock index eed025dc..2ae77616 100644 --- a/yarn.lock +++ b/yarn.lock @@ -237,6 +237,16 @@ lodash "^4.17.13" to-fast-properties "^2.0.0" +"@chevrotain/types@^9.0.2": + version "9.0.2" + resolved "https://registry.yarnpkg.com/@chevrotain/types/-/types-9.0.2.tgz#477bb3b973b91ff47377399d1e9f4410be6c0141" + integrity sha512-lo1dQPX7DQffJb26eaYLEy4/jUTFmsGKa43mDvMNAHwItEgUQHUkTZR0iAkHG0aJv8ejM/KqYpRVSNetrOK8qw== + +"@chevrotain/utils@^9.0.2": + version "9.0.2" + resolved "https://registry.yarnpkg.com/@chevrotain/utils/-/utils-9.0.2.tgz#6f28fd41d2168c21e441e1a4ad6a0097e4d351f5" + integrity sha512-iTju1VpbGruWagXS/XswuqeimOCRNeDvrXLlWHYsHp1qTU8sJfAfLiX5vs7DNxB1px6N8VWVI0SD8vMUksNBYw== + "@eslint/eslintrc@^0.4.2": version "0.4.2" resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.4.2.tgz#f63d0ef06f5c0c57d76c4ab5f63d3835c51b0179" @@ -1826,12 +1836,14 @@ check-error@^1.0.2: resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82" integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII= -chevrotain@6.5.0: - version "6.5.0" - resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-6.5.0.tgz#dcbef415516b0af80fd423cc0d96b28d3f11374e" - integrity sha512-BwqQ/AgmKJ8jcMEjaSnfMybnKMgGTrtDKowfTP3pX4jwVy0kNjRsT/AP6h+wC3+3NC+X8X15VWBnTCQlX+wQFg== +chevrotain@9.0.2: + version "9.0.2" + resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-9.0.2.tgz#7cc462262fec8ee293d96b756085ac71b8804bf1" + integrity sha512-6ZjgUdGvU4j1n1b2hTjb79Vr2V+qNtmP7f8FVt79+kdAYcUj2QfYNwI8ycCVsgHD/dIeO5Vr1hckkkfliVQTfg== dependencies: - regexp-to-ast "0.4.0" + "@chevrotain/types" "^9.0.2" + "@chevrotain/utils" "^9.0.2" + regexp-to-ast "0.5.0" chokidar@3.5.1: version "3.5.1" @@ -6250,10 +6262,10 @@ redent@^3.0.0: indent-string "^4.0.0" strip-indent "^3.0.0" -regexp-to-ast@0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/regexp-to-ast/-/regexp-to-ast-0.4.0.tgz#f3dbcb42726cd71902ba50193f63eab5325cd7cb" - integrity sha512-4qf/7IsIKfSNHQXSwial1IFmfM1Cc/whNBQqRwe0V2stPe7KmN1U0tWQiIx6JiirgSrisjE0eECdNf7Tav1Ntw== +regexp-to-ast@0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/regexp-to-ast/-/regexp-to-ast-0.5.0.tgz#56c73856bee5e1fef7f73a00f1473452ab712a24" + integrity sha512-tlbJqcMHnPKI9zSrystikWKwHkBqu2a/Sgw01h3zFjvYrMxEDYHzzoMZnUrbIfpTFEsoRnnviOXNCzFiSc54Qw== regexpp@^3.1.0: version "3.1.0"