Skip to content

Commit 352f501

Browse files
authored
Enum support (#1916)
* Update php-parser unlocking enum support * enum support * Fix typed enums * restore test snapshot * use the correct utility function for semicolon printing
1 parent 18340bf commit 352f501

7 files changed

Lines changed: 238 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"dependencies": {
1414
"linguist-languages": "^7.5.1",
1515
"mem": "^8.0.0",
16-
"php-parser": "3.1.0-beta.0"
16+
"php-parser": "3.1.0-beta.1"
1717
},
1818
"devDependencies": {
1919
"@babel/preset-env": "^7.16.11",

src/printer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,10 @@ function printClass(path, options, print) {
12681268
declaration.push(" ", path.call(print, "name"));
12691269
}
12701270

1271+
if (node.kind === "enum" && node.valueType) {
1272+
declaration.push(": ", path.call(print, "valueType"));
1273+
}
1274+
12711275
// Only `class` can have `extends` and `implements`
12721276
if (node.extends && node.implements) {
12731277
declaration.push(
@@ -1737,6 +1741,7 @@ function printNode(path, options, print) {
17371741
node.alias ? concat([" as ", path.call(print, "alias")]) : "",
17381742
]);
17391743
case "class":
1744+
case "enum":
17401745
case "interface":
17411746
case "trait":
17421747
return printClass(path, options, print);
@@ -3062,6 +3067,27 @@ function printNode(path, options, print) {
30623067
: "";
30633068
case "namedargument":
30643069
return concat([node.name, ": ", path.call(print, "value")]);
3070+
3071+
case "enumcase":
3072+
return group(
3073+
concat([
3074+
"case ",
3075+
path.call(print, "name"),
3076+
node.value
3077+
? concat([
3078+
" =",
3079+
printAssignmentRight(
3080+
node.name,
3081+
node.value,
3082+
path.call(print, "value"),
3083+
false,
3084+
options
3085+
),
3086+
])
3087+
: "",
3088+
])
3089+
);
3090+
30653091
case "error":
30663092
default:
30673093
// istanbul ignore next

src/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ function nodeHasStatement(node) {
165165
"program",
166166
"namespace",
167167
"class",
168+
"enum",
168169
"interface",
169170
"trait",
170171
"traituse",
@@ -420,6 +421,7 @@ function lineShouldEndWithSemicolon(path) {
420421
"traitalias",
421422
"goto",
422423
"constantstatement",
424+
"enumcase",
423425
"global",
424426
"static",
425427
"echo",
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`enum.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
<?php
10+
11+
interface Colorful
12+
{
13+
public function color(): string;
14+
}
15+
16+
trait Rectangle
17+
{
18+
public function shape(): string {
19+
return "Rectangle";
20+
}
21+
}
22+
23+
enum Suit implements Colorful
24+
{
25+
use Rectangle; // https://www.php.net/manual/en/language.enumerations.traits.php
26+
27+
case Hearts;
28+
case Diamonds;
29+
case Clubs;
30+
case Spades;
31+
32+
public const Favorite = self::Clubs;
33+
34+
// Fulfills the interface contract.
35+
public function color(): string
36+
{
37+
return match($this) {
38+
Suit::Hearts,
39+
Suit::Diamonds => 'Red',
40+
Suit::Clubs,
41+
Suit::Spades => 'Black',
42+
};
43+
}
44+
45+
// Not part of an interface; that's fine.
46+
public function shape(): string
47+
{
48+
return "Rectangle";
49+
}
50+
51+
public static function staticMethod(){
52+
return self::Clubs;
53+
}
54+
}
55+
56+
function paint(Colorful $c) { }
57+
58+
paint(Suit::Clubs); // Works
59+
60+
print Suit::Diamonds->shape(); // prints "Rectangle"
61+
62+
63+
class Foo
64+
{
65+
const Bar = Suit::Hearts; // https://www.php.net/manual/en/language.enumerations.expressions.php
66+
}
67+
68+
69+
enum BackedSuit: string
70+
{
71+
case Spades = 5;
72+
}
73+
=====================================output=====================================
74+
<?php
75+
76+
interface Colorful
77+
{
78+
public function color(): string;
79+
}
80+
81+
trait Rectangle
82+
{
83+
public function shape(): string
84+
{
85+
return "Rectangle";
86+
}
87+
}
88+
89+
enum Suit implements Colorful
90+
{
91+
use Rectangle; // https://www.php.net/manual/en/language.enumerations.traits.php
92+
93+
case Hearts;
94+
case Diamonds;
95+
case Clubs;
96+
case Spades;
97+
98+
public const Favorite = self::Clubs;
99+
100+
// Fulfills the interface contract.
101+
public function color(): string
102+
{
103+
return match ($this) {
104+
Suit::Hearts, Suit::Diamonds => "Red",
105+
Suit::Clubs, Suit::Spades => "Black"
106+
};
107+
}
108+
109+
// Not part of an interface; that's fine.
110+
public function shape(): string
111+
{
112+
return "Rectangle";
113+
}
114+
115+
public static function staticMethod()
116+
{
117+
return self::Clubs;
118+
}
119+
}
120+
121+
function paint(Colorful $c)
122+
{
123+
}
124+
125+
paint(Suit::Clubs); // Works
126+
127+
print Suit::Diamonds->shape(); // prints "Rectangle"
128+
129+
class Foo
130+
{
131+
const Bar = Suit::Hearts; // https://www.php.net/manual/en/language.enumerations.expressions.php
132+
}
133+
134+
enum BackedSuit: string
135+
{
136+
case Spades = 5;
137+
}
138+
139+
================================================================================
140+
`;

tests/enum/enum.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
interface Colorful
4+
{
5+
public function color(): string;
6+
}
7+
8+
trait Rectangle
9+
{
10+
public function shape(): string {
11+
return "Rectangle";
12+
}
13+
}
14+
15+
enum Suit implements Colorful
16+
{
17+
use Rectangle; // https://www.php.net/manual/en/language.enumerations.traits.php
18+
19+
case Hearts;
20+
case Diamonds;
21+
case Clubs;
22+
case Spades;
23+
24+
public const Favorite = self::Clubs;
25+
26+
// Fulfills the interface contract.
27+
public function color(): string
28+
{
29+
return match($this) {
30+
Suit::Hearts,
31+
Suit::Diamonds => 'Red',
32+
Suit::Clubs,
33+
Suit::Spades => 'Black',
34+
};
35+
}
36+
37+
// Not part of an interface; that's fine.
38+
public function shape(): string
39+
{
40+
return "Rectangle";
41+
}
42+
43+
public static function staticMethod(){
44+
return self::Clubs;
45+
}
46+
}
47+
48+
function paint(Colorful $c) { }
49+
50+
paint(Suit::Clubs); // Works
51+
52+
print Suit::Diamonds->shape(); // prints "Rectangle"
53+
54+
55+
class Foo
56+
{
57+
const Bar = Suit::Hearts; // https://www.php.net/manual/en/language.enumerations.expressions.php
58+
}
59+
60+
61+
enum BackedSuit: string
62+
{
63+
case Spades = 5;
64+
}

tests/enum/jsfmt.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname, ["php"]);

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4313,10 +4313,10 @@ path-type@^4.0.0:
43134313
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
43144314
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
43154315

4316-
php-parser@3.1.0-beta.0:
4317-
version "3.1.0-beta.0"
4318-
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.1.0-beta.0.tgz#e43cb58ca35022f00973daf9dc55170de1f64ebf"
4319-
integrity sha512-j4v15Zy6pvJKd1N3XR53AUTySr1YEiKer8OlIFvqZTdRwMMmWttPiOZ7oyP53/QHY4DDHUfAtK9ZMomKw0xFlw==
4316+
php-parser@3.1.0-beta.1:
4317+
version "3.1.0-beta.1"
4318+
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.1.0-beta.1.tgz#bd8cd6d872b0f85f7a59a6b5f019ea8f37c7cbcc"
4319+
integrity sha512-4dvvMO+cDbF+CqgZ+7h87wexInYVoocIAaSO9sc5DRzTVfz914KTk9H9y6QupjHQ3pygXfCbP2LE5zvpexkGXg==
43204320

43214321
picocolors@^1.0.0:
43224322
version "1.0.0"

0 commit comments

Comments
 (0)