prettier-java-next-line 2.10.3 → 2.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,99 +1,84 @@
1
1
  # Prettier Java Next Line
2
2
 
3
- Prettier Java Next Line is a fork of
4
- [prettier-plugin-java](https://github.com/jhipster/prettier-java) with
5
- configurable opening-brace placement. It defaults to Allman-style, next-line
6
- braces.
3
+ Prettier Java Next Line forks [prettier-plugin-java](https://github.com/jhipster/prettier-java) to make brace placement configurable, with next-line braces as the default.
7
4
 
8
- The option was proposed upstream in
9
- [jhipster/prettier-java#840](https://github.com/jhipster/prettier-java/pull/840)
10
- and declined because upstream intentionally preserves one brace style.
5
+ [Upstream declined the option](https://github.com/jhipster/prettier-java/pull/840) because Prettier Java intentionally supports one brace style.
11
6
 
12
- ## Installation
7
+ ## Why next line is the default
13
8
 
14
- Use it with Prettier:
9
+ Same-line formatting made sense on small, low-resolution CRT displays where every row counted.
15
10
 
16
- ```bash
17
- npm install --save-dev prettier prettier-java-next-line
18
- ```
11
+ On a modern display, we would rather spend one extra line and see the shape of the block.
19
12
 
20
- Or use the matching Prettier Next Line fork through an npm alias:
13
+ ## Install
14
+
15
+ Follow the [upstream instructions](https://github.com/jhipster/prettier-java#readme), replacing `prettier-plugin-java` with `prettier-java-next-line`:
21
16
 
22
17
  ```bash
23
- npm install --save-dev \
24
- prettier@npm:prettier-next-line \
25
- prettier-java-next-line
18
+ npm install --save-dev prettier prettier-java-next-line
26
19
  ```
27
20
 
28
- ## Configuration
29
-
30
21
  Load the plugin explicitly:
31
22
 
32
23
  ```json
33
24
  {
34
- "plugins": ["prettier-java-next-line"]
25
+ "plugins": ["prettier-java-next-line"]
35
26
  }
36
27
  ```
37
28
 
38
- Next-line braces are the default. Use the inherited K&R placement when needed:
29
+ Set `braceStyle` only when a project needs the inherited same-line style:
39
30
 
40
31
  ```json
41
32
  {
42
- "plugins": ["prettier-java-next-line"],
43
- "braceStyle": "same-line"
33
+ "plugins": ["prettier-java-next-line"],
34
+ "braceStyle": "same-line"
44
35
  }
45
36
  ```
46
37
 
38
+ To use the matching Prettier runtime fork too, install it under the name `prettier` with an npm alias:
39
+
40
+ ```bash
41
+ npm install --save-dev \
42
+ prettier@npm:prettier-next-line \
43
+ prettier-java-next-line
44
+ ```
45
+
47
46
  ## Spotless Maven
48
47
 
49
- Spotless loads a package named `prettier`, so install Prettier Next Line under
50
- that name with an npm alias:
48
+ Spotless loads a package named `prettier`, so install Prettier Next Line under that name with an npm alias:
51
49
 
52
50
  ```xml
53
51
  <prettier>
54
- <devDependencies>
55
- <prettier>npm:prettier-next-line@3.10.0-dev</prettier>
56
- <prettier-java-next-line>2.10.3-beta.1</prettier-java-next-line>
57
- </devDependencies>
58
- <configFile>.prettierrc.json5</configFile>
59
- <config>
60
- <parser>java</parser>
61
- <plugins>prettier-java-next-line</plugins>
62
- </config>
52
+ <devDependencies>
53
+ <prettier>npm:prettier-next-line@3.10.0</prettier>
54
+ <prettier-java-next-line>2.10.3</prettier-java-next-line>
55
+ </devDependencies>
56
+ <configFile>.prettierrc.json5</configFile>
57
+ <config>
58
+ <parser>java</parser>
59
+ <plugins>prettier-java-next-line</plugins>
60
+ </config>
63
61
  </prettier>
64
62
  ```
65
63
 
66
- ## CLI
67
-
68
- With upstream Prettier:
69
-
70
- ```bash
71
- npx prettier --check --plugin=prettier-java-next-line "**/*.java"
72
- npx prettier --write --plugin=prettier-java-next-line "**/*.java"
73
- ```
74
-
75
- With the aliased Prettier Next Line fork, use `npx prettier-next-line` instead.
76
-
77
64
  ## Example
78
65
 
79
66
  ```java
80
67
  public class Example
81
68
  {
82
- public void method()
83
- {
84
- if (condition)
85
- {
86
- doSomething();
87
- }
88
- }
69
+ public void method()
70
+ {
71
+ if (condition)
72
+ {
73
+ doSomething();
74
+ }
75
+ }
89
76
  }
90
77
  ```
91
78
 
92
79
  ## Upstream
93
80
 
94
- Prettier Java Next Line tracks
95
- [jhipster/prettier-java](https://github.com/jhipster/prettier-java) and keeps
96
- fork-specific behavior focused on brace placement.
81
+ Except for brace placement, behavior comes from [Prettier Java](https://github.com/jhipster/prettier-java#readme).
97
82
 
98
83
  ## License
99
84
 
package/dist/index.cjs CHANGED
@@ -125,6 +125,9 @@ function printComment(comment) {
125
125
  function hasLeadingComments(node) {
126
126
  return node.comments?.some(({ leading }) => leading) ?? false;
127
127
  }
128
+ function hasDanglingComments(node) {
129
+ return node.comments?.some(({ leading, trailing }) => !leading && !trailing) ?? false;
130
+ }
128
131
  function indentInParentheses(contents) {
129
132
  return contents && !Array.isArray(contents) || contents.length ? [
130
133
  "(",
@@ -292,7 +295,7 @@ function printVariableDeclaration(path, print) {
292
295
  }
293
296
  function printAssignment(leftDoc, operator, rightDoc, rightNode) {
294
297
  if (!rightDoc || !rightNode) return leftDoc;
295
- if (rightNode.type === "binary_expression" || rightNode.type === "instanceof_expression" || rightNode.type === "ternary_expression" && (rightNode.conditionNode.type === "binary_expression" || rightNode.conditionNode.type === "instanceof_expression") || hasLeadingComments(rightNode)) return group$7([
298
+ if (rightNode.type === "binary_expression" || rightNode.type === "instanceof_expression" || rightNode.type === "ternary_expression" && (rightNode.conditionNode.type === "binary_expression" || rightNode.conditionNode.type === "instanceof_expression") || isMember(rightNode) && hasDanglingComments(rightNode) || !isMember(rightNode) && hasLeadingComments(rightNode)) return group$7([
296
299
  leftDoc,
297
300
  operator,
298
301
  group$7(indent$7([line$7, rightDoc]))
@@ -463,9 +466,7 @@ function parentNeedsParentheses(path) {
463
466
  const { parent } = path;
464
467
  switch (parent?.type) {
465
468
  case "return_statement":
466
- case "throw_statement":
467
- if (willReturnOrThrowStatementBreak(path)) return false;
468
- break;
469
+ case "throw_statement": if (willReturnOrThrowStatementBreak(path)) return false;
469
470
  }
470
471
  }
471
472
  function willReturnOrThrowStatementBreak(path) {
@@ -491,7 +492,7 @@ function findBaseIndent(lines) {
491
492
  return Math.min(...lines.map((line) => line.search(/\S/)).filter((indent) => indent >= 0));
492
493
  }
493
494
  function findEmbeddedLanguage(path) {
494
- return path.ancestors.find(({ type, comments }) => type === "block" || comments?.some(({ leading }) => leading))?.comments?.filter(({ leading }) => leading).map(({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]).findLast((language) => language)?.toLowerCase();
495
+ return [path.node, ...path.ancestors].find(({ type, comments }) => type === "block" || comments?.some(({ leading }) => leading))?.comments?.filter(({ leading }) => leading).map(({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]).findLast((language) => language)?.toLowerCase();
495
496
  }
496
497
  function escapeDocForTextBlock(doc) {
497
498
  return mapDoc(doc, (currentDoc) => typeof currentDoc === "string" ? currentDoc.replace(/\\|"""/g, (match) => match === "\\" ? "\\\\" : "\"\"\\\"") : currentDoc);
@@ -1453,7 +1454,7 @@ function shouldPrintParamsWithoutParens(path, options) {
1453
1454
  return false;
1454
1455
  }
1455
1456
  function canPrintParamsWithoutParens(node) {
1456
- return node.parametersNode.type === "identifier" && !node.comments?.some(({ leading, trailing }) => !leading && !trailing) && !node.parametersNode.comments;
1457
+ return node.parametersNode.type === "identifier" && !hasDanglingComments(node) && !node.parametersNode.comments;
1457
1458
  }
1458
1459
  function printMemberChain(path, print, options) {
1459
1460
  const isExpressionStatement = path.parent?.type === "expression_statement";
@@ -1948,9 +1949,7 @@ var packages_and_modules_default = {
1948
1949
  annotations.push(print(child));
1949
1950
  break;
1950
1951
  case "identifier":
1951
- case "scoped_identifier":
1952
- identifier.push(print(child));
1953
- break;
1952
+ case "scoped_identifier": identifier.push(print(child));
1954
1953
  }
1955
1954
  }, "namedChildren");
1956
1955
  return join$1(hardline$1, [...annotations, [
@@ -2281,9 +2280,10 @@ function handleLabeledStatementComments(commentNode) {
2281
2280
  function handleLambdaExpressionComments(commentNode) {
2282
2281
  const { enclosingNode, precedingNode, followingNode } = commentNode;
2283
2282
  if (enclosingNode?.type === "lambda_expression" && precedingNode && followingNode && enclosingNode.children.find(({ type }) => type === "->").end.index < commentNode.start.index) {
2284
- if (followingNode.type === "block") if (followingNode.namedChildren.length) prettier.util.addLeadingComment(followingNode.namedChildren[0], commentNode);
2285
- else prettier.util.addDanglingComment(followingNode, commentNode, void 0);
2286
- else prettier.util.addLeadingComment(followingNode, commentNode);
2283
+ if (followingNode.type === "block") {
2284
+ if (followingNode.namedChildren.length) prettier.util.addLeadingComment(followingNode.namedChildren[0], commentNode);
2285
+ else prettier.util.addDanglingComment(followingNode, commentNode, void 0);
2286
+ } else prettier.util.addLeadingComment(followingNode, commentNode);
2287
2287
  return true;
2288
2288
  }
2289
2289
  }
@@ -2366,8 +2366,10 @@ function printLeadingComment(path) {
2366
2366
  const originalText = path.root.value;
2367
2367
  if (comment.type === "block_comment") {
2368
2368
  let lineBreak = " ";
2369
- if (hasNewline(originalText, parser_default.locEnd(comment))) if (hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) lineBreak = hardline;
2370
- else lineBreak = line;
2369
+ if (hasNewline(originalText, parser_default.locEnd(comment))) {
2370
+ if (hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) lineBreak = hardline;
2371
+ else lineBreak = line;
2372
+ }
2371
2373
  parts.push(lineBreak);
2372
2374
  } else parts.push(hardline);
2373
2375
  const index = skipNewline(originalText, skipSpaces(originalText, parser_default.locEnd(comment)));
@@ -2527,8 +2529,10 @@ function processTree(node, fieldName = null, comments) {
2527
2529
  const javaChild = processTree(child, fieldName, comments);
2528
2530
  javaNode.children.push(javaChild);
2529
2531
  if (javaChild.isNamed) javaNode.namedChildren.push(javaChild);
2530
- if (fieldName) if (multiFields?.[fieldName]) javaNode[`${fieldName}Nodes`].push(javaChild);
2531
- else javaNode[`${fieldName}Node`] = javaChild;
2532
+ if (fieldName) {
2533
+ if (multiFields?.[fieldName]) javaNode[`${fieldName}Nodes`].push(javaChild);
2534
+ else javaNode[`${fieldName}Node`] = javaChild;
2535
+ }
2532
2536
  }
2533
2537
  });
2534
2538
  });
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AstPath, ParserOptions } from "prettier";
2
- import { builders } from "prettier/doc";
2
+ import { builders as builders$1 } from "prettier/doc";
3
3
  //#region src/node-types.d.ts
4
4
  interface Point {
5
5
  index: number;
package/dist/index.mjs CHANGED
@@ -125,6 +125,9 @@ function printComment(comment) {
125
125
  function hasLeadingComments(node) {
126
126
  return node.comments?.some(({ leading }) => leading) ?? false;
127
127
  }
128
+ function hasDanglingComments(node) {
129
+ return node.comments?.some(({ leading, trailing }) => !leading && !trailing) ?? false;
130
+ }
128
131
  function indentInParentheses(contents) {
129
132
  return contents && !Array.isArray(contents) || contents.length ? [
130
133
  "(",
@@ -292,7 +295,7 @@ function printVariableDeclaration(path, print) {
292
295
  }
293
296
  function printAssignment(leftDoc, operator, rightDoc, rightNode) {
294
297
  if (!rightDoc || !rightNode) return leftDoc;
295
- if (rightNode.type === "binary_expression" || rightNode.type === "instanceof_expression" || rightNode.type === "ternary_expression" && (rightNode.conditionNode.type === "binary_expression" || rightNode.conditionNode.type === "instanceof_expression") || hasLeadingComments(rightNode)) return group$7([
298
+ if (rightNode.type === "binary_expression" || rightNode.type === "instanceof_expression" || rightNode.type === "ternary_expression" && (rightNode.conditionNode.type === "binary_expression" || rightNode.conditionNode.type === "instanceof_expression") || isMember(rightNode) && hasDanglingComments(rightNode) || !isMember(rightNode) && hasLeadingComments(rightNode)) return group$7([
296
299
  leftDoc,
297
300
  operator,
298
301
  group$7(indent$7([line$7, rightDoc]))
@@ -463,9 +466,7 @@ function parentNeedsParentheses(path) {
463
466
  const { parent } = path;
464
467
  switch (parent?.type) {
465
468
  case "return_statement":
466
- case "throw_statement":
467
- if (willReturnOrThrowStatementBreak(path)) return false;
468
- break;
469
+ case "throw_statement": if (willReturnOrThrowStatementBreak(path)) return false;
469
470
  }
470
471
  }
471
472
  function willReturnOrThrowStatementBreak(path) {
@@ -491,7 +492,7 @@ function findBaseIndent(lines) {
491
492
  return Math.min(...lines.map((line) => line.search(/\S/)).filter((indent) => indent >= 0));
492
493
  }
493
494
  function findEmbeddedLanguage(path) {
494
- return path.ancestors.find(({ type, comments }) => type === "block" || comments?.some(({ leading }) => leading))?.comments?.filter(({ leading }) => leading).map(({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]).findLast((language) => language)?.toLowerCase();
495
+ return [path.node, ...path.ancestors].find(({ type, comments }) => type === "block" || comments?.some(({ leading }) => leading))?.comments?.filter(({ leading }) => leading).map(({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]).findLast((language) => language)?.toLowerCase();
495
496
  }
496
497
  function escapeDocForTextBlock(doc) {
497
498
  return mapDoc(doc, (currentDoc) => typeof currentDoc === "string" ? currentDoc.replace(/\\|"""/g, (match) => match === "\\" ? "\\\\" : "\"\"\\\"") : currentDoc);
@@ -1453,7 +1454,7 @@ function shouldPrintParamsWithoutParens(path, options) {
1453
1454
  return false;
1454
1455
  }
1455
1456
  function canPrintParamsWithoutParens(node) {
1456
- return node.parametersNode.type === "identifier" && !node.comments?.some(({ leading, trailing }) => !leading && !trailing) && !node.parametersNode.comments;
1457
+ return node.parametersNode.type === "identifier" && !hasDanglingComments(node) && !node.parametersNode.comments;
1457
1458
  }
1458
1459
  function printMemberChain(path, print, options) {
1459
1460
  const isExpressionStatement = path.parent?.type === "expression_statement";
@@ -1948,9 +1949,7 @@ var packages_and_modules_default = {
1948
1949
  annotations.push(print(child));
1949
1950
  break;
1950
1951
  case "identifier":
1951
- case "scoped_identifier":
1952
- identifier.push(print(child));
1953
- break;
1952
+ case "scoped_identifier": identifier.push(print(child));
1954
1953
  }
1955
1954
  }, "namedChildren");
1956
1955
  return join$1(hardline$1, [...annotations, [
@@ -2281,9 +2280,10 @@ function handleLabeledStatementComments(commentNode) {
2281
2280
  function handleLambdaExpressionComments(commentNode) {
2282
2281
  const { enclosingNode, precedingNode, followingNode } = commentNode;
2283
2282
  if (enclosingNode?.type === "lambda_expression" && precedingNode && followingNode && enclosingNode.children.find(({ type }) => type === "->").end.index < commentNode.start.index) {
2284
- if (followingNode.type === "block") if (followingNode.namedChildren.length) util.addLeadingComment(followingNode.namedChildren[0], commentNode);
2285
- else util.addDanglingComment(followingNode, commentNode, void 0);
2286
- else util.addLeadingComment(followingNode, commentNode);
2283
+ if (followingNode.type === "block") {
2284
+ if (followingNode.namedChildren.length) util.addLeadingComment(followingNode.namedChildren[0], commentNode);
2285
+ else util.addDanglingComment(followingNode, commentNode, void 0);
2286
+ } else util.addLeadingComment(followingNode, commentNode);
2287
2287
  return true;
2288
2288
  }
2289
2289
  }
@@ -2366,8 +2366,10 @@ function printLeadingComment(path) {
2366
2366
  const originalText = path.root.value;
2367
2367
  if (comment.type === "block_comment") {
2368
2368
  let lineBreak = " ";
2369
- if (hasNewline(originalText, parser_default.locEnd(comment))) if (hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) lineBreak = hardline;
2370
- else lineBreak = line;
2369
+ if (hasNewline(originalText, parser_default.locEnd(comment))) {
2370
+ if (hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) lineBreak = hardline;
2371
+ else lineBreak = line;
2372
+ }
2371
2373
  parts.push(lineBreak);
2372
2374
  } else parts.push(hardline);
2373
2375
  const index = skipNewline(originalText, skipSpaces(originalText, parser_default.locEnd(comment)));
@@ -2527,8 +2529,10 @@ function processTree(node, fieldName = null, comments) {
2527
2529
  const javaChild = processTree(child, fieldName, comments);
2528
2530
  javaNode.children.push(javaChild);
2529
2531
  if (javaChild.isNamed) javaNode.namedChildren.push(javaChild);
2530
- if (fieldName) if (multiFields?.[fieldName]) javaNode[`${fieldName}Nodes`].push(javaChild);
2531
- else javaNode[`${fieldName}Node`] = javaChild;
2532
+ if (fieldName) {
2533
+ if (multiFields?.[fieldName]) javaNode[`${fieldName}Nodes`].push(javaChild);
2534
+ else javaNode[`${fieldName}Node`] = javaChild;
2535
+ }
2532
2536
  }
2533
2537
  });
2534
2538
  });
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-java-next-line",
3
- "version": "2.10.3",
3
+ "version": "2.10.4",
4
4
  "description": "Prettier Java plugin with configurable same-line and next-line braces",
5
5
  "homepage": "https://github.com/motlin/prettier-java-next-line",
6
6
  "repository": {
@@ -58,7 +58,7 @@
58
58
  ]
59
59
  },
60
60
  "dependencies": {
61
- "web-tree-sitter": "0.26.11"
61
+ "web-tree-sitter": "0.27.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@eslint/js": "^10.0.1",
@@ -73,12 +73,12 @@
73
73
  "lerna": "^10.0.0",
74
74
  "lint-staged": "^17.0.2",
75
75
  "mitata": "^1.0.34",
76
- "mocha": "^11.7.5",
76
+ "mocha": "^12.0.0",
77
77
  "postcss": "^8.5.15",
78
78
  "prettier": "^3.8.1",
79
79
  "prettier-plugin-css-order": "^2.2.0",
80
- "tree-sitter-java-orchard": "0.5.10",
81
- "tsdown": "^0.22.0",
80
+ "tree-sitter-java-orchard": "0.5.18",
81
+ "tsdown": "^0.23.0",
82
82
  "typescript": "^6.0.3",
83
83
  "typescript-eslint": "^8.57.1"
84
84
  },