Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enums printing #266

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/prettier-plugin-java/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ test-samples/**/*
scripts/single-printer-run
samples/**/*
!test-samples/.eslintrc.json
!samples/.eslintrc.json
3 changes: 0 additions & 3 deletions packages/prettier-plugin-java/samples/.eslintrc.json

This file was deleted.

29 changes: 15 additions & 14 deletions packages/prettier-plugin-java/src/printers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const {
concat,
join,
group,
fill,
indent,
getImageWithComments
getImageWithComments,
getLeadingComments,
getTrailingComments
} = require("./prettier-builder");

class ClassesPrettierVisitor {
Expand Down Expand Up @@ -583,9 +584,7 @@ class ClassesPrettierVisitor {

enumBody(ctx) {
const enumConstantList = this.visit(ctx.enumConstantList);
const enumBodyDeclarations = ctx.enumBodyDeclarations
? this.visit(ctx.enumBodyDeclarations)
: ";";
const enumBodyDeclarations = this.visit(ctx.enumBodyDeclarations);

const optionnalComma = ctx.Comma
? ctx.Comma.map(elt => concat([elt, " "]))
Expand All @@ -606,12 +605,7 @@ class ClassesPrettierVisitor {
const enumConstants = this.mapVisit(ctx.enumConstant);
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];

const enumConstantsWithSeparators = [enumConstants[0]];
for (let i = 1; i < enumConstants.length; i++) {
enumConstantsWithSeparators.push(commas[i - 1], enumConstants[i]);
}

return fill(reject(enumConstantsWithSeparators));
return group(rejectAndJoinSeps(commas, enumConstants));
}

enumConstant(ctx) {
Expand Down Expand Up @@ -642,17 +636,24 @@ class ClassesPrettierVisitor {
}

enumBodyDeclarations(ctx) {
let content = "";
if (ctx.classBodyDeclaration !== undefined) {
const classBodyDeclaration = this.mapVisit(ctx.classBodyDeclaration);

const separators = getClassBodyDeclarationsSeparator(
ctx.classBodyDeclaration
);

content = rejectAndJoinSeps(separators, classBodyDeclaration);
return rejectAndJoin(line, [
ctx.Semicolon[0],
rejectAndJoinSeps(separators, classBodyDeclaration)
]);
}
return rejectAndJoin(line, [ctx.Semicolon[0], content]);

return concat([
getLeadingComments(ctx.Semicolon[0]),
"",
getTrailingComments(ctx.Semicolon[0])
]);
}

isClassDeclaration(ctx) {
Expand Down
18 changes: 17 additions & 1 deletion packages/prettier-plugin-java/src/printers/prettier-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const prettier = require("prettier").doc.builders;
const hardLineWithoutBreakParent = { type: "line", hard: true };

function getImageWithComments(token) {
return concat([
getLeadingComments(token),
token.image,
getTrailingComments(token)
]);
}

function getLeadingComments(token) {
const arr = [];
if (Object.prototype.hasOwnProperty.call(token, "leadingComments")) {
token.leadingComments.forEach(element => {
Expand All @@ -16,7 +24,12 @@ function getImageWithComments(token) {
}
});
}
arr.push(token.image);

return concat(arr);
}

function getTrailingComments(token) {
const arr = [];
if (Object.prototype.hasOwnProperty.call(token, "trailingComments")) {
if (token.trailingComments[0].startLine !== token.startLine) {
arr.push(hardLineWithoutBreakParent);
Expand Down Expand Up @@ -44,6 +57,7 @@ function getImageWithComments(token) {
arr.pop();
}
}

return concat(arr);
}

Expand Down Expand Up @@ -125,5 +139,7 @@ module.exports = {
indent,
dedent,
getImageWithComments,
getLeadingComments,
getTrailingComments,
hardLineWithoutBreakParent
};
6 changes: 5 additions & 1 deletion packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ function getCSTNodeStartEndToken(ctx) {
}

function isStatementEmptyStatement(statement) {
return statement.type === "concat" && statement.parts[0] === ";";
return (
statement.type === "concat" &&
statement.parts[0] === "" &&
statement.parts[1] === ";"
);
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ private synchronized void myFunction(int arg1, int arg2/*overloading*/) {
//Additionnal enumeration
enum Cards {
//The Heart and the Spade
HEART/*the heart*/, SPADES/*and the spade*/;
HEART/*the heart*/, SPADES/*and the spade*/
}
22 changes: 21 additions & 1 deletion packages/prettier-plugin-java/test/unit-test/enum/_input.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
public enum Enum {
public enum EnumWhichNotBreak {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM

}

public enum EnumWhichNotBreakWithExtraSemicolon {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;

}

public enum EnumWhichBreak {

ONE_VALUE, TWO_VALUE, THREE_VALUE, FOUR_VALUE, FIVE_VALUE, SIX_VALUE, SEVEN_VALUE, EIGTH_VALUE, NINE_VALUE,
TEN_VALUE

}

public enum EnumWhichBreakWithExtraSemicolon {

ONE_VALUE, TWO_VALUE, THREE_VALUE, FOUR_VALUE, FIVE_VALUE, SIX_VALUE, SEVEN_VALUE, EIGTH_VALUE, NINE_VALUE,
TEN_VALUE;

}

public enum Enum {

THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");
Expand Down
36 changes: 33 additions & 3 deletions packages/prettier-plugin-java/test/unit-test/enum/_output.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
public enum Enum {
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;
public enum EnumWhichNotBreak {
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM
}

public enum EnumWhichNotBreakWithExtraSemicolon {
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM
}

public enum EnumWhichBreak {
ONE_VALUE,
TWO_VALUE,
THREE_VALUE,
FOUR_VALUE,
FIVE_VALUE,
SIX_VALUE,
SEVEN_VALUE,
EIGTH_VALUE,
NINE_VALUE,
TEN_VALUE
}

public enum EnumWhichBreakWithExtraSemicolon {
ONE_VALUE,
TWO_VALUE,
THREE_VALUE,
FOUR_VALUE,
FIVE_VALUE,
SIX_VALUE,
SEVEN_VALUE,
EIGTH_VALUE,
NINE_VALUE,
TEN_VALUE
}

public enum Enum {
Expand All @@ -20,6 +50,6 @@ public String toString() {
class CLassWithEnum {

public static enum VALID_THINGS {
FIRST, SECOND;
FIRST, SECOND
}
}