@prettier/plugin-php 0.20.0 → 0.20.1

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
@@ -139,7 +139,7 @@ First, grab both standalone scripts from an npm CDN like [unpkg](https://unpkg.c
139
139
  Then use Prettier with PHP, just like this:
140
140
 
141
141
  ```js
142
- prettier.format(YOUR_CODE, {
142
+ await prettier.format(YOUR_CODE, {
143
143
  plugins: prettierPlugins,
144
144
  parser: "php",
145
145
  });
@@ -153,10 +153,10 @@ Bundlers like webpack, Rollup or browserify automatically recognize how to handl
153
153
 
154
154
  ```js
155
155
  import prettier from "prettier/standalone";
156
- import phpPlugin from "@prettier/plugin-php/standalone";
156
+ import * as prettierPluginPhp from "@prettier/plugin-php/standalone";
157
157
 
158
- prettier.format(YOUR_CODE, {
159
- plugins: [phpPlugin],
158
+ await prettier.format(YOUR_CODE, {
159
+ plugins: [prettierPluginPhp],
160
160
  parser: "php",
161
161
  });
162
162
  ```
@@ -217,9 +217,28 @@ matrix(
217
217
 
218
218
  The official [prettier plugin for atom](https://github.com/prettier/prettier-atom) supports plugins.
219
219
 
220
- ### VScode
220
+ ### Visual Studio Code
221
221
 
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.
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).
223
+
224
+ 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`:
225
+
226
+ ```json
227
+ "prettier.documentSelectors": [
228
+ "**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
229
+ ]
230
+ ```
231
+
232
+ You may also need to declare php as the parser in your prettier config file:
233
+
234
+ ```json
235
+ {
236
+ "parser": "php",
237
+ "plugins": ["@prettier/plugin-php"],
238
+ }
239
+ ```
240
+
241
+ See (this issue)[https://github.com/prettier/plugin-php/issues/1730] for more discussion around potential VS Code solutions.
223
242
 
224
243
  ### PhpStorm / IntelliJ / Jetbrains IDE
225
244
 
package/package.json CHANGED
@@ -1,22 +1,34 @@
1
1
  {
2
2
  "name": "@prettier/plugin-php",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
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
29
  "@babel/preset-env": "^7.22.10",
30
+ "@rollup/plugin-commonjs": "^25.0.4",
31
+ "@rollup/plugin-node-resolve": "^15.1.0",
20
32
  "codecov": "3.8.3",
21
33
  "cross-env": "^7.0.2",
22
34
  "eslint": "8.47.0",
@@ -34,13 +46,11 @@
34
46
  "rollup": "^2.75.7",
35
47
  "rollup-plugin-alias": "^2.0.0",
36
48
  "rollup-plugin-babel": "^4.3.2",
37
- "rollup-plugin-commonjs": "^10.0.2",
38
49
  "rollup-plugin-inject": "^3.0.1",
39
50
  "rollup-plugin-json": "^4.0.0",
40
- "rollup-plugin-node-resolve": "^5.2.0",
41
51
  "rollup-plugin-replace": "^2.1.0",
42
52
  "rollup-plugin-terser": "^7.0.2",
43
- "strip-ansi": "^6.0.0"
53
+ "strip-ansi": "^7.1.0"
44
54
  },
45
55
  "peerDependencies": {
46
56
  "prettier": "^3.0.0"
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,10 @@
1
- "use strict";
1
+ import { util as prettierUtil, doc } from "prettier";
2
+ import {
3
+ getNextNonSpaceNonCommentCharacterIndex,
4
+ isNextLineEmpty,
5
+ isPreviousLineEmpty,
6
+ isLookupNode,
7
+ } from "./util.js";
2
8
 
3
9
  const {
4
10
  addLeadingComment,
@@ -7,15 +13,9 @@ const {
7
13
  skipNewline,
8
14
  hasNewline,
9
15
  hasNewlineInRange,
10
- } = require("prettier").util;
16
+ } = prettierUtil;
11
17
  const { join, indent, hardline, cursor, lineSuffix, breakParent } =
12
- require("prettier").doc.builders;
13
- const {
14
- getNextNonSpaceNonCommentCharacterIndex,
15
- isNextLineEmpty,
16
- isPreviousLineEmpty,
17
- isLookupNode,
18
- } = require("./util");
18
+ doc.builders;
19
19
 
20
20
  /*
21
21
  Comment functions are meant to inspect various edge cases using given comment nodes,
@@ -822,11 +822,11 @@ function handleWhileComments(
822
822
  return false;
823
823
  }
824
824
 
825
- // https://github.com/prettier/prettier/blob/master/src/main/comments.js#L335
826
- function printComment(commentPath, options) {
827
- const comment = commentPath.getValue();
825
+ // https://github.com/prettier/prettier/blob/c01661f311a2e1e033f1f9cb127882cc13e293bd/src/main/comments/print.js#L23
826
+ function printComment(path, options) {
827
+ const comment = path.node;
828
828
  comment.printed = true;
829
- return options.printer.printComment(commentPath, options);
829
+ return options.printer.printComment(path, options);
830
830
  }
831
831
 
832
832
  // https://github.com/prettier/prettier/blob/master/src/main/comments.js#L440
@@ -838,15 +838,15 @@ function printDanglingComments(path, options, sameIndent, filter) {
838
838
  return "";
839
839
  }
840
840
 
841
- path.each((commentPath) => {
842
- const comment = commentPath.getValue();
841
+ path.each(() => {
842
+ const comment = path.node;
843
843
  if (
844
844
  comment &&
845
845
  !comment.leading &&
846
846
  !comment.trailing &&
847
847
  (!filter || filter(comment))
848
848
  ) {
849
- parts.push(printComment(commentPath, options));
849
+ parts.push(printComment(path, options));
850
850
  }
851
851
  }, "comments");
852
852
 
@@ -917,21 +917,22 @@ function canAttachComment(node) {
917
917
  // Based on https://github.com/prettier/prettier/blob/master/src/main/comments.js
918
918
  // TODO remove after https://github.com/prettier/prettier/issues/5087
919
919
  function prependCursorPlaceholder(path, options, printed) {
920
- if (path.getNode() === options.cursorNode && path.getValue()) {
920
+ const { node } = path;
921
+ if (node && node === options.cursorNode) {
921
922
  return [cursor, printed, cursor];
922
923
  }
923
924
 
924
925
  return printed;
925
926
  }
926
927
 
927
- function printLeadingComment(commentPath, print, options) {
928
- const comment = commentPath.getValue();
929
- const contents = printComment(commentPath, options);
928
+ function printLeadingComment(path, print, options) {
929
+ const contents = printComment(path, options);
930
930
 
931
931
  if (!contents) {
932
932
  return "";
933
933
  }
934
934
 
935
+ const comment = path.node;
935
936
  const isBlock =
936
937
  options.printer.isBlockComment && options.printer.isBlockComment(comment);
937
938
 
@@ -949,12 +950,13 @@ function printLeadingComment(commentPath, print, options) {
949
950
  return [contents, hardline];
950
951
  }
951
952
 
952
- function printTrailingComment(commentPath, print, options) {
953
- const comment = commentPath.getValue();
954
- const contents = printComment(commentPath, options);
953
+ function printTrailingComment(path, print, options) {
954
+ const contents = printComment(path, options);
955
955
  if (!contents) {
956
956
  return "";
957
957
  }
958
+
959
+ const comment = path.node;
958
960
  const isBlock =
959
961
  options.printer.isBlockComment && options.printer.isBlockComment(comment);
960
962
 
@@ -991,9 +993,9 @@ function printTrailingComment(commentPath, print, options) {
991
993
  }
992
994
 
993
995
  function printAllComments(path, print, options, needsSemi) {
994
- const value = path.getValue();
996
+ const { node } = path;
995
997
  const printed = print(path);
996
- const comments = value && value.comments;
998
+ const comments = node && node.comments;
997
999
 
998
1000
  if (!comments || comments.length === 0) {
999
1001
  return prependCursorPlaceholder(path, options, printed);
@@ -1002,12 +1004,11 @@ function printAllComments(path, print, options, needsSemi) {
1002
1004
  const leadingParts = [];
1003
1005
  const trailingParts = [needsSemi ? ";" : "", printed];
1004
1006
 
1005
- path.each((commentPath) => {
1006
- const comment = commentPath.getValue();
1007
+ path.each(({ node: comment }) => {
1007
1008
  const { leading, trailing } = comment;
1008
1009
 
1009
1010
  if (leading) {
1010
- const contents = printLeadingComment(commentPath, print, options);
1011
+ const contents = printLeadingComment(path, print, options);
1011
1012
  if (!contents) {
1012
1013
  return;
1013
1014
  }
@@ -1018,7 +1019,7 @@ function printAllComments(path, print, options, needsSemi) {
1018
1019
  leadingParts.push(hardline);
1019
1020
  }
1020
1021
  } else if (trailing) {
1021
- trailingParts.push(printTrailingComment(commentPath, print, options));
1022
+ trailingParts.push(printTrailingComment(path, print, options));
1022
1023
  }
1023
1024
  }, "comments");
1024
1025
 
@@ -1029,7 +1030,7 @@ function printAllComments(path, print, options, needsSemi) {
1029
1030
  );
1030
1031
  }
1031
1032
 
1032
- module.exports = {
1033
+ export {
1033
1034
  handleOwnLineComment,
1034
1035
  handleEndOfLineComment,
1035
1036
  handleRemainingComment,
package/src/index.js CHANGED
@@ -1,12 +1,23 @@
1
- "use strict";
2
-
3
- const parse = require("./parser");
4
- const print = require("./printer");
5
- const clean = require("./clean");
6
- const options = require("./options");
7
- const comments = require("./comments");
8
- const { join, hardline } = require("prettier").doc.builders;
9
- const { hasPragma, insertPragma } = require("./pragma");
1
+ import { doc } from "prettier";
2
+ import {
3
+ LINGUIST_LANGUAGES_PHP,
4
+ LINGUIST_LANGUAGES_HTML_PHP,
5
+ } from "./linguist-languages.cjs";
6
+ import parse from "./parser.js";
7
+ import print from "./printer.js";
8
+ import clean from "./clean.js";
9
+ import options from "./options.js";
10
+ import {
11
+ handleOwnLineComment,
12
+ handleEndOfLineComment,
13
+ handleRemainingComment,
14
+ getCommentChildNodes,
15
+ canAttachComment,
16
+ isBlockComment,
17
+ } from "./comments.js";
18
+ import { hasPragma, insertPragma } from "./pragma.js";
19
+
20
+ const { join, hardline } = doc.builders;
10
21
 
11
22
  function createLanguage(linguistData, { extend, override }) {
12
23
  const language = {};
@@ -30,13 +41,13 @@ function createLanguage(linguistData, { extend, override }) {
30
41
  }
31
42
 
32
43
  const languages = [
33
- createLanguage(require("linguist-languages/data/PHP"), {
44
+ createLanguage(LINGUIST_LANGUAGES_PHP, {
34
45
  override: {
35
46
  parsers: ["php"],
36
47
  vscodeLanguageIds: ["php"],
37
48
  },
38
49
  }),
39
- createLanguage(require("linguist-languages/data/HTML+PHP"), {
50
+ createLanguage(LINGUIST_LANGUAGES_HTML_PHP, {
40
51
  override: {
41
52
  parsers: ["php"],
42
53
  vscodeLanguageIds: ["php"],
@@ -81,21 +92,21 @@ const printers = {
81
92
  getVisitorKeys,
82
93
  insertPragma,
83
94
  massageAstNode: clean,
84
- getCommentChildNodes: comments.getCommentChildNodes,
85
- canAttachComment: comments.canAttachComment,
86
- isBlockComment: comments.isBlockComment,
95
+ getCommentChildNodes,
96
+ canAttachComment,
97
+ isBlockComment,
87
98
  handleComments: {
88
- ownLine: comments.handleOwnLineComment,
89
- endOfLine: comments.handleEndOfLineComment,
90
- remaining: comments.handleRemainingComment,
99
+ ownLine: handleOwnLineComment,
100
+ endOfLine: handleEndOfLineComment,
101
+ remaining: handleRemainingComment,
91
102
  },
92
103
  willPrintOwnComments(path) {
93
- const node = path.getValue();
104
+ const { node } = path;
94
105
 
95
106
  return node && node.kind === "noop";
96
107
  },
97
- printComment(commentPath) {
98
- const comment = commentPath.getValue();
108
+ printComment(path) {
109
+ const comment = path.node;
99
110
 
100
111
  switch (comment.kind) {
101
112
  case "commentblock": {
@@ -138,8 +149,7 @@ const printers = {
138
149
  !comment.value.includes("prettier-ignore-start") &&
139
150
  !comment.value.includes("prettier-ignore-end");
140
151
 
141
- const parentNode = path.getParentNode();
142
- const node = path.getNode();
152
+ const { node, parent: parentNode } = path;
143
153
 
144
154
  return (
145
155
  (node &&
@@ -161,12 +171,8 @@ const printers = {
161
171
  },
162
172
  };
163
173
 
164
- module.exports = {
165
- languages,
166
- printers,
167
- parsers,
168
- options,
169
- defaultOptions: {
170
- tabWidth: 4,
171
- },
174
+ const defaultOptions = {
175
+ tabWidth: 4,
172
176
  };
177
+
178
+ export { languages, printers, parsers, options, defaultOptions };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+
3
+ module.exports.LINGUIST_LANGUAGES_PHP = require("linguist-languages/data/PHP");
4
+ module.exports.LINGUIST_LANGUAGES_HTML_PHP = require("linguist-languages/data/HTML+PHP");
@@ -1,18 +1,14 @@
1
- "use strict";
2
-
3
- const assert = require("assert");
4
-
5
- const { getPrecedence, shouldFlatten, isBitwiseOperator } = require("./util");
1
+ import assert from "assert";
2
+ import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
6
3
 
7
4
  function needsParens(path) {
8
- const parent = path.getParentNode();
5
+ const { parent } = path;
9
6
 
10
7
  if (!parent) {
11
8
  return false;
12
9
  }
13
10
 
14
- const name = path.getName();
15
- const node = path.getNode();
11
+ const { key, node } = path;
16
12
 
17
13
  if (
18
14
  [
@@ -56,9 +52,9 @@ function needsParens(path) {
56
52
  case "staticlookup":
57
53
  case "offsetlookup":
58
54
  case "call":
59
- return name === "what" && parent.what === node;
55
+ return key === "what";
60
56
  case "bin":
61
- return parent.type === "**" && name === "left";
57
+ return parent.type === "**" && key === "left";
62
58
  default:
63
59
  return false;
64
60
  }
@@ -80,7 +76,7 @@ function needsParens(path) {
80
76
  case "nullsafepropertylookup":
81
77
  case "staticlookup":
82
78
  case "offsetlookup":
83
- return name === "what" && parent.what === node;
79
+ return key === "what";
84
80
  case "bin": {
85
81
  const po = parent.type;
86
82
  const pp = getPrecedence(po);
@@ -95,7 +91,7 @@ function needsParens(path) {
95
91
  return true;
96
92
  }
97
93
 
98
- if (pp === np && name === "right") {
94
+ if (pp === np && key === "right") {
99
95
  assert.strictEqual(parent.right, node);
100
96
 
101
97
  return true;
@@ -127,11 +123,7 @@ function needsParens(path) {
127
123
  case "staticlookup": {
128
124
  switch (parent.kind) {
129
125
  case "call":
130
- return (
131
- name === "what" &&
132
- parent.what === node &&
133
- node.parenthesizedExpression
134
- );
126
+ return key === "what" && node.parenthesizedExpression;
135
127
 
136
128
  default:
137
129
  return false;
@@ -145,7 +137,7 @@ function needsParens(path) {
145
137
  case "staticlookup":
146
138
  case "offsetlookup":
147
139
  case "call":
148
- return name === "what" && parent.what === node;
140
+ return key === "what";
149
141
  default:
150
142
  return false;
151
143
  }
@@ -157,10 +149,10 @@ function needsParens(path) {
157
149
  case "staticlookup":
158
150
  case "offsetlookup":
159
151
  case "call":
160
- return name === "what" && parent.what === node;
152
+ return key === "what";
161
153
 
162
154
  case "retif":
163
- return parent.test === node;
155
+ return key === "test";
164
156
 
165
157
  default:
166
158
  return !!(node.key || node.value);
@@ -195,7 +187,7 @@ function needsParens(path) {
195
187
  case "unary":
196
188
  case "bin":
197
189
  case "retif":
198
- if (name === "test" && !parent.trueExpr) {
190
+ if (key === "test" && !parent.trueExpr) {
199
191
  return false;
200
192
  }
201
193
 
@@ -205,7 +197,7 @@ function needsParens(path) {
205
197
  case "staticlookup":
206
198
  case "offsetlookup":
207
199
  case "call":
208
- return name === "what" && parent.what === node;
200
+ return key === "what";
209
201
 
210
202
  default:
211
203
  return false;
@@ -213,7 +205,7 @@ function needsParens(path) {
213
205
  case "closure":
214
206
  switch (parent.kind) {
215
207
  case "call":
216
- return name === "what" && parent.what === node;
208
+ return key === "what";
217
209
 
218
210
  // https://github.com/prettier/plugin-php/issues/1675
219
211
  case "propertylookup":
@@ -243,7 +235,7 @@ function needsParens(path) {
243
235
  return false;
244
236
  }
245
237
 
246
- return name === "what" && parent.what === node;
238
+ return key === "what";
247
239
  default:
248
240
  return false;
249
241
  }
@@ -255,4 +247,4 @@ function needsParens(path) {
255
247
  return false;
256
248
  }
257
249
 
258
- module.exports = needsParens;
250
+ export default needsParens;
package/src/options.js CHANGED
@@ -1,8 +1,6 @@
1
- "use strict";
2
-
3
1
  const CATEGORY_PHP = "PHP";
4
2
 
5
- module.exports = {
3
+ export default {
6
4
  phpVersion: {
7
5
  since: "0.13.0",
8
6
  category: CATEGORY_PHP,
package/src/parser.js CHANGED
@@ -1,5 +1,4 @@
1
- "use strict";
2
- const engine = require("php-parser");
1
+ import engine from "php-parser";
3
2
 
4
3
  function parse(text, opts) {
5
4
  const inMarkdown = opts && opts.parentParser === "markdown";
@@ -61,4 +60,4 @@ function parse(text, opts) {
61
60
  return ast;
62
61
  }
63
62
 
64
- module.exports = parse;
63
+ export default parse;
package/src/pragma.js CHANGED
@@ -1,7 +1,5 @@
1
- "use strict";
2
-
3
- const parse = require("./parser");
4
- const memoize = require("mem");
1
+ import memoize from "mem";
2
+ import parse from "./parser.js";
5
3
 
6
4
  const reHasPragma = /@prettier|@format/;
7
5
 
@@ -91,7 +89,4 @@ function insertPragma(text) {
91
89
  ${after}`;
92
90
  }
93
91
 
94
- module.exports = {
95
- hasPragma,
96
- insertPragma,
97
- };
92
+ export { hasPragma, insertPragma };