Skip to content

Commit

Permalink
fix: fix parsing of unannPrimitiveType in primary
Browse files Browse the repository at this point in the history
fix #412
  • Loading branch information
clementdessoude committed Aug 22, 2020
1 parent 3805f7f commit f9b4249
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
18 changes: 9 additions & 9 deletions packages/java-parser/src/productions/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,19 @@ function defineRules($, t) {
$.OR([
// Spec Deviation: The array type "dims" suffix was extracted to this rule
// to avoid backtracking for performance reasons.
{
ALT: () => {
$.SUBRULE($.unannPrimitiveType);
$.OPTION({
GATE: () => this.BACKTRACK_LOOKAHEAD($.isDims),
DEF: () => $.SUBRULE2($.dims)
});
}
},
{ ALT: () => $.SUBRULE($.unannPrimitiveTypeWithOptionalDimsSuffix) },
{ ALT: () => $.SUBRULE($.unannReferenceType) }
]);
});

$.RULE("unannPrimitiveTypeWithOptionalDimsSuffix", () => {
$.SUBRULE($.unannPrimitiveType);
$.OPTION({
GATE: () => this.BACKTRACK_LOOKAHEAD($.isDims),
DEF: () => $.SUBRULE2($.dims)
});
});

// https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-UnannPrimitiveType
$.RULE("unannPrimitiveType", () => {
$.OR([
Expand Down
4 changes: 1 addition & 3 deletions packages/java-parser/src/productions/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ function defineRules($, t) {
{ ALT: () => $.SUBRULE($.literal) },
{ ALT: () => $.CONSUME(t.This) },
{ ALT: () => $.CONSUME(t.Void) },
// should be extracted to primitive type with optional dims suffix?
{ ALT: () => $.SUBRULE($.numericType) },
{ ALT: () => $.CONSUME(t.Boolean) },
{ ALT: () => $.SUBRULE($.unannPrimitiveTypeWithOptionalDimsSuffix) },
{ ALT: () => $.SUBRULE($.fqnOrRefType) },
{
GATE: () => isCastExpression,
Expand Down
5 changes: 5 additions & 0 deletions packages/java-parser/test/bugs-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ describe("The Java Parser fixed bugs", () => {
const input = "(left) < right";
expect(() => javaParser.parse(input, "expression")).to.not.throw();
});

it("issue #412 - should parse a double[][] as primaryPrefix", () => {
const input = "double[][]";
expect(() => javaParser.parse(input, "primaryPrefix")).to.not.throw();
});
});
1 change: 1 addition & 0 deletions packages/prettier-plugin-java/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = {
{ value: "variableInitializer" },
{ value: "unannType" },
{ value: "unannPrimitiveType" },
{ value: "unannPrimitiveTypeWithOptionalDimsSuffix" },
{ value: "unannReferenceType" },
{ value: "unannClassOrInterfaceType" },
{ value: "unannClassType" },
Expand Down
6 changes: 3 additions & 3 deletions packages/prettier-plugin-java/src/printers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ class ClassesPrettierVisitor {
}

unannType(ctx) {
if (ctx.unannReferenceType !== undefined) {
return this.visit(ctx.unannReferenceType);
}
return this.visitSingle(ctx);
}

unannPrimitiveTypeWithOptionalDimsSuffix(ctx) {
const unannPrimitiveType = this.visit(ctx.unannPrimitiveType);
const dims = this.visit(ctx.dims);

Expand Down
2 changes: 1 addition & 1 deletion packages/prettier-plugin-java/src/printers/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class ExpressionsPrettierVisitor {
}

primaryPrefix(ctx, params) {
if (ctx.This || ctx.Void || ctx.Boolean) {
if (ctx.This || ctx.Void) {
return printTokenWithComments(this.getSingle(ctx));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void printIf() {
}

if(myValue != 42 && 42/42 || myValue & 42 && myValue > 42 || myValue < 42 && myValue == 42) {

}

if(myValue != 42 && myValue == 42) {
Expand All @@ -103,11 +103,11 @@ public void printSwitch() {
}

switch(myValue != 42 && 42/42 || myValue & 42 && myValue > 42 || myValue < 42 && myValue == 42) {

}

switch(myValue != 42) {

}

switch(myValue != 42 && myValue == 42) {
Expand All @@ -117,17 +117,17 @@ public void printSwitch() {

public void printWhile() {
while/*infinite*/ (true) /*stop the program*/throw new RuntimeException();

while(myValue == 42 || myValue == 42 && myValue == 42 && myValue == 42 || myValue == 42 && myValue == 42) {

}

while(myValue != 42 && 42/42 || myValue & 42 && myValue > 42 || myValue < 42 && myValue == 42) {

}

while(myValue != 42) {

}

while(myValue != 42 && myValue == 42) {
Expand Down Expand Up @@ -178,9 +178,12 @@ public void longFullyQualifiedName() {
com
.me.very.very.very.very.very.very.very.very.very.very.very.very.very.longg.fully.qualified.name.FullyQualifiedName.builder()
.build();

com.FullyQualifiedName.builder();
}

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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,14 @@ public void longFullyQualifiedName() {

com.FullyQualifiedName.builder();
}

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);
}
}

0 comments on commit f9b4249

Please sign in to comment.