@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/src/index.js CHANGED
@@ -1,12 +1,24 @@
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
+ import { locStart, locEnd } from "./loc.js";
20
+
21
+ const { join, hardline } = doc.builders;
10
22
 
11
23
  function createLanguage(linguistData, { extend, override }) {
12
24
  const language = {};
@@ -30,13 +42,13 @@ function createLanguage(linguistData, { extend, override }) {
30
42
  }
31
43
 
32
44
  const languages = [
33
- createLanguage(require("linguist-languages/data/PHP"), {
45
+ createLanguage(LINGUIST_LANGUAGES_PHP, {
34
46
  override: {
35
47
  parsers: ["php"],
36
48
  vscodeLanguageIds: ["php"],
37
49
  },
38
50
  }),
39
- createLanguage(require("linguist-languages/data/HTML+PHP"), {
51
+ createLanguage(LINGUIST_LANGUAGES_HTML_PHP, {
40
52
  override: {
41
53
  parsers: ["php"],
42
54
  vscodeLanguageIds: ["php"],
@@ -44,16 +56,12 @@ const languages = [
44
56
  }),
45
57
  ];
46
58
 
47
- const loc = (prop) => (node) => {
48
- return node.loc && node.loc[prop] && node.loc[prop].offset;
49
- };
50
-
51
59
  const parsers = {
52
60
  php: {
53
61
  parse,
54
62
  astFormat: "php",
55
- locStart: loc("start"),
56
- locEnd: loc("end"),
63
+ locStart,
64
+ locEnd,
57
65
  hasPragma,
58
66
  },
59
67
  };
@@ -81,21 +89,21 @@ const printers = {
81
89
  getVisitorKeys,
82
90
  insertPragma,
83
91
  massageAstNode: clean,
84
- getCommentChildNodes: comments.getCommentChildNodes,
85
- canAttachComment: comments.canAttachComment,
86
- isBlockComment: comments.isBlockComment,
92
+ getCommentChildNodes,
93
+ canAttachComment,
94
+ isBlockComment,
87
95
  handleComments: {
88
- ownLine: comments.handleOwnLineComment,
89
- endOfLine: comments.handleEndOfLineComment,
90
- remaining: comments.handleRemainingComment,
96
+ ownLine: handleOwnLineComment,
97
+ endOfLine: handleEndOfLineComment,
98
+ remaining: handleRemainingComment,
91
99
  },
92
100
  willPrintOwnComments(path) {
93
- const node = path.getValue();
101
+ const { node } = path;
94
102
 
95
103
  return node && node.kind === "noop";
96
104
  },
97
- printComment(commentPath) {
98
- const comment = commentPath.getValue();
105
+ printComment(path) {
106
+ const comment = path.node;
99
107
 
100
108
  switch (comment.kind) {
101
109
  case "commentblock": {
@@ -127,7 +135,7 @@ const printers = {
127
135
  case "commentline": {
128
136
  return comment.value.trimRight();
129
137
  }
130
- /* istanbul ignore next */
138
+ /* c8 ignore next 2 */
131
139
  default:
132
140
  throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
133
141
  }
@@ -138,8 +146,7 @@ const printers = {
138
146
  !comment.value.includes("prettier-ignore-start") &&
139
147
  !comment.value.includes("prettier-ignore-end");
140
148
 
141
- const parentNode = path.getParentNode();
142
- const node = path.getNode();
149
+ const { node, parent: parentNode } = path;
143
150
 
144
151
  return (
145
152
  (node &&
@@ -161,12 +168,8 @@ const printers = {
161
168
  },
162
169
  };
163
170
 
164
- module.exports = {
165
- languages,
166
- printers,
167
- parsers,
168
- options,
169
- defaultOptions: {
170
- tabWidth: 4,
171
- },
171
+ const defaultOptions = {
172
+ tabWidth: 4,
172
173
  };
174
+
175
+ 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");
package/src/loc.js ADDED
@@ -0,0 +1,4 @@
1
+ const loc = (prop) => (node) => node.loc?.[prop]?.offset;
2
+
3
+ export const locStart = loc("start");
4
+ export const locEnd = loc("end");
@@ -1,18 +1,13 @@
1
- "use strict";
2
-
3
- const assert = require("assert");
4
-
5
- const { getPrecedence, shouldFlatten, isBitwiseOperator } = require("./util");
1
+ import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
6
2
 
7
3
  function needsParens(path) {
8
- const parent = path.getParentNode();
4
+ const { parent } = path;
9
5
 
10
6
  if (!parent) {
11
7
  return false;
12
8
  }
13
9
 
14
- const name = path.getName();
15
- const node = path.getNode();
10
+ const { key, node } = path;
16
11
 
17
12
  if (
18
13
  [
@@ -56,9 +51,9 @@ function needsParens(path) {
56
51
  case "staticlookup":
57
52
  case "offsetlookup":
58
53
  case "call":
59
- return name === "what" && parent.what === node;
54
+ return key === "what";
60
55
  case "bin":
61
- return parent.type === "**" && name === "left";
56
+ return parent.type === "**" && key === "left";
62
57
  default:
63
58
  return false;
64
59
  }
@@ -80,7 +75,7 @@ function needsParens(path) {
80
75
  case "nullsafepropertylookup":
81
76
  case "staticlookup":
82
77
  case "offsetlookup":
83
- return name === "what" && parent.what === node;
78
+ return key === "what";
84
79
  case "bin": {
85
80
  const po = parent.type;
86
81
  const pp = getPrecedence(po);
@@ -95,9 +90,7 @@ function needsParens(path) {
95
90
  return true;
96
91
  }
97
92
 
98
- if (pp === np && name === "right") {
99
- assert.strictEqual(parent.right, node);
100
-
93
+ if (pp === np && key === "right") {
101
94
  return true;
102
95
  }
103
96
 
@@ -127,11 +120,7 @@ function needsParens(path) {
127
120
  case "staticlookup": {
128
121
  switch (parent.kind) {
129
122
  case "call":
130
- return (
131
- name === "what" &&
132
- parent.what === node &&
133
- node.parenthesizedExpression
134
- );
123
+ return key === "what" && node.parenthesizedExpression;
135
124
 
136
125
  default:
137
126
  return false;
@@ -145,7 +134,7 @@ function needsParens(path) {
145
134
  case "staticlookup":
146
135
  case "offsetlookup":
147
136
  case "call":
148
- return name === "what" && parent.what === node;
137
+ return key === "what";
149
138
  default:
150
139
  return false;
151
140
  }
@@ -157,10 +146,10 @@ function needsParens(path) {
157
146
  case "staticlookup":
158
147
  case "offsetlookup":
159
148
  case "call":
160
- return name === "what" && parent.what === node;
149
+ return key === "what";
161
150
 
162
151
  case "retif":
163
- return parent.test === node;
152
+ return key === "test";
164
153
 
165
154
  default:
166
155
  return !!(node.key || node.value);
@@ -195,7 +184,7 @@ function needsParens(path) {
195
184
  case "unary":
196
185
  case "bin":
197
186
  case "retif":
198
- if (name === "test" && !parent.trueExpr) {
187
+ if (key === "test" && !parent.trueExpr) {
199
188
  return false;
200
189
  }
201
190
 
@@ -205,7 +194,7 @@ function needsParens(path) {
205
194
  case "staticlookup":
206
195
  case "offsetlookup":
207
196
  case "call":
208
- return name === "what" && parent.what === node;
197
+ return key === "what";
209
198
 
210
199
  default:
211
200
  return false;
@@ -213,7 +202,7 @@ function needsParens(path) {
213
202
  case "closure":
214
203
  switch (parent.kind) {
215
204
  case "call":
216
- return name === "what" && parent.what === node;
205
+ return key === "what";
217
206
 
218
207
  // https://github.com/prettier/plugin-php/issues/1675
219
208
  case "propertylookup":
@@ -243,7 +232,7 @@ function needsParens(path) {
243
232
  return false;
244
233
  }
245
234
 
246
- return name === "what" && parent.what === node;
235
+ return key === "what";
247
236
  default:
248
237
  return false;
249
238
  }
@@ -255,4 +244,4 @@ function needsParens(path) {
255
244
  return false;
256
245
  }
257
246
 
258
- module.exports = needsParens;
247
+ 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 };