Skip to content

Commit 49bf194

Browse files
czoselfisker
andauthored
feat: upgrade to prettier 3 (#2200)
* feat: upgrade to prettier 3 (wip) * fix: array tests (and potentially more?) * fix: use getVisitorKeys instead of getCommentChildNodes * fix: tests * Move `getVisitorKeys` to correct location * Workaround * Use `jest-light-runner` * Run test on Windows --------- Co-authored-by: fisker <lionkay@gmail.com>
1 parent a5a174b commit 49bf194

17 files changed

Lines changed: 1233 additions & 627 deletions

File tree

.eslintrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extends:
44
- plugin:prettier/recommended
55
- plugin:jest/recommended
66
- plugin:prettier-doc/recommended
7+
parserOptions:
8+
ecmaVersion: 2023
79
plugins:
810
- import
911
root: true

.github/dependabot.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2
22
updates:
3-
- package-ecosystem: npm
4-
directory: "/"
5-
schedule:
6-
interval: daily
7-
open-pull-requests-limit: 10
3+
- package-ecosystem: npm
4+
directory: "/"
5+
schedule:
6+
interval: daily
7+
open-pull-requests-limit: 10

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88

99
strategy:
1010
matrix:
11-
os: [ubuntu-latest, macos-latest]
12-
node-version: [14.x, 16.x, 18.x]
11+
os: [ubuntu-latest, macos-latest, windows-latest]
12+
node-version: [14.x, 16.x, 18.x, 20.x]
1313

1414
steps:
1515
- uses: actions/checkout@v1

.prettierrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trailingComma: "es5"

CONTRIBUTING.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ yarn test tests/{folder of your choice}
1515

1616
Here's what you need to know about the tests:
1717
18-
* The tests use [Jest](https://facebook.github.io/jest/) snapshots.
19-
* You can make changes and run `jest -u` (or `yarn test -u`) to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR.
20-
* You can run `AST_COMPARE=1 yarn test` for a more robust test run. That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent.
18+
- The tests use [Jest](https://facebook.github.io/jest/) snapshots.
19+
- You can make changes and run `jest -u` (or `yarn test -u`) to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR.
20+
- You can run `AST_COMPARE=1 yarn test` for a more robust test run. That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent.
2121
2222
Run `yarn lint --fix` to automatically format files.
2323
@@ -31,16 +31,14 @@ Add the following configuration to `.vscode/launch.json`:
3131

3232
```json
3333
{
34-
"type": "node",
35-
"request": "launch",
36-
"name": "Prettify test.php",
37-
"skipFiles": [
38-
"<node_internals>/**"
39-
],
40-
"program": "${workspaceRoot}/node_modules/.bin/prettier",
41-
"args": ["--plugin=.", "--parser=php", "test.php"],
42-
"cwd": "${workspaceRoot}",
43-
"outputCapture": "std"
34+
"type": "node",
35+
"request": "launch",
36+
"name": "Prettify test.php",
37+
"skipFiles": ["<node_internals>/**"],
38+
"program": "${workspaceRoot}/node_modules/.bin/prettier",
39+
"args": ["--plugin=.", "--parser=php", "test.php"],
40+
"cwd": "${workspaceRoot}",
41+
"outputCapture": "std"
4442
}
4543
```
4644

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Then use Prettier with PHP, just like this:
141141
```js
142142
prettier.format(YOUR_CODE, {
143143
plugins: prettierPlugins,
144-
parser: "php"
144+
parser: "php",
145145
});
146146
```
147147

@@ -157,25 +157,25 @@ import phpPlugin from "@prettier/plugin-php/standalone";
157157

158158
prettier.format(YOUR_CODE, {
159159
plugins: [phpPlugin],
160-
parser: "php"
160+
parser: "php",
161161
});
162162
```
163163

164164
## Configuration
165165

166166
Prettier for PHP supports the following options. We recommend that all users set the `phpVersion` option.
167167

168-
| Name | Default | Description |
169-
| ------------------ |------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
168+
| Name | Default | Description |
169+
| ------------------ | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
170170
| `phpVersion` | `"7.0"` | Allows specifying the PHP version you're using. If you're using PHP 7.1 or later, setting this option will make use of modern language features in the printed output. If you're using PHP lower than 7.0, you'll have to set this option or Prettier will generate incompatible code. |
171-
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
172-
| `tabWidth` | `4` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)), The default is `4` based on the `PSR-2` coding standard. |
173-
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
174-
| `singleQuote` | `false` | If set to `"true"`, strings that use double quotes but do not rely on the features they add, will be reformatted. Example: `"foo" -> 'foo'`, `"foo $bar" -> "foo $bar"`. |
175-
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
176-
| `braceStyle` | `"per-cs"` | If set to `"per-cs"`, prettier will move open brace for code blocks (classes, functions and methods) onto new line. <br> If set to `"1tbs"`, prettier will move open brace for code blocks (classes, functions and methods) onto same line. |
177-
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
178-
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |
171+
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
172+
| `tabWidth` | `4` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)), The default is `4` based on the `PSR-2` coding standard. |
173+
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
174+
| `singleQuote` | `false` | If set to `"true"`, strings that use double quotes but do not rely on the features they add, will be reformatted. Example: `"foo" -> 'foo'`, `"foo $bar" -> "foo $bar"`. |
175+
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
176+
| `braceStyle` | `"per-cs"` | If set to `"per-cs"`, prettier will move open brace for code blocks (classes, functions and methods) onto new line. <br> If set to `"1tbs"`, prettier will move open brace for code blocks (classes, functions and methods) onto same line. |
177+
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
178+
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |
179179

180180
## Ignoring code
181181

@@ -232,7 +232,7 @@ The official [prettier plugin for vscode](https://github.com/prettier/prettier-v
232232
- Tick the On Save button, if you want your files formatting updated on file save
233233
- Clock OK to save settings
234234

235-
_Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
235+
_Note: Just pressing save does not reformat your current file unless the file has been modified in some way,
236236
alternatively you can use the Prettier shortcut Ctrl+Alt+Shift+P_
237237

238238
### Sublime Text
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
# Prettier integration to php-cs-fixer
22

3-
This recipe uses `prettier/plugin-php` as a Fixer for `php-cs-fixer`.
3+
This recipe uses `prettier/plugin-php` as a Fixer for `php-cs-fixer`.
44

5-
`prettier` will be executed at the very beginning before the other fixers are
6-
applied, such that the `php-cs-fixer` user's configurations is respected.
5+
`prettier` will be executed at the very beginning before the other fixers are
6+
applied, such that the `php-cs-fixer` user's configurations is respected.
77

88
## Useful Configurations
99

1010
### Priority
1111

12-
If you would like `prettier` to execute last, which means you prefer to use
13-
`php-cs-fixer` to complement the current missing features of `prettier`, you
14-
can decrease the priority value of this fixer by decreasing the value returned
15-
by `getPriority` function to something like `-999`
12+
If you would like `prettier` to execute last, which means you prefer to use
13+
`php-cs-fixer` to complement the current missing features of `prettier`, you
14+
can decrease the priority value of this fixer by decreasing the value returned
15+
by `getPriority` function to something like `-999`
1616

1717
### Configure Prettier's setting
1818

19-
If you would like to add configuration settings for `prettier` to this Fixer,
20-
you can modify the `exec` line in `applyFix` function.
19+
If you would like to add configuration settings for `prettier` to this Fixer,
20+
you can modify the `exec` line in `applyFix` function.
2121

22-
For example,
23-
```diff
24-
- exec("yarn exec -- prettier $file");
25-
+ exec("yarn exec -- prettier --brace-style=1tbs $file");
26-
```
27-
will allow you to change the `braceStyle` for this fixer
22+
For example,
23+
24+
```diff
25+
- exec("yarn exec -- prettier $file");
26+
+ exec("yarn exec -- prettier --brace-style=1tbs $file");
27+
```
28+
29+
will allow you to change the `braceStyle` for this fixer
2830

2931
## Possible Improvement
30-
- the configuration can be modified from `php-cs-fixer` configuration
31-
- autoloading
32+
33+
- the configuration can be modified from `php-cs-fixer` configuration
34+
- autoloading

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = {
1919
globals: {
2020
STANDALONE: false,
2121
},
22+
runner: "jest-light-runner",
23+
transform: {},
2224
},
2325
...(/^true$/i.test(process.env.RUN_STANDALONE_TESTS)
2426
? [
@@ -31,6 +33,8 @@ module.exports = {
3133
globals: {
3234
STANDALONE: true,
3335
},
36+
runner: "jest-light-runner",
37+
transform: {},
3438
},
3539
]
3640
: []),

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616
"php-parser": "^3.1.5"
1717
},
1818
"devDependencies": {
19-
"@babel/preset-env": "^7.19.4",
19+
"@babel/preset-env": "^7.22.10",
2020
"codecov": "3.8.3",
2121
"cross-env": "^7.0.2",
22-
"eslint": "8.42.0",
23-
"eslint-config-prettier": "8.8.0",
24-
"eslint-plugin-import": "2.27.5",
25-
"eslint-plugin-jest": "27.2.1",
26-
"eslint-plugin-prettier": "4.2.1",
22+
"eslint": "8.47.0",
23+
"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",
2727
"eslint-plugin-prettier-doc": "^1.1.0",
28-
"jest": "29.5.0",
29-
"jest-environment-jsdom": "29.5.0",
28+
"jest": "29.6.2",
29+
"jest-environment-jsdom": "29.6.2",
30+
"jest-light-runner": "^0.5.0",
3031
"jest-runner-eslint": "2.1.0",
3132
"jest-snapshot-serializer-raw": "^1.1.0",
32-
"prettier": "^2.8.8",
33+
"prettier": "^3.0.1",
3334
"rollup": "^2.75.7",
3435
"rollup-plugin-alias": "^2.0.0",
3536
"rollup-plugin-babel": "^4.3.2",
@@ -43,12 +44,12 @@
4344
"yarpm": "^1.1.1"
4445
},
4546
"peerDependencies": {
46-
"prettier": "^1.15.0 || ^2.0.0"
47+
"prettier": "^1.15.0 || ^2.0.0 || ^3.0.0"
4748
},
4849
"scripts": {
4950
"test": "jest",
5051
"prepublishOnly": "yarpm run build-standalone && cross-env RUN_STANDALONE_TESTS=true yarpm test",
51-
"prettier": "prettier --plugin=. --parser=php",
52+
"prettier": "prettier --plugin=src/index.js --parser=php",
5253
"build-standalone": "rollup -c build/rollup.config.js",
5354
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
5455
}

src/comments.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ function handleClassComments(enclosingNode, followingNode, comment) {
445445
enclosingNode &&
446446
["class", "interface", "trait"].includes(enclosingNode.kind)
447447
) {
448+
if (enclosingNode.__parent_new_arguments?.includes(followingNode)) {
449+
return false;
450+
}
451+
448452
// for extends nodes that have leading comments, we can store them as
449453
// dangling comments so we can handle them in the printer
450454

@@ -897,27 +901,11 @@ function isBlockComment(comment) {
897901
}
898902

899903
function getCommentChildNodes(node) {
900-
if (typeof node !== "object") {
901-
return [];
904+
if (node.kind === "new" && node.what.kind === "class") {
905+
// Pretend to be child of `class`
906+
node.what.__parent_new_arguments = [...node.arguments];
907+
return [node.what];
902908
}
903-
904-
const getChildNodes = (node) =>
905-
Object.keys(node)
906-
.filter(
907-
(n) =>
908-
n !== "kind" &&
909-
n !== "loc" &&
910-
n !== "errors" &&
911-
n !== "extra" &&
912-
n !== "comments" &&
913-
n !== "leadingComments" &&
914-
n !== "enclosingNode" &&
915-
n !== "precedingNode" &&
916-
n !== "followingNode"
917-
)
918-
.map((n) => node[n]);
919-
920-
return getChildNodes(node);
921909
}
922910

923911
function canAttachComment(node) {

0 commit comments

Comments
 (0)