@prettier/plugin-php 0.20.1 → 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
@@ -234,7 +244,7 @@ You may also need to declare php as the parser in your prettier config file:
234
244
  ```json
235
245
  {
236
246
  "parser": "php",
237
- "plugins": ["@prettier/plugin-php"],
247
+ "plugins": ["@prettier/plugin-php"]
238
248
  }
239
249
  ```
240
250
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prettier/plugin-php",
3
- "version": "0.20.1",
3
+ "version": "0.21.0",
4
4
  "description": "Prettier PHP Plugin",
5
5
  "repository": "prettier/prettier-php",
6
6
  "author": "Lucas Azzola <@azz>",
@@ -26,42 +26,57 @@
26
26
  "php-parser": "^3.1.5"
27
27
  },
28
28
  "devDependencies": {
29
- "@babel/preset-env": "^7.22.10",
29
+ "@babel/preset-env": "^7.22.14",
30
+ "@rollup/plugin-alias": "^5.0.0",
31
+ "@rollup/plugin-babel": "^6.0.3",
30
32
  "@rollup/plugin-commonjs": "^25.0.4",
31
- "@rollup/plugin-node-resolve": "^15.1.0",
32
- "codecov": "3.8.3",
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",
33
39
  "cross-env": "^7.0.2",
34
- "eslint": "8.47.0",
40
+ "eslint": "8.51.0",
35
41
  "eslint-config-prettier": "9.0.0",
36
- "eslint-plugin-import": "2.28.0",
37
- "eslint-plugin-jest": "27.2.3",
38
- "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",
39
45
  "eslint-plugin-prettier-doc": "^1.1.0",
40
- "jest": "29.6.2",
41
- "jest-environment-jsdom": "29.6.2",
42
- "jest-light-runner": "^0.5.0",
43
- "jest-runner-eslint": "2.1.0",
44
- "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",
45
50
  "prettier": "^3.0.1",
46
- "rollup": "^2.75.7",
47
- "rollup-plugin-alias": "^2.0.0",
48
- "rollup-plugin-babel": "^4.3.2",
49
- "rollup-plugin-inject": "^3.0.1",
50
- "rollup-plugin-json": "^4.0.0",
51
- "rollup-plugin-replace": "^2.1.0",
52
- "rollup-plugin-terser": "^7.0.2",
51
+ "rollup": "^3.28.1",
53
52
  "strip-ansi": "^7.1.0"
54
53
  },
55
54
  "peerDependencies": {
56
55
  "prettier": "^3.0.0"
57
56
  },
58
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",
59
64
  "test": "yarn test:node && yarn test:standalone",
60
65
  "test:node": "jest",
61
- "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",
62
67
  "prepublishOnly": "yarn test",
63
68
  "prettier": "prettier --plugin=src/index.js --parser=php",
64
- "build-standalone": "rollup -c build/rollup.config.js",
69
+ "build": "rollup --config build/rollup.config.js",
65
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
+ ]
66
81
  }
67
82
  }
package/src/comments.js CHANGED
@@ -1,10 +1,6 @@
1
1
  import { util as prettierUtil, doc } from "prettier";
2
- import {
3
- getNextNonSpaceNonCommentCharacterIndex,
4
- isNextLineEmpty,
5
- isPreviousLineEmpty,
6
- isLookupNode,
7
- } from "./util.js";
2
+ import { isLookupNode } from "./util.js";
3
+ import { locStart, locEnd } from "./loc.js";
8
4
 
9
5
  const {
10
6
  addLeadingComment,
@@ -13,6 +9,9 @@ const {
13
9
  skipNewline,
14
10
  hasNewline,
15
11
  hasNewlineInRange,
12
+ getNextNonSpaceNonCommentCharacterIndex,
13
+ isNextLineEmpty,
14
+ isPreviousLineEmpty,
16
15
  } = prettierUtil;
17
16
  const { join, indent, hardline, cursor, lineSuffix, breakParent } =
18
17
  doc.builders;
@@ -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
 
@@ -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);
@@ -941,9 +942,7 @@ function printLeadingComment(path, print, options) {
941
942
  if (isBlock) {
942
943
  return [
943
944
  contents,
944
- hasNewline(options.originalText, options.locEnd(comment))
945
- ? hardline
946
- : " ",
945
+ hasNewline(options.originalText, locEnd(comment)) ? hardline : " ",
947
946
  ];
948
947
  }
949
948
 
@@ -961,7 +960,7 @@ function printTrailingComment(path, print, options) {
961
960
  options.printer.isBlockComment && options.printer.isBlockComment(comment);
962
961
 
963
962
  if (
964
- hasNewline(options.originalText, options.locStart(comment), {
963
+ hasNewline(options.originalText, locStart(comment), {
965
964
  backwards: true,
966
965
  })
967
966
  ) {
@@ -979,8 +978,7 @@ function printTrailingComment(path, print, options) {
979
978
 
980
979
  const isLineBeforeEmpty = isPreviousLineEmpty(
981
980
  options.originalText,
982
- comment,
983
- options
981
+ locStart(comment)
984
982
  );
985
983
 
986
984
  return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]);
@@ -1015,7 +1013,7 @@ function printAllComments(path, print, options, needsSemi) {
1015
1013
  leadingParts.push(contents);
1016
1014
 
1017
1015
  const text = options.originalText;
1018
- if (hasNewline(text, skipNewline(text, options.locEnd(comment)))) {
1016
+ if (hasNewline(text, skipNewline(text, locEnd(comment)))) {
1019
1017
  leadingParts.push(hardline);
1020
1018
  }
1021
1019
  } else if (trailing) {
package/src/index.js CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  isBlockComment,
17
17
  } from "./comments.js";
18
18
  import { hasPragma, insertPragma } from "./pragma.js";
19
+ import { locStart, locEnd } from "./loc.js";
19
20
 
20
21
  const { join, hardline } = doc.builders;
21
22
 
@@ -55,16 +56,12 @@ const languages = [
55
56
  }),
56
57
  ];
57
58
 
58
- const loc = (prop) => (node) => {
59
- return node.loc && node.loc[prop] && node.loc[prop].offset;
60
- };
61
-
62
59
  const parsers = {
63
60
  php: {
64
61
  parse,
65
62
  astFormat: "php",
66
- locStart: loc("start"),
67
- locEnd: loc("end"),
63
+ locStart,
64
+ locEnd,
68
65
  hasPragma,
69
66
  },
70
67
  };
@@ -138,7 +135,7 @@ const printers = {
138
135
  case "commentline": {
139
136
  return comment.value.trimRight();
140
137
  }
141
- /* istanbul ignore next */
138
+ /* c8 ignore next 2 */
142
139
  default:
143
140
  throw new Error(`Not a comment: ${JSON.stringify(comment)}`);
144
141
  }
@@ -1,4 +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
+ "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,4 +1,3 @@
1
- import assert from "assert";
2
1
  import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
3
2
 
4
3
  function needsParens(path) {
@@ -92,8 +91,6 @@ function needsParens(path) {
92
91
  }
93
92
 
94
93
  if (pp === np && key === "right") {
95
- assert.strictEqual(parent.right, node);
96
-
97
94
  return true;
98
95
  }
99
96