@prettier/plugin-php 0.20.0 → 0.21.0

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
@@ -97,6 +97,16 @@ npm install --save-dev prettier @prettier/plugin-php
97
97
  npm install --global prettier @prettier/plugin-php
98
98
  ```
99
99
 
100
+ ### Activate the plugin
101
+
102
+ Create or modify your [prettier configuration file](https://prettier.io/docs/en/configuration) to activate the plugin:
103
+
104
+ ```json
105
+ {
106
+ "plugins": ["@prettier/plugin-php"]
107
+ }
108
+ ```
109
+
100
110
  ## Use
101
111
 
102
112
  ### With Node.js
@@ -139,7 +149,7 @@ First, grab both standalone scripts from an npm CDN like [unpkg](https://unpkg.c
139
149
  Then use Prettier with PHP, just like this:
140
150
 
141
151
  ```js
142
- prettier.format(YOUR_CODE, {
152
+ await prettier.format(YOUR_CODE, {
143
153
  plugins: prettierPlugins,
144
154
  parser: "php",
145
155
  });
@@ -153,10 +163,10 @@ Bundlers like webpack, Rollup or browserify automatically recognize how to handl
153
163
 
154
164
  ```js
155
165
  import prettier from "prettier/standalone";
156
- import phpPlugin from "@prettier/plugin-php/standalone";
166
+ import * as prettierPluginPhp from "@prettier/plugin-php/standalone";
157
167
 
158
- prettier.format(YOUR_CODE, {
159
- plugins: [phpPlugin],
168
+ await prettier.format(YOUR_CODE, {
169
+ plugins: [prettierPluginPhp],
160
170
  parser: "php",
161
171
  });
162
172
  ```
@@ -217,9 +227,28 @@ matrix(
217
227
 
218
228
  The official [prettier plugin for atom](https://github.com/prettier/prettier-atom) supports plugins.
219
229
 
220
- ### VScode
230
+ ### Visual Studio Code
231
+
232
+ 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).
233
+
234
+ Visual Studio Code may not recognise the document selectors provided by this plugin, and so you can add php to your document selectors by adding the following line to `.vscode/settings.json`:
235
+
236
+ ```json
237
+ "prettier.documentSelectors": [
238
+ "**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
239
+ ]
240
+ ```
241
+
242
+ You may also need to declare php as the parser in your prettier config file:
243
+
244
+ ```json
245
+ {
246
+ "parser": "php",
247
+ "plugins": ["@prettier/plugin-php"]
248
+ }
249
+ ```
221
250
 
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.
251
+ See (this issue)[https://github.com/prettier/plugin-php/issues/1730] for more discussion around potential VS Code solutions.
223
252
 
224
253
  ### PhpStorm / IntelliJ / Jetbrains IDE
225
254
 
package/package.json CHANGED
@@ -1,57 +1,82 @@
1
1
  {
2
2
  "name": "@prettier/plugin-php",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "Prettier PHP Plugin",
5
5
  "repository": "prettier/prettier-php",
6
6
  "author": "Lucas Azzola <@azz>",
7
7
  "license": "MIT",
8
- "main": "src",
8
+ "type": "module",
9
+ "unpkg": "./standalone.js",
10
+ "browser": "./standalone.js",
11
+ "exports": {
12
+ ".": {
13
+ "browser": "./standalone.js",
14
+ "default": "./src/index.js"
15
+ },
16
+ "./standalone": "./standalone.js",
17
+ "./package.json": "./package.json"
18
+ },
9
19
  "files": [
10
20
  "src",
11
21
  "standalone.js"
12
22
  ],
13
23
  "dependencies": {
14
24
  "linguist-languages": "^7.21.0",
15
- "mem": "^8.0.0",
25
+ "mem": "^9.0.2",
16
26
  "php-parser": "^3.1.5"
17
27
  },
18
28
  "devDependencies": {
19
- "@babel/preset-env": "^7.22.10",
20
- "codecov": "3.8.3",
29
+ "@babel/preset-env": "^7.22.14",
30
+ "@rollup/plugin-alias": "^5.0.0",
31
+ "@rollup/plugin-babel": "^6.0.3",
32
+ "@rollup/plugin-commonjs": "^25.0.4",
33
+ "@rollup/plugin-inject": "^5.0.3",
34
+ "@rollup/plugin-json": "^6.0.0",
35
+ "@rollup/plugin-node-resolve": "^15.2.1",
36
+ "@rollup/plugin-replace": "^5.0.2",
37
+ "@rollup/plugin-terser": "^0.4.3",
38
+ "c8": "^8.0.1",
21
39
  "cross-env": "^7.0.2",
22
- "eslint": "8.47.0",
40
+ "eslint": "8.51.0",
23
41
  "eslint-config-prettier": "9.0.0",
24
- "eslint-plugin-import": "2.28.0",
25
- "eslint-plugin-jest": "27.2.3",
26
- "eslint-plugin-prettier": "5.0.0",
42
+ "eslint-plugin-import": "2.28.1",
43
+ "eslint-plugin-jest": "27.4.2",
44
+ "eslint-plugin-prettier": "5.0.1",
27
45
  "eslint-plugin-prettier-doc": "^1.1.0",
28
- "jest": "29.6.2",
29
- "jest-environment-jsdom": "29.6.2",
30
- "jest-light-runner": "^0.5.0",
31
- "jest-runner-eslint": "2.1.0",
32
- "jest-snapshot-serializer-raw": "^1.1.0",
46
+ "jest": "29.7.0",
47
+ "jest-environment-jsdom": "29.7.0",
48
+ "jest-light-runner": "^0.5.1",
49
+ "jest-snapshot-serializer-raw": "^2.0.0",
33
50
  "prettier": "^3.0.1",
34
- "rollup": "^2.75.7",
35
- "rollup-plugin-alias": "^2.0.0",
36
- "rollup-plugin-babel": "^4.3.2",
37
- "rollup-plugin-commonjs": "^10.0.2",
38
- "rollup-plugin-inject": "^3.0.1",
39
- "rollup-plugin-json": "^4.0.0",
40
- "rollup-plugin-node-resolve": "^5.2.0",
41
- "rollup-plugin-replace": "^2.1.0",
42
- "rollup-plugin-terser": "^7.0.2",
43
- "strip-ansi": "^6.0.0"
51
+ "rollup": "^3.28.1",
52
+ "strip-ansi": "^7.1.0"
44
53
  },
45
54
  "peerDependencies": {
46
55
  "prettier": "^3.0.0"
47
56
  },
48
57
  "scripts": {
58
+ "lint": "yarn lint:eslint && yarn lint:prettier",
59
+ "lint:eslint": "eslint .",
60
+ "lint:prettier": "prettier . --check",
61
+ "fix": "yarn fix:eslint && yarn fix:prettier",
62
+ "fix:eslint": "eslint . --fix",
63
+ "fix:prettier": "prettier . --write",
49
64
  "test": "yarn test:node && yarn test:standalone",
50
65
  "test:node": "jest",
51
- "test:standalone": "yarn run build-standalone && cross-env RUN_STANDALONE_TESTS=true yarn jest",
66
+ "test:standalone": "yarn run build && cross-env RUN_STANDALONE_TESTS=true yarn jest",
52
67
  "prepublishOnly": "yarn test",
53
68
  "prettier": "prettier --plugin=src/index.js --parser=php",
54
- "build-standalone": "rollup -c build/rollup.config.js",
69
+ "build": "rollup --config build/rollup.config.js",
55
70
  "debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
71
+ },
72
+ "c8": {
73
+ "reporter": [
74
+ "lcov",
75
+ "text"
76
+ ],
77
+ "all": true,
78
+ "include": [
79
+ "src/**"
80
+ ]
56
81
  }
57
82
  }
package/src/clean.js CHANGED
@@ -1,6 +1,4 @@
1
- "use strict";
2
-
3
- const util = require("./util");
1
+ import { printNumber, normalizeMagicMethodName } from "./util.js";
4
2
 
5
3
  const ignoredProperties = new Set([
6
4
  "loc",
@@ -28,7 +26,7 @@ const ignoredProperties = new Set([
28
26
  function clean(node, newObj) {
29
27
  if (node.kind === "string") {
30
28
  // TODO if options are available in this method, replace with
31
- // newObj.isDoubleQuote = !util.useSingleQuote(node, options);
29
+ // newObj.isDoubleQuote = !useSingleQuote(node, options);
32
30
  delete newObj.isDoubleQuote;
33
31
  }
34
32
 
@@ -66,7 +64,7 @@ function clean(node, newObj) {
66
64
 
67
65
  // Normalize numbers
68
66
  if (node.kind === "number") {
69
- newObj.value = util.printNumber(node.value);
67
+ newObj.value = printNumber(node.value);
70
68
  }
71
69
 
72
70
  const statements = ["foreach", "for", "if", "while", "do"];
@@ -100,7 +98,7 @@ function clean(node, newObj) {
100
98
  }
101
99
 
102
100
  if (node.kind === "method" && node.name.kind === "identifier") {
103
- newObj.name.name = util.normalizeMagicMethodName(newObj.name.name);
101
+ newObj.name.name = normalizeMagicMethodName(newObj.name.name);
104
102
  }
105
103
 
106
104
  if (node.kind === "noop") {
@@ -110,4 +108,4 @@ function clean(node, newObj) {
110
108
 
111
109
  clean.ignoredProperties = ignoredProperties;
112
110
 
113
- module.exports = clean;
111
+ export default clean;
package/src/comments.js CHANGED
@@ -1,4 +1,6 @@
1
- "use strict";
1
+ import { util as prettierUtil, doc } from "prettier";
2
+ import { isLookupNode } from "./util.js";
3
+ import { locStart, locEnd } from "./loc.js";
2
4
 
3
5
  const {
4
6
  addLeadingComment,
@@ -7,15 +9,12 @@ const {
7
9
  skipNewline,
8
10
  hasNewline,
9
11
  hasNewlineInRange,
10
- } = require("prettier").util;
11
- const { join, indent, hardline, cursor, lineSuffix, breakParent } =
12
- require("prettier").doc.builders;
13
- const {
14
12
  getNextNonSpaceNonCommentCharacterIndex,
15
13
  isNextLineEmpty,
16
14
  isPreviousLineEmpty,
17
- isLookupNode,
18
- } = require("./util");
15
+ } = prettierUtil;
16
+ const { join, indent, hardline, cursor, lineSuffix, breakParent } =
17
+ doc.builders;
19
18
 
20
19
  /*
21
20
  Comment functions are meant to inspect various edge cases using given comment nodes,
@@ -274,13 +273,12 @@ function handleLastFunctionArgComments(
274
273
  precedingNode,
275
274
  enclosingNode,
276
275
  followingNode,
277
- comment,
278
- options
276
+ comment
277
+ /* options */
279
278
  ) {
280
279
  const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
281
280
  text,
282
- comment,
283
- options
281
+ locEnd(comment)
284
282
  );
285
283
  const nextCharacter = text.charAt(nextCharIndex);
286
284
 
@@ -329,8 +327,8 @@ function handleIfStatementComments(
329
327
  precedingNode,
330
328
  enclosingNode,
331
329
  followingNode,
332
- comment,
333
- options
330
+ comment
331
+ /* options */
334
332
  ) {
335
333
  if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) {
336
334
  return false;
@@ -338,8 +336,7 @@ function handleIfStatementComments(
338
336
 
339
337
  const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
340
338
  text,
341
- comment,
342
- options
339
+ locEnd(comment)
343
340
  );
344
341
  const nextCharacter = text.charAt(nextCharIndex);
345
342
 
@@ -380,16 +377,12 @@ function handleRetifComments(
380
377
  precedingNode,
381
378
  followingNode,
382
379
  comment,
383
- text,
384
- options
380
+ text
381
+ /* options */
385
382
  ) {
386
383
  const isSameLineAsPrecedingNode =
387
384
  precedingNode &&
388
- !hasNewlineInRange(
389
- text,
390
- options.locEnd(precedingNode),
391
- options.locStart(comment)
392
- );
385
+ !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
393
386
 
394
387
  if (
395
388
  (!precedingNode || !isSameLineAsPrecedingNode) &&
@@ -507,7 +500,13 @@ function handleClassComments(enclosingNode, followingNode, comment) {
507
500
  return false;
508
501
  }
509
502
 
510
- function handleFunction(text, enclosingNode, followingNode, comment, options) {
503
+ function handleFunction(
504
+ text,
505
+ enclosingNode,
506
+ followingNode,
507
+ comment
508
+ /* options */
509
+ ) {
511
510
  if (
512
511
  enclosingNode &&
513
512
  (enclosingNode.kind === "function" || enclosingNode.kind === "method")
@@ -518,18 +517,17 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
518
517
  let argumentsLocEnd = 0;
519
518
  for (let i = 0; i < enclosingNode.arguments.length; i++) {
520
519
  argumentsLocEnd =
521
- options.locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
522
- ? options.locEnd(enclosingNode.arguments[i])
520
+ locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
521
+ ? locEnd(enclosingNode.arguments[i])
523
522
  : argumentsLocEnd;
524
523
  }
525
524
  const commentIsBetweenArgumentsAndBody =
526
525
  enclosingNode.body &&
527
- options.locStart(comment) > argumentsLocEnd &&
528
- options.locEnd(comment) < options.locStart(enclosingNode.body);
526
+ locStart(comment) > argumentsLocEnd &&
527
+ locEnd(comment) < locStart(enclosingNode.body);
529
528
  const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
530
529
  text,
531
- comment,
532
- options
530
+ locEnd(comment)
533
531
  );
534
532
  // we additionally need to check if this isn't a trailing argument comment,
535
533
  // by checking the next character isn't ")"
@@ -538,7 +536,7 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
538
536
  commentIsBetweenArgumentsAndBody &&
539
537
  text.charAt(nextCharIndex) !== ")"
540
538
  ) {
541
- if (options.locEnd(comment) < options.locStart(enclosingNode.type)) {
539
+ if (locEnd(comment) < locStart(enclosingNode.type)) {
542
540
  // we need to store this as a dangling comment in case the type is nullable
543
541
  // ie function(): ?string {} - the "nullable" attribute is part of the
544
542
  // function node, not the type.
@@ -609,11 +607,15 @@ function handleHalt(precedingNode, enclosingNode, followingNode, comment) {
609
607
  return false;
610
608
  }
611
609
 
612
- function handleCommentInEmptyParens(text, enclosingNode, comment, options) {
610
+ function handleCommentInEmptyParens(
611
+ text,
612
+ enclosingNode,
613
+ comment
614
+ /* options */
615
+ ) {
613
616
  const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
614
617
  text,
615
- comment,
616
- options
618
+ locEnd(comment)
617
619
  );
618
620
 
619
621
  if (text.charAt(nextCharIndex) !== ")") {
@@ -790,8 +792,8 @@ function handleWhileComments(
790
792
  precedingNode,
791
793
  enclosingNode,
792
794
  followingNode,
793
- comment,
794
- options
795
+ comment
796
+ /* options */
795
797
  ) {
796
798
  if (!enclosingNode || enclosingNode.kind !== "while" || !followingNode) {
797
799
  return false;
@@ -804,8 +806,7 @@ function handleWhileComments(
804
806
 
805
807
  const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
806
808
  text,
807
- comment,
808
- options
809
+ locEnd(comment)
809
810
  );
810
811
  const nextCharacter = text.charAt(nextCharIndex);
811
812
 
@@ -822,11 +823,11 @@ function handleWhileComments(
822
823
  return false;
823
824
  }
824
825
 
825
- // https://github.com/prettier/prettier/blob/master/src/main/comments.js#L335
826
- function printComment(commentPath, options) {
827
- const comment = commentPath.getValue();
826
+ // https://github.com/prettier/prettier/blob/c01661f311a2e1e033f1f9cb127882cc13e293bd/src/main/comments/print.js#L23
827
+ function printComment(path, options) {
828
+ const comment = path.node;
828
829
  comment.printed = true;
829
- return options.printer.printComment(commentPath, options);
830
+ return options.printer.printComment(path, options);
830
831
  }
831
832
 
832
833
  // https://github.com/prettier/prettier/blob/master/src/main/comments.js#L440
@@ -838,15 +839,15 @@ function printDanglingComments(path, options, sameIndent, filter) {
838
839
  return "";
839
840
  }
840
841
 
841
- path.each((commentPath) => {
842
- const comment = commentPath.getValue();
842
+ path.each(() => {
843
+ const comment = path.node;
843
844
  if (
844
845
  comment &&
845
846
  !comment.leading &&
846
847
  !comment.trailing &&
847
848
  (!filter || filter(comment))
848
849
  ) {
849
- parts.push(printComment(commentPath, options));
850
+ parts.push(printComment(path, options));
850
851
  }
851
852
  }, "comments");
852
853
 
@@ -868,11 +869,11 @@ function hasTrailingComment(node) {
868
869
  return node.comments && node.comments.some((comment) => comment.trailing);
869
870
  }
870
871
 
871
- function hasLeadingOwnLineComment(text, node, options) {
872
+ function hasLeadingOwnLineComment(text, node) {
872
873
  return (
873
874
  node.comments &&
874
875
  node.comments.some(
875
- (comment) => comment.leading && hasNewline(text, options.locEnd(comment))
876
+ (comment) => comment.leading && hasNewline(text, locEnd(comment))
876
877
  )
877
878
  );
878
879
  }
@@ -887,7 +888,7 @@ function printComments(comments, options) {
887
888
  parts.push(hardline);
888
889
  }
889
890
  if (
890
- isNextLineEmpty(options.originalText, comment, options) &&
891
+ isNextLineEmpty(options.originalText, locEnd(comment)) &&
891
892
  !isLastComment
892
893
  ) {
893
894
  parts.push(hardline);
@@ -917,21 +918,22 @@ function canAttachComment(node) {
917
918
  // Based on https://github.com/prettier/prettier/blob/master/src/main/comments.js
918
919
  // TODO remove after https://github.com/prettier/prettier/issues/5087
919
920
  function prependCursorPlaceholder(path, options, printed) {
920
- if (path.getNode() === options.cursorNode && path.getValue()) {
921
+ const { node } = path;
922
+ if (node && node === options.cursorNode) {
921
923
  return [cursor, printed, cursor];
922
924
  }
923
925
 
924
926
  return printed;
925
927
  }
926
928
 
927
- function printLeadingComment(commentPath, print, options) {
928
- const comment = commentPath.getValue();
929
- const contents = printComment(commentPath, options);
929
+ function printLeadingComment(path, print, options) {
930
+ const contents = printComment(path, options);
930
931
 
931
932
  if (!contents) {
932
933
  return "";
933
934
  }
934
935
 
936
+ const comment = path.node;
935
937
  const isBlock =
936
938
  options.printer.isBlockComment && options.printer.isBlockComment(comment);
937
939
 
@@ -940,26 +942,25 @@ function printLeadingComment(commentPath, print, options) {
940
942
  if (isBlock) {
941
943
  return [
942
944
  contents,
943
- hasNewline(options.originalText, options.locEnd(comment))
944
- ? hardline
945
- : " ",
945
+ hasNewline(options.originalText, locEnd(comment)) ? hardline : " ",
946
946
  ];
947
947
  }
948
948
 
949
949
  return [contents, hardline];
950
950
  }
951
951
 
952
- function printTrailingComment(commentPath, print, options) {
953
- const comment = commentPath.getValue();
954
- const contents = printComment(commentPath, options);
952
+ function printTrailingComment(path, print, options) {
953
+ const contents = printComment(path, options);
955
954
  if (!contents) {
956
955
  return "";
957
956
  }
957
+
958
+ const comment = path.node;
958
959
  const isBlock =
959
960
  options.printer.isBlockComment && options.printer.isBlockComment(comment);
960
961
 
961
962
  if (
962
- hasNewline(options.originalText, options.locStart(comment), {
963
+ hasNewline(options.originalText, locStart(comment), {
963
964
  backwards: true,
964
965
  })
965
966
  ) {
@@ -977,8 +978,7 @@ function printTrailingComment(commentPath, print, options) {
977
978
 
978
979
  const isLineBeforeEmpty = isPreviousLineEmpty(
979
980
  options.originalText,
980
- comment,
981
- options
981
+ locStart(comment)
982
982
  );
983
983
 
984
984
  return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]);
@@ -991,9 +991,9 @@ function printTrailingComment(commentPath, print, options) {
991
991
  }
992
992
 
993
993
  function printAllComments(path, print, options, needsSemi) {
994
- const value = path.getValue();
994
+ const { node } = path;
995
995
  const printed = print(path);
996
- const comments = value && value.comments;
996
+ const comments = node && node.comments;
997
997
 
998
998
  if (!comments || comments.length === 0) {
999
999
  return prependCursorPlaceholder(path, options, printed);
@@ -1002,23 +1002,22 @@ function printAllComments(path, print, options, needsSemi) {
1002
1002
  const leadingParts = [];
1003
1003
  const trailingParts = [needsSemi ? ";" : "", printed];
1004
1004
 
1005
- path.each((commentPath) => {
1006
- const comment = commentPath.getValue();
1005
+ path.each(({ node: comment }) => {
1007
1006
  const { leading, trailing } = comment;
1008
1007
 
1009
1008
  if (leading) {
1010
- const contents = printLeadingComment(commentPath, print, options);
1009
+ const contents = printLeadingComment(path, print, options);
1011
1010
  if (!contents) {
1012
1011
  return;
1013
1012
  }
1014
1013
  leadingParts.push(contents);
1015
1014
 
1016
1015
  const text = options.originalText;
1017
- if (hasNewline(text, skipNewline(text, options.locEnd(comment)))) {
1016
+ if (hasNewline(text, skipNewline(text, locEnd(comment)))) {
1018
1017
  leadingParts.push(hardline);
1019
1018
  }
1020
1019
  } else if (trailing) {
1021
- trailingParts.push(printTrailingComment(commentPath, print, options));
1020
+ trailingParts.push(printTrailingComment(path, print, options));
1022
1021
  }
1023
1022
  }, "comments");
1024
1023
 
@@ -1029,7 +1028,7 @@ function printAllComments(path, print, options, needsSemi) {
1029
1028
  );
1030
1029
  }
1031
1030
 
1032
- module.exports = {
1031
+ export {
1033
1032
  handleOwnLineComment,
1034
1033
  handleEndOfLineComment,
1035
1034
  handleRemainingComment,