prettier-plugin-java
Advanced tools
Comparing version 0.6.0 to 0.7.0
{ | ||
"name": "prettier-plugin-java", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Prettier Java Plugin", | ||
@@ -9,3 +9,3 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"java-parser": "^0.6.0", | ||
"java-parser": "^0.7.0", | ||
"lodash": "4.17.15", | ||
@@ -23,3 +23,4 @@ "prettier": "1.18.2" | ||
"clone-samples": "node scripts/clone-samples.js" | ||
} | ||
}, | ||
"gitHead": "7cf83bececc20e6c658a8b07df5d8dd056a3711f" | ||
} |
@@ -21,3 +21,5 @@ "use strict"; | ||
} = require("./printers/packages-and-modules"); | ||
const { printNodeWithComments } = require("./printers/comments"); | ||
const { | ||
printNodeWithComments | ||
} = require("./printers/comments/format-comments"); | ||
@@ -90,3 +92,6 @@ class CstPrettierPrinter extends BaseJavaCstVisitor { | ||
return this.originalText.substring(startOffset, endOffset + 1); | ||
return this.prettierOptions.originalText.substring( | ||
startOffset, | ||
endOffset + 1 | ||
); | ||
} catch (e) { | ||
@@ -134,4 +139,4 @@ throw Error( | ||
// see https://github.com/prettier/prettier/issues/5747 | ||
function createPrettierDoc(cstNode, originalText) { | ||
prettyPrinter.originalText = originalText; | ||
function createPrettierDoc(cstNode, options) { | ||
prettyPrinter.prettierOptions = options; | ||
return prettyPrinter.visit(cstNode); | ||
@@ -138,0 +143,0 @@ } |
@@ -29,5 +29,5 @@ "use strict"; | ||
return createPrettierDoc(node, options.originalText); | ||
return createPrettierDoc(node, options); | ||
} | ||
module.exports = genericPrint; |
@@ -6,3 +6,3 @@ "use strict"; | ||
rejectAndJoinSeps, | ||
putIntoCurlyBraces | ||
printArrayList | ||
} = require("./printer-utils"); | ||
@@ -15,10 +15,10 @@ | ||
); | ||
const optionalComma = ctx.Comma ? ctx.Comma[0] : ""; | ||
return putIntoCurlyBraces( | ||
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 | ||
}); | ||
} | ||
@@ -25,0 +25,0 @@ |
@@ -5,4 +5,8 @@ "use strict"; | ||
const { group, indent, concat, join } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
const { | ||
hasLeadingLineComments, | ||
hasTrailingLineComments | ||
} = require("./comments/comments-utils"); | ||
const { | ||
displaySemicolon, | ||
@@ -15,7 +19,4 @@ rejectAndConcat, | ||
putIntoBraces, | ||
putIntoCurlyBraces, | ||
isStatementEmptyStatement, | ||
sortModifiers, | ||
hasTrailingLineComments, | ||
hasLeadingLineComments | ||
sortModifiers | ||
} = require("./printer-utils"); | ||
@@ -27,3 +28,3 @@ | ||
return putIntoCurlyBraces( | ||
return putIntoBraces( | ||
blockStatements, | ||
@@ -100,3 +101,5 @@ hardline, | ||
newLabelStatement.leadingComments = labeledStatementLeadingComments; | ||
if (labeledStatementLeadingComments.length !== 0) { | ||
newLabelStatement.leadingComments = labeledStatementLeadingComments; | ||
} | ||
newLabelStatement.children.Colon[0] = newColon; | ||
@@ -200,4 +203,4 @@ newLabelStatement.children.statement[0] = newStatement; | ||
return putIntoBraces( | ||
rejectAndJoin(line, switchCases), | ||
line, | ||
rejectAndJoin(hardline, switchCases), | ||
hardline, | ||
ctx.LCurly[0], | ||
@@ -212,3 +215,3 @@ ctx.RCurly[0] | ||
return indent(rejectAndJoin(line, [switchLabel, blockStatements])); | ||
return indent(rejectAndJoin(hardline, [switchLabel, blockStatements])); | ||
} | ||
@@ -215,0 +218,0 @@ |
@@ -5,3 +5,3 @@ "use strict"; | ||
const { | ||
hasLeadingLineComments, | ||
getBlankLinesSeparator, | ||
reject, | ||
@@ -15,3 +15,2 @@ rejectAndConcat, | ||
putIntoBraces, | ||
putIntoCurlyBraces, | ||
getClassBodyDeclarationsSeparator, | ||
@@ -21,3 +20,4 @@ isStatementEmptyStatement | ||
const { concat, join, group, indent } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
const { hasLeadingLineComments } = require("./comments/comments-utils"); | ||
@@ -143,3 +143,3 @@ class ClassesPrettierVisitor { | ||
return putIntoCurlyBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0]); | ||
return putIntoBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0]); | ||
} | ||
@@ -615,3 +615,3 @@ | ||
return putIntoCurlyBraces( | ||
return putIntoBraces( | ||
rejectAndJoin(hardline, [explicitConstructorInvocation, blockStatements]), | ||
@@ -682,5 +682,10 @@ hardline, | ||
const optionalComma = ctx.Comma ? { ...ctx.Comma[0], image: "" } : ""; | ||
let optionalComma; | ||
if (this.prettierOptions.trailingComma !== "none") { | ||
optionalComma = ctx.Comma ? ctx.Comma[0] : ","; | ||
} else { | ||
optionalComma = ctx.Comma ? { ...ctx.Comma[0], image: "" } : ""; | ||
} | ||
return putIntoCurlyBraces( | ||
return putIntoBraces( | ||
rejectAndConcat([enumConstantList, optionalComma, enumBodyDeclarations]), | ||
@@ -695,4 +700,6 @@ hardline, | ||
const enumConstants = this.mapVisit(ctx.enumConstant); | ||
const blankLineSeparators = getBlankLinesSeparator(ctx.enumConstant); | ||
const commas = ctx.Comma | ||
? ctx.Comma.map(elt => concat([elt, hardline])) | ||
? ctx.Comma.map((elt, index) => concat([elt, blankLineSeparators[index]])) | ||
: []; | ||
@@ -738,3 +745,3 @@ | ||
return rejectAndJoin(line, [ | ||
return rejectAndJoin(concat([hardline, hardline]), [ | ||
ctx.Semicolon[0], | ||
@@ -741,0 +748,0 @@ rejectAndJoinSeps(separators, classBodyDeclaration) |
@@ -80,14 +80,30 @@ "use strict"; | ||
if (Object.prototype.hasOwnProperty.call(nodeOrToken, "leadingComments")) { | ||
nodeOrToken.leadingComments.forEach(comment => { | ||
let previousEndLine = nodeOrToken.leadingComments[0].endLine; | ||
let step; | ||
arr.push(concat(formatComment(nodeOrToken.leadingComments[0]))); | ||
for (let i = 1; i < nodeOrToken.leadingComments.length; i++) { | ||
step = nodeOrToken.leadingComments[i].startLine - previousEndLine; | ||
if ( | ||
comment.tokenType.name === "LineComment" || | ||
comment.endLine !== location.startLine || | ||
comment.startOffset > location.startOffset | ||
step === 1 || | ||
nodeOrToken.leadingComments[i].startOffset > location.startOffset | ||
) { | ||
arr.push(concat(formatComment(comment))); | ||
arr.push(hardline); | ||
} else { | ||
arr.push(concat(formatComment(comment))); | ||
} else if (step > 1) { | ||
arr.push(hardline, hardline); | ||
} | ||
}); | ||
arr.push(concat(formatComment(nodeOrToken.leadingComments[i]))); | ||
previousEndLine = nodeOrToken.leadingComments[i].endLine; | ||
} | ||
step = location.startLine - previousEndLine; | ||
if ( | ||
step === 1 || | ||
nodeOrToken.leadingComments[nodeOrToken.leadingComments.length - 1] | ||
.startOffset > location.startOffset | ||
) { | ||
arr.push(hardline); | ||
} else if (step > 1) { | ||
arr.push(hardline, hardline); | ||
} | ||
} | ||
@@ -209,2 +225,3 @@ | ||
module.exports = { | ||
getTokenLeadingComments, | ||
processComments, | ||
@@ -211,0 +228,0 @@ printTokenWithComments, |
@@ -6,4 +6,7 @@ "use strict"; | ||
const { concat, group, indent } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
const { | ||
handleCommentsBinaryExpression | ||
} = require("./comments/handle-comments"); | ||
const { | ||
matchCategory, | ||
@@ -162,2 +165,4 @@ rejectAndJoin, | ||
binaryExpression(ctx, params) { | ||
handleCommentsBinaryExpression(ctx); | ||
const referenceType = this.mapVisit(ctx.referenceType); | ||
@@ -164,0 +169,0 @@ const expression = this.mapVisit(ctx.expression); |
@@ -5,3 +5,3 @@ "use strict"; | ||
const { concat, group, indent } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
const { | ||
@@ -14,5 +14,5 @@ rejectAndConcat, | ||
putIntoBraces, | ||
putIntoCurlyBraces, | ||
displaySemicolon, | ||
isStatementEmptyStatement | ||
isStatementEmptyStatement, | ||
printArrayList | ||
} = require("./printer-utils"); | ||
@@ -96,3 +96,3 @@ | ||
} | ||
return putIntoCurlyBraces( | ||
return putIntoBraces( | ||
joinedInterfaceMemberDeclaration, | ||
@@ -282,12 +282,10 @@ hardline, | ||
const elementValueList = this.visit(ctx.elementValueList); | ||
const comma = ctx.Comma ? ctx.Comma[0] : ""; | ||
return group( | ||
rejectAndConcat([ | ||
ctx.LCurly[0], | ||
indent(rejectAndConcat([line, elementValueList, comma])), | ||
line, | ||
ctx.RCurly[0] | ||
]) | ||
); | ||
return printArrayList({ | ||
list: elementValueList, | ||
extraComma: ctx.Comma, | ||
LCurly: ctx.LCurly[0], | ||
RCurly: ctx.RCurly[0], | ||
trailingComma: this.prettierOptions.trailingComma | ||
}); | ||
} | ||
@@ -294,0 +292,0 @@ |
"use strict"; | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
@@ -4,0 +4,0 @@ class LexicalStructurePrettierVisitor { |
"use strict"; | ||
const { buildFqn } = require("./printer-utils"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
@@ -6,0 +6,0 @@ class NamesPrettierVisitor { |
@@ -5,3 +5,3 @@ "use strict"; | ||
const { concat, join } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
const { | ||
@@ -13,3 +13,3 @@ buildFqn, | ||
displaySemicolon, | ||
putIntoCurlyBraces, | ||
putIntoBraces, | ||
getBlankLinesSeparator, | ||
@@ -24,8 +24,3 @@ sortImports | ||
// Do not add additional line if only comments in file | ||
const additionalLine = isNaN(compilationUnit[0].location.startOffset) | ||
? "" | ||
: line; | ||
return concat([this.visit(compilationUnit[0]), additionalLine]); | ||
return concat([this.visit(compilationUnit[0]), line]); | ||
} | ||
@@ -122,3 +117,3 @@ | ||
name, | ||
putIntoCurlyBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0]) | ||
putIntoBraces(content, hardline, ctx.LCurly[0], ctx.RCurly[0]) | ||
]); | ||
@@ -125,0 +120,0 @@ } |
"use strict"; | ||
const prettier = require("prettier").doc.builders; | ||
const { processComments } = require("./comments"); | ||
const { processComments } = require("./comments/format-comments"); | ||
@@ -45,2 +45,9 @@ /* | ||
function ifBreak(breakContents, flatContents) { | ||
return prettier.ifBreak( | ||
processComments(breakContents), | ||
processComments(flatContents) | ||
); | ||
} | ||
module.exports = { | ||
@@ -52,3 +59,4 @@ concat, | ||
indent, | ||
dedent | ||
dedent, | ||
ifBreak | ||
}; |
"use strict"; | ||
const _ = require("lodash"); | ||
const { join, concat, group } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { indent, hardline } = require("prettier").doc.builders; | ||
const { ifBreak, join, concat, group } = require("./prettier-builder"); | ||
const { | ||
getTokenLeadingComments, | ||
printTokenWithComments | ||
} = require("./comments/format-comments"); | ||
const { hasComments } = require("./comments/comments-utils"); | ||
const { indent, hardline, line } = require("prettier").doc.builders; | ||
@@ -232,32 +236,2 @@ const orderedModifiers = [ | ||
function hasLeadingComments(token) { | ||
return token.leadingComments !== undefined; | ||
} | ||
function hasTrailingComments(token) { | ||
return token.trailingComments !== undefined; | ||
} | ||
function hasLeadingLineComments(token) { | ||
return ( | ||
token.leadingComments !== undefined && | ||
token.leadingComments.length !== 0 && | ||
token.leadingComments[token.leadingComments.length - 1].tokenType.name === | ||
"LineComment" | ||
); | ||
} | ||
function hasTrailingLineComments(token) { | ||
return ( | ||
token.trailingComments !== undefined && | ||
token.trailingComments.length !== 0 && | ||
token.trailingComments[token.trailingComments.length - 1].tokenType.name === | ||
"LineComment" | ||
); | ||
} | ||
function hasComments(token) { | ||
return hasLeadingComments(token) || hasTrailingComments(token); | ||
} | ||
function isExplicitLambdaParameter(ctx) { | ||
@@ -280,6 +254,13 @@ return ( | ||
for (let i = 0; i < ctx.length - 1; i++) { | ||
const previousRuleEndLine = ctx[i].location.endLine; | ||
const nextRuleStartLine = ctx[i + 1].location.startLine; | ||
const previousRuleEndLineWithComment = | ||
ctx[i].trailingComments !== undefined | ||
? ctx[i].trailingComments[ctx[i].trailingComments.length - 1].endLine | ||
: ctx[i].location.endLine; | ||
if (nextRuleStartLine > previousRuleEndLine + 1) { | ||
const nextRuleStartLineWithComment = | ||
ctx[i + 1].leadingComments !== undefined | ||
? ctx[i + 1].leadingComments[0].startLine | ||
: ctx[i + 1].location.startLine; | ||
if (nextRuleStartLineWithComment - previousRuleEndLineWithComment > 1) { | ||
separators.push(concat([hardline, hardline])); | ||
@@ -299,25 +280,43 @@ } else { | ||
) { | ||
const separators = []; | ||
const additionalBlankLines = []; | ||
const declarationsWithoutEmptyStatements = declarations.filter( | ||
declaration => !isSemicolon(declaration) | ||
); | ||
declarations.forEach(declaration => { | ||
if (needLineDeclaration(declaration)) { | ||
additionalBlankLines.push(hardline); | ||
} else { | ||
additionalBlankLines.push(""); | ||
} | ||
}); | ||
const userBlankLinesSeparators = getBlankLinesSeparator( | ||
declarationsWithoutEmptyStatements | ||
); | ||
const additionalBlankLines = declarationsWithoutEmptyStatements.map( | ||
needLineDeclaration | ||
); | ||
const userBlankLinesSeparators = getBlankLinesSeparator(declarations); | ||
const separators = []; | ||
let indexNextNotEmptyDeclaration = 0; | ||
for (let i = 0; i < declarations.length - 1; i++) { | ||
if (!isSemicolon(declarations[i])) { | ||
// if the empty statement has comments | ||
// we want to print them on their own line | ||
if (isSemicolon(declarations[i])) { | ||
if (hasComments(declarations[i])) { | ||
separators.push(hardline); | ||
} | ||
} else if ( | ||
indexNextNotEmptyDeclaration < | ||
declarationsWithoutEmptyStatements.length - 1 | ||
) { | ||
const isTwoHardLines = | ||
userBlankLinesSeparators[i].parts[0].type === "concat"; | ||
userBlankLinesSeparators[indexNextNotEmptyDeclaration].parts[0].type === | ||
"concat"; | ||
const additionalSep = | ||
!isTwoHardLines && | ||
(additionalBlankLines[i + 1] !== "" || additionalBlankLines[i] !== "") | ||
(additionalBlankLines[indexNextNotEmptyDeclaration + 1] || | ||
additionalBlankLines[indexNextNotEmptyDeclaration]) | ||
? hardline | ||
: ""; | ||
separators.push(concat([userBlankLinesSeparators[i], additionalSep])); | ||
separators.push( | ||
concat([ | ||
userBlankLinesSeparators[indexNextNotEmptyDeclaration], | ||
additionalSep | ||
]) | ||
); | ||
indexNextNotEmptyDeclaration += 1; | ||
} | ||
@@ -417,4 +416,26 @@ } | ||
function putIntoBraces(argument, separator, LBrace, RBrace) { | ||
const rightBraceLeadingComments = getTokenLeadingComments(RBrace); | ||
const lastBreakLine = | ||
// check if last element of the array is a line | ||
rightBraceLeadingComments.length !== 0 && | ||
rightBraceLeadingComments[rightBraceLeadingComments.length - 1] === hardline | ||
? rightBraceLeadingComments.pop() | ||
: separator; | ||
delete RBrace.leadingComments; | ||
let contentInsideBraces; | ||
if (argument === undefined || argument === "") { | ||
return concat([LBrace, RBrace]); | ||
if (rightBraceLeadingComments.length === 0) { | ||
return concat([LBrace, RBrace]); | ||
} | ||
contentInsideBraces = [separator, ...rightBraceLeadingComments]; | ||
} else if (rightBraceLeadingComments.length !== 0) { | ||
contentInsideBraces = [ | ||
separator, | ||
argument, | ||
separator, | ||
...rightBraceLeadingComments | ||
]; | ||
} else { | ||
contentInsideBraces = [separator, argument]; | ||
} | ||
@@ -425,4 +446,4 @@ | ||
LBrace, | ||
indent(concat([separator, argument])), | ||
separator, | ||
indent(concat(contentInsideBraces)), | ||
lastBreakLine, | ||
RBrace | ||
@@ -433,14 +454,2 @@ ]) | ||
function putIntoCurlyBraces(argument, separator, LBrace, RBrace) { | ||
if (argument !== undefined && argument !== "") { | ||
return putIntoBraces(argument, separator, LBrace, RBrace); | ||
} | ||
if (hasTrailingComments(LBrace) || hasLeadingComments(RBrace)) { | ||
return concat([LBrace, indent(hardline), RBrace]); | ||
} | ||
return concat([LBrace, RBrace]); | ||
} | ||
const andOrBinaryOperators = new Set(["&&", "||", "&", "|", "^"]); | ||
@@ -623,2 +632,20 @@ function separateTokensIntoGroups(ctx) { | ||
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 = { | ||
@@ -636,4 +663,2 @@ buildFqn, | ||
findDeepElementInPartsArray, | ||
hasLeadingLineComments, | ||
hasTrailingLineComments, | ||
isExplicitLambdaParameter, | ||
@@ -644,3 +669,2 @@ getBlankLinesSeparator, | ||
putIntoBraces, | ||
putIntoCurlyBraces, | ||
getInterfaceBodyDeclarationsSeparator, | ||
@@ -653,3 +677,4 @@ getClassBodyDeclarationsSeparator, | ||
sortImports, | ||
isUniqueMethodInvocation | ||
isUniqueMethodInvocation, | ||
printArrayList | ||
}; |
@@ -6,3 +6,3 @@ "use strict"; | ||
const { concat, join } = require("./prettier-builder"); | ||
const { printTokenWithComments } = require("./comments"); | ||
const { printTokenWithComments } = require("./comments/format-comments"); | ||
const { | ||
@@ -9,0 +9,0 @@ rejectAndJoin, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128425
22
3677
+ Addedjava-parser@0.7.1(transitive)
- Removedjava-parser@0.6.0(transitive)
Updatedjava-parser@^0.7.0