Skip to content

Commit b35fa5a

Browse files
committed
feat: propertyhooks
1 parent c2e38c1 commit b35fa5a

File tree

8 files changed

+226
-5
lines changed

8 files changed

+226
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"dependencies": {
2929
"linguist-languages": "^8.0.0",
30-
"@jorgsowa/php-parser": "^3.2.5-7"
30+
"@jorgsowa/php-parser": "3.2.5-7"
3131
},
3232
"devDependencies": {
3333
"@babel/preset-env": "^7.27.2",

src/printer.mjs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,40 @@ function printAttrs(path, options, print, { inline = false } = {}) {
12051205
return [...allAttrs, inline ? "" : hardline];
12061206
}
12071207

1208+
function printPropertyHook(path, options, print, hook) {
1209+
const parts = [];
1210+
if (hook.byref) {
1211+
parts.push("&");
1212+
}
1213+
if (hook.visibility) {
1214+
parts.push(hook.visibility, " ");
1215+
}
1216+
if (hook.isFinal) {
1217+
parts.push("final ");
1218+
}
1219+
parts.push(hook.name);
1220+
1221+
if (hook.parameter) {
1222+
path.call((parameterPath) => {
1223+
parts.push("(", print(parameterPath), ")");
1224+
}, "parameter");
1225+
}
1226+
1227+
if (hook.body) {
1228+
if (hook.body.kind === "block") {
1229+
console.log(hook.body);
1230+
parts.push(" ", "{", indent([line, path.call(print, "body")]), line, "}");
1231+
} else {
1232+
parts.push(
1233+
" => ",
1234+
path.call((p) => print(p), "body"),
1235+
";"
1236+
);
1237+
}
1238+
}
1239+
return group(parts);
1240+
}
1241+
12081242
function printClass(path, options, print) {
12091243
const { node } = path;
12101244
const isAnonymousClass = node.kind === "class" && node.isAnonymous;
@@ -1782,6 +1816,7 @@ function printNode(path, options, print) {
17821816
case "variadic":
17831817
return ["...", print("what")];
17841818
case "property":
1819+
const isInterface = getAncestorNode(path, "interface");
17851820
return group([
17861821
node.readonly ? "readonly " : "",
17871822
node.type ? [node.nullable ? "?" : "", print("type"), " "] : "",
@@ -1799,6 +1834,38 @@ function printNode(path, options, print) {
17991834
),
18001835
]
18011836
: "",
1837+
node.hooks && node.hooks.length > 0 && options.phpVersion >= 8.4
1838+
? isInterface
1839+
? [
1840+
" { ",
1841+
join(
1842+
" ",
1843+
path.map(
1844+
(p) => [
1845+
printPropertyHook(p, options, print, p.node),
1846+
p.node.body ? "" : ";",
1847+
],
1848+
"hooks"
1849+
)
1850+
),
1851+
" }",
1852+
]
1853+
: [
1854+
" {",
1855+
indent([
1856+
hardline,
1857+
join(
1858+
[hardline, hardline],
1859+
path.map(
1860+
(p) => printPropertyHook(p, options, print, p.node),
1861+
"hooks"
1862+
)
1863+
),
1864+
]),
1865+
hardline,
1866+
"}",
1867+
]
1868+
: "",
18021869
]);
18031870
case "propertystatement": {
18041871
const attrs = [];

src/util.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ function lineShouldEndWithSemicolon(path) {
359359
return true;
360360
}
361361
}
362+
if (
363+
node.kind === "propertystatement" &&
364+
node.properties.some((p) => p.hooks && p.hooks.length > 0)
365+
) {
366+
return false;
367+
}
362368
return [
363369
"expressionstatement",
364370
"do",

tests/class/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,37 @@ class Bar extends Foo {
943943
public const ?int J = null;
944944
}
945945
946+
class B
947+
{
948+
public string $bar {
949+
get {
950+
return $this->bar;
951+
}
952+
}
953+
954+
public string $bar2 {
955+
get => $this->bar2;
956+
}
957+
958+
public string $foo {
959+
set => $this->foo = $value;
960+
}
961+
962+
public string $foo2 {
963+
set(string $value2) {
964+
{
965+
$this->bar = $value2;
966+
$this->foo2 = 'something';
967+
}
968+
}
969+
}
970+
}
971+
972+
interface ABC {
973+
public string $foo { get; }
974+
public string $bar { set; }
975+
}
976+
946977
=====================================output=====================================
947978
<?php
948979
@@ -1423,5 +1454,33 @@ class Bar extends Foo
14231454
public const ?int J = null;
14241455
}
14251456
1457+
class B
1458+
{
1459+
public string $bar {
1460+
get { return $this->bar; }
1461+
}
1462+
1463+
public string $bar2 {
1464+
get => $this->bar2;
1465+
}
1466+
1467+
public string $foo {
1468+
set => ($this->foo = $value);
1469+
}
1470+
1471+
public string $foo2 {
1472+
set(string $value2) {
1473+
$this->bar = $value2;
1474+
$this->foo2 = "something";
1475+
}
1476+
}
1477+
}
1478+
1479+
interface ABC
1480+
{
1481+
public string $foo { get; }
1482+
public string $bar { set; }
1483+
}
1484+
14261485
================================================================================
14271486
`;

tests/class/class.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,34 @@ class Bar extends Foo {
339339
public const int|null I = null;
340340
public const ?int J = null;
341341
}
342+
343+
class B
344+
{
345+
public string $bar {
346+
get {
347+
return $this->bar;
348+
}
349+
}
350+
351+
public string $bar2 {
352+
get => $this->bar2;
353+
}
354+
355+
public string $foo {
356+
set => $this->foo = $value;
357+
}
358+
359+
public string $foo2 {
360+
set(string $value2) {
361+
{
362+
$this->bar = $value2;
363+
$this->foo2 = 'something';
364+
}
365+
}
366+
}
367+
}
368+
369+
interface ABC {
370+
public string $foo { get; }
371+
public string $bar { set; }
372+
}

tests/property/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ string
4747
string' . 'string
4848
string
4949
string';
50+
51+
public $string3 { get => 'string'; }
52+
public $string13 { &get => 'string'; }
53+
public $string4 { get => $this->string4; }
54+
public $string5 { final get => $this->string5; }
55+
public $string6 { set => $this->string6 = $value; }
56+
public $string7 { set => $this->string7 = $value; }
57+
public string $string8 { set(string $value) => $this->string8 = $value; }
58+
public string $string9 { set(string $value) { $this->string9 = $value; }}
59+
public string $string10 { set { $this->string10 = $value; }}
60+
public $string11 { final set => $this->string11 = $value; }
61+
public $string12 { get { return $this->string12; } }
5062
}
5163
5264
=====================================output=====================================
@@ -99,6 +111,40 @@ string' .
99111
'string
100112
string
101113
string';
114+
115+
public $string3 {
116+
get => "string";
117+
}
118+
public $string13 {
119+
&get => "string";
120+
}
121+
public $string4 {
122+
get => $this->string4;
123+
}
124+
public $string5 {
125+
final get => $this->string5;
126+
}
127+
public $string6 {
128+
set => ($this->string6 = $value);
129+
}
130+
public $string7 {
131+
set => ($this->string7 = $value);
132+
}
133+
public string $string8 {
134+
set(string $value) => ($this->string8 = $value);
135+
}
136+
public string $string9 {
137+
set(string $value) { $this->string9 = $value; }
138+
}
139+
public string $string10 {
140+
set { $this->string10 = $value; }
141+
}
142+
public $string11 {
143+
final set => ($this->string11 = $value);
144+
}
145+
public $string12 {
146+
get { return $this->string12; }
147+
}
102148
}
103149
104150
================================================================================

tests/property/property.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ class Foo
3939
string' . 'string
4040
string
4141
string';
42+
43+
public $string3 { get => 'string'; }
44+
public $string13 { &get => 'string'; }
45+
public $string4 { get => $this->string4; }
46+
public $string5 { final get => $this->string5; }
47+
public $string6 { set => $this->string6 = $value; }
48+
public $string7 { set => $this->string7 = $value; }
49+
public string $string8 { set(string $value) => $this->string8 = $value; }
50+
public string $string9 { set(string $value) { $this->string9 = $value; }}
51+
public string $string10 { set { $this->string10 = $value; }}
52+
public $string11 { final set => $this->string11 = $value; }
53+
public $string12 { get { return $this->string12; } }
4254
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,10 +1604,10 @@
16041604
"@types/yargs" "^17.0.33"
16051605
chalk "^4.1.2"
16061606

1607-
"@jorgsowa/php-parser@^3.2.5-7":
1608-
version "3.2.5"
1609-
resolved "https://registry.yarnpkg.com/@jorgsowa/php-parser/-/php-parser-3.2.5.tgz#4ee2c03f48aa22b5bd22b785acb5f483b46e5b88"
1610-
integrity sha512-EOhxHfvDjofdhRcuyoY06GlaNsHFkfWKaRJ5Q5UdtAkhUwfnvQDznFAL7CiIGHYqB1YqFOc+uvnP5ubXEya8Qw==
1607+
"@jorgsowa/php-parser@3.2.5-7":
1608+
version "3.2.5-7"
1609+
resolved "https://registry.yarnpkg.com/@jorgsowa/php-parser/-/php-parser-3.2.5-7.tgz#65b0d3281f295271566c7fc51aa94b9d7811141d"
1610+
integrity sha512-FYIbu0b+hzWzUgc7oMwb8OMuDGsDSPP4Pm/tgGkuRxvQwCLuRRyVhCdjvkWsbXP+GL8gGWPn1D66BfZXX+IU4Q==
16111611

16121612
"@jridgewell/gen-mapping@^0.3.0", "@jridgewell/gen-mapping@^0.3.2":
16131613
version "0.3.3"

0 commit comments

Comments
 (0)