@prettier/plugin-php 0.17.2 → 0.17.6

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
@@ -221,6 +221,20 @@ The official [prettier plugin for atom](https://github.com/prettier/prettier-ato
221
221
 
222
222
  The official [prettier plugin for vscode](https://github.com/prettier/prettier-vscode) supports plugins since Version 1.10.0. To enable it, install the extension and make sure the plugin is installed _locally_ (in your project folder). After restarting VScode the plugin should work as expected.
223
223
 
224
+ ### PhpStorm / IntelliJ / Jetbrains IDE
225
+
226
+ - Install prettier and plugin locally `yarn add -D prettier @prettier/plugin-php`
227
+ - Open Settings (File, Settings)
228
+ - Go to Plugins Section, Select Marketplace, Search for Prettier, Install Plugin, Restart IDE
229
+ - Open Settings, Search for Prettier, select Prettier in left settings navigation
230
+ - Check prettier package has auto-detected, should be something like `myproject/node_modules/prettier`
231
+ - Update Run for Files to include .php, eg: `{**/*,*}.{js,ts,jsx,tsx,php,json,scss,vue,md}`
232
+ - Tick the On Save button, if you want your files formatting updated on file save
233
+ - Clock OK to save settings
234
+
235
+ _Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
236
+ alternatively you can use the Prettier shortcut Ctrl+Alt+Shift+P_
237
+
224
238
  ### Sublime Text
225
239
 
226
240
  Sublime Text support is available through Package Control and the [JsPrettier](https://packagecontrol.io/packages/JsPrettier) plugin.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prettier/plugin-php",
3
- "version": "0.17.2",
3
+ "version": "0.17.6",
4
4
  "description": "Prettier PHP Plugin",
5
5
  "repository": "prettier/prettier-php",
6
6
  "author": "Lucas Azzola <@azz>",
@@ -13,21 +13,21 @@
13
13
  "dependencies": {
14
14
  "linguist-languages": "^7.5.1",
15
15
  "mem": "^8.0.0",
16
- "php-parser": "https://github.com/glayzzle/php-parser#cca83041d938b972c7ef74648f29de03777b0ea4"
16
+ "php-parser": "https://github.com/glayzzle/php-parser#27abcb2337ac6450c068ef064982dfabf77916a5"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@babel/preset-env": "^7.7.5",
20
- "codecov": "3.8.2",
20
+ "codecov": "3.8.3",
21
21
  "cross-env": "^7.0.2",
22
- "eslint": "7.29.0",
22
+ "eslint": "7.32.0",
23
23
  "eslint-config-prettier": "8.3.0",
24
- "eslint-plugin-import": "2.23.4",
25
- "eslint-plugin-jest": "24.3.6",
26
- "eslint-plugin-prettier": "3.4.0",
27
- "jest": "26.6.3",
28
- "jest-runner-eslint": "0.10.1",
24
+ "eslint-plugin-import": "2.25.2",
25
+ "eslint-plugin-jest": "24.4.0",
26
+ "eslint-plugin-prettier": "4.0.0",
27
+ "jest": "27.3.1",
28
+ "jest-runner-eslint": "1.0.0",
29
29
  "jest-snapshot-serializer-raw": "^1.1.0",
30
- "prettier": "^2.0.0",
30
+ "prettier": "2.1.2",
31
31
  "rollup": "^2.7.5",
32
32
  "rollup-plugin-alias": "^2.0.0",
33
33
  "rollup-plugin-babel": "^4.3.2",
package/src/index.js CHANGED
@@ -115,16 +115,29 @@ const printers = {
115
115
  }
116
116
  },
117
117
  hasPrettierIgnore(path) {
118
- const node = path.getNode();
119
118
  const isSimpleIgnore = (comment) =>
120
119
  comment.value.includes("prettier-ignore") &&
121
120
  !comment.value.includes("prettier-ignore-start") &&
122
121
  !comment.value.includes("prettier-ignore-end");
122
+
123
+ const parentNode = path.getParentNode();
124
+ const node = path.getNode();
125
+
123
126
  return (
124
- node &&
125
- node.comments &&
126
- node.comments.length > 0 &&
127
- node.comments.some(isSimpleIgnore)
127
+ (node &&
128
+ node.kind !== "classconstant" &&
129
+ node.comments &&
130
+ node.comments.length > 0 &&
131
+ node.comments.some(isSimpleIgnore)) ||
132
+ // For proper formatting, the classconstant ignore formatting should
133
+ // run on the "constant" child
134
+ (node &&
135
+ node.kind === "constant" &&
136
+ parentNode &&
137
+ parentNode.kind === "classconstant" &&
138
+ parentNode.comments &&
139
+ parentNode.comments.length > 0 &&
140
+ parentNode.comments.some(isSimpleIgnore))
128
141
  );
129
142
  },
130
143
  },
package/src/printer.js CHANGED
@@ -1049,14 +1049,20 @@ function printLines(path, options, print, childrenAttribute = "children") {
1049
1049
  : isProgramLikeNode(node) && isFirstNode && node.kind !== "namespace"
1050
1050
  ? ""
1051
1051
  : concat([beforeCloseTagInlineNode, "?>"]);
1052
+
1053
+ const nextV = path.getNode(index + 1);
1054
+ const skipLastComment = nextV && nextV.children && nextV.children.length;
1055
+
1052
1056
  const afterInline =
1053
1057
  childNode.comments && childNode.comments.length
1054
1058
  ? concat([
1055
1059
  openTag,
1056
1060
  hardline,
1057
- comments.printComments(childNode.comments, options),
1061
+ skipLastComment
1062
+ ? comments.printComments(childNode.comments, options)
1063
+ : "",
1058
1064
  hardline,
1059
- "?>",
1065
+ skipLastComment ? "?>" : "",
1060
1066
  ])
1061
1067
  : isProgramLikeNode(node) && isLastNode
1062
1068
  ? ""
@@ -1102,7 +1108,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
1102
1108
  const beforeCloseTag = lastNode
1103
1109
  ? concat([
1104
1110
  hasNewlineInRange(
1105
- options.originalText,
1111
+ options.originalText.trimEnd(),
1106
1112
  options.locEnd(lastNode),
1107
1113
  options.locEnd(node)
1108
1114
  )
@@ -1199,10 +1205,51 @@ function printClassPart(
1199
1205
  );
1200
1206
  }
1201
1207
 
1208
+ function printAttrs(path, options, print, { inline = false } = {}) {
1209
+ const allAttrs = [];
1210
+ if (!path.getValue().attrGroups) {
1211
+ return [];
1212
+ }
1213
+ path.each((agPath) => {
1214
+ const attrGroup = ["#["];
1215
+ if (allAttrs.length > 0) {
1216
+ allAttrs.push(inline ? " " : hardline);
1217
+ }
1218
+ attrGroup.push(ifBreak(softline));
1219
+ agPath.each((attrPath) => {
1220
+ const attrNode = attrPath.getValue();
1221
+ if (attrGroup.length > 2) {
1222
+ attrGroup.push(",", ifBreak(softline, " "));
1223
+ }
1224
+ const attrStmt = [attrNode.name];
1225
+ if (attrNode.args.length > 0) {
1226
+ attrStmt.push(printArgumentsList(attrPath, options, print, "args"));
1227
+ }
1228
+ attrGroup.push(group(concat(attrStmt)));
1229
+ }, "attrs");
1230
+ allAttrs.push(
1231
+ group(
1232
+ concat([
1233
+ indent(concat(attrGroup)),
1234
+ ifBreak(softline),
1235
+ "]",
1236
+ softline,
1237
+ inline ? " " : "",
1238
+ ])
1239
+ )
1240
+ );
1241
+ }, "attrGroups");
1242
+ if (allAttrs.length === 0) {
1243
+ return [];
1244
+ }
1245
+ return [concat([...allAttrs, inline ? "" : hardline])];
1246
+ }
1247
+
1202
1248
  function printClass(path, options, print) {
1203
1249
  const node = path.getValue();
1204
-
1205
- const declaration = [];
1250
+ const isAnonymousClass = node.kind === "class" && node.isAnonymous;
1251
+ const attrs = printAttrs(path, options, print, { inline: isAnonymousClass });
1252
+ const declaration = isAnonymousClass ? [] : [...attrs];
1206
1253
 
1207
1254
  if (node.isFinal) {
1208
1255
  declaration.push("final ");
@@ -1212,8 +1259,6 @@ function printClass(path, options, print) {
1212
1259
  declaration.push("abstract ");
1213
1260
  }
1214
1261
 
1215
- const isAnonymousClass = node.kind === "class" && node.isAnonymous;
1216
-
1217
1262
  // `new` print `class` keyword with arguments
1218
1263
  declaration.push(isAnonymousClass ? "" : node.kind);
1219
1264
 
@@ -1318,7 +1363,9 @@ function printClass(path, options, print) {
1318
1363
 
1319
1364
  function printFunction(path, options, print) {
1320
1365
  const node = path.getValue();
1321
- const declaration = [];
1366
+ const declaration = [
1367
+ ...printAttrs(path, options, print, { inline: node.kind === "closure" }),
1368
+ ];
1322
1369
 
1323
1370
  if (node.isFinal) {
1324
1371
  declaration.push("final ");
@@ -1745,6 +1792,7 @@ function printNode(path, options, print) {
1745
1792
  return printFunction(path, options, print);
1746
1793
  case "arrowfunc":
1747
1794
  return concat([
1795
+ ...printAttrs(path, options, print, { inline: true }),
1748
1796
  node.isStatic ? "static " : "",
1749
1797
  "fn",
1750
1798
  printArgumentsList(path, options, print),
@@ -1764,6 +1812,7 @@ function printNode(path, options, print) {
1764
1812
  promoted = "private ";
1765
1813
  }
1766
1814
  const name = concat([
1815
+ ...printAttrs(path, options, print, { inline: true }),
1767
1816
  promoted,
1768
1817
  node.nullable ? "?" : "",
1769
1818
  node.type ? concat([path.call(print, "type"), " "]) : "",
@@ -1823,6 +1872,11 @@ function printNode(path, options, print) {
1823
1872
  ])
1824
1873
  );
1825
1874
  case "propertystatement": {
1875
+ const attrs = [];
1876
+ path.map(
1877
+ (propPath) => attrs.push(...printAttrs(propPath, options, print)),
1878
+ "properties"
1879
+ );
1826
1880
  const printed = path.map((childPath) => {
1827
1881
  return print(childPath);
1828
1882
  }, "properties");
@@ -1842,6 +1896,7 @@ function printNode(path, options, print) {
1842
1896
 
1843
1897
  return group(
1844
1898
  concat([
1899
+ ...attrs,
1845
1900
  hasVisibility
1846
1901
  ? concat([node.visibility === null ? "var" : node.visibility, ""])
1847
1902
  : "",
@@ -2148,6 +2203,7 @@ function printNode(path, options, print) {
2148
2203
  ) {
2149
2204
  return concat([
2150
2205
  "new ",
2206
+ ...path.call(printAttrs, "what"),
2151
2207
  path.call(print, "what"),
2152
2208
  "(",
2153
2209
  join(", ", path.map(print, "arguments")),
@@ -2168,6 +2224,10 @@ function printNode(path, options, print) {
2168
2224
  " ",
2169
2225
  ])
2170
2226
  : "",
2227
+ ...path.call(
2228
+ (pa, opt, pr) => printAttrs(pa, opt, pr, { inline: true }),
2229
+ "what"
2230
+ ),
2171
2231
  "class",
2172
2232
  node.arguments.length > 0
2173
2233
  ? concat([" ", printArgumentsList(path, options, print)])
@@ -2406,9 +2466,8 @@ function printNode(path, options, print) {
2406
2466
  }
2407
2467
  case "constantstatement":
2408
2468
  case "classconstant": {
2409
- const printed = path.map((childPath) => {
2410
- return print(childPath);
2411
- }, "constants");
2469
+ const attrs = printAttrs(path, options, print);
2470
+ const printed = path.map((childPath) => print(childPath), "constants");
2412
2471
 
2413
2472
  let firstVariable;
2414
2473
 
@@ -2421,6 +2480,7 @@ function printNode(path, options, print) {
2421
2480
 
2422
2481
  return group(
2423
2482
  concat([
2483
+ ...attrs,
2424
2484
  node.visibility ? concat([node.visibility, " "]) : "",
2425
2485
  "const",
2426
2486
  firstVariable ? concat([" ", firstVariable]) : "",