Skip to content

Commit

Permalink
refactor: remove duplicate code in printing arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Jan 3, 2020
1 parent 9723188 commit 6794bda
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
25 changes: 8 additions & 17 deletions packages/prettier-plugin-java/src/printers/arrays.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"use strict";
const { line } = require("prettier").doc.builders;
const { ifBreak } = require("./prettier-builder");
const {
rejectAndConcat,
rejectAndJoinSeps,
putIntoBraces
printArrayList
} = require("./printer-utils");

class ArraysPrettierVisitor {
Expand All @@ -13,21 +12,13 @@ class ArraysPrettierVisitor {
ctx.variableInitializerList
);

let optionalComma;
if (this.prettierOptions.trailingComma !== "none") {
optionalComma = ctx.Comma
? ifBreak(ctx.Comma[0], { ...ctx.Comma[0], image: "" })
: ifBreak(",", "");
} else {
optionalComma = ctx.Comma ? { ...ctx.Comma[0], image: "" } : "";
}

return putIntoBraces(
rejectAndConcat([optionalVariableInitializerList, optionalComma]),
line,
ctx.LCurly[0],
ctx.RCurly[0]
);
return printArrayList({
list: optionalVariableInitializerList,
extraComma: ctx.Comma,
LCurly: ctx.LCurly[0],
RCurly: ctx.RCurly[0],
trailingComma: this.prettierOptions.trailingComma
});
}

variableInitializerList(ctx) {
Expand Down
27 changes: 10 additions & 17 deletions packages/prettier-plugin-java/src/printers/interfaces.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { line, softline, hardline } = require("prettier").doc.builders;
const { concat, group, ifBreak, indent } = require("./prettier-builder");
const { concat, group, indent } = require("./prettier-builder");
const { printTokenWithComments } = require("./comments/format-comments");
const {
rejectAndConcat,
Expand All @@ -11,7 +11,8 @@ const {
getInterfaceBodyDeclarationsSeparator,
putIntoBraces,
displaySemicolon,
isStatementEmptyStatement
isStatementEmptyStatement,
printArrayList
} = require("./printer-utils");

class InterfacesPrettierVisitor {
Expand Down Expand Up @@ -276,21 +277,13 @@ class InterfacesPrettierVisitor {
elementValueArrayInitializer(ctx) {
const elementValueList = this.visit(ctx.elementValueList);

let optionalComma;
if (this.prettierOptions.trailingComma !== "none") {
optionalComma = ctx.Comma
? ifBreak(ctx.Comma[0], { ...ctx.Comma[0], image: "" })
: ifBreak(",", "");
} else {
optionalComma = ctx.Comma ? { ...ctx.Comma[0], image: "" } : "";
}

return putIntoBraces(
rejectAndConcat([elementValueList, optionalComma]),
line,
ctx.LCurly[0],
ctx.RCurly[0]
);
return printArrayList({
list: elementValueList,
extraComma: ctx.Comma,
LCurly: ctx.LCurly[0],
RCurly: ctx.RCurly[0],
trailingComma: this.prettierOptions.trailingComma
});
}

elementValueList(ctx) {
Expand Down
25 changes: 22 additions & 3 deletions packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use strict";
const _ = require("lodash");
const { join, concat, group } = require("./prettier-builder");
const { ifBreak, join, concat, group } = require("./prettier-builder");
const {
getTokenLeadingComments,
printTokenWithComments
} = require("./comments/format-comments");
const { hasComments } = require("./comments/comments-utils");
const { indent, hardline } = require("prettier").doc.builders;
const { indent, hardline, line } = require("prettier").doc.builders;

const orderedModifiers = [
"Public",
Expand Down Expand Up @@ -605,6 +605,24 @@ function isUniqueMethodInvocation(primarySuffixes) {
return count;
}

function printArrayList({ list, extraComma, LCurly, RCurly, trailingComma }) {
let optionalComma;
if (trailingComma !== "none") {
optionalComma = extraComma
? ifBreak(extraComma[0], { ...extraComma[0], image: "" })
: ifBreak(",", "");
} else {
optionalComma = extraComma ? { ...extraComma[0], image: "" } : "";
}

return putIntoBraces(
rejectAndConcat([list, optionalComma]),
line,
LCurly,
RCurly
);
}

module.exports = {
buildFqn,
reject,
Expand All @@ -629,5 +647,6 @@ module.exports = {
retrieveNodesToken,
isStatementEmptyStatement,
sortImports,
isUniqueMethodInvocation
isUniqueMethodInvocation,
printArrayList
};

0 comments on commit 6794bda

Please sign in to comment.