Skip to content

Commit 2d39b04

Browse files
authored
(PHP 8.1) Added support for class constants with FINAL keyword (#1110)
* Added support for class constants with FINAL keyword * updated ts definition * reformat types with prettier to fix but in CI
1 parent 4f57b13 commit 2d39b04

12 files changed

+1455
-1400
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build": "webpack --config webpack.config.js",
2121
"-postbuild": "yarpm run build-docs",
2222
"build-docs": "jsdoc -c .jsdoc.json",
23-
"build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src -d .",
23+
"build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src -d . && prettier -w types.d.ts",
2424
"publish-docs": "git subtree push --prefix docs origin gh-pages",
2525
"benchmark": "node test/benchmark.js"
2626
},
@@ -81,5 +81,6 @@
8181
"webpack": "^5.76.2",
8282
"webpack-cli": "^5.0.1",
8383
"yarpm": "^1.2.0"
84-
}
84+
},
85+
"dependencies": {}
8586
}

src/ast/classconstant.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const IS_PRIVATE = "private";
1919
* @memberOf module:php-parser
2020
* @extends {ConstantStatement}
2121
* @property {string} visibility
22+
* @property {bool} final
2223
* @property {AttrGroup[]} attrGroups
2324
*/
2425
const ClassConstant = ConstantStatement.extends(
@@ -51,6 +52,7 @@ ClassConstant.prototype.parseFlags = function (flags) {
5152
} else if (flags[0] === 2) {
5253
this.visibility = IS_PRIVATE;
5354
}
55+
this.final = flags[2] === 2;
5456
};
5557

5658
module.exports = ClassConstant;

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ Program {
811811
},
812812
},
813813
],
814+
"final": false,
814815
"kind": "classconstant",
815816
"loc": Location {
816817
"end": Position {

test/snapshot/__snapshots__/attributes.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ Program {
333333
},
334334
},
335335
],
336+
"final": false,
336337
"kind": "classconstant",
337338
"visibility": "",
338339
},
@@ -521,6 +522,7 @@ Program {
521522
},
522523
},
523524
],
525+
"final": false,
524526
"kind": "classconstant",
525527
"visibility": "",
526528
},

test/snapshot/__snapshots__/class.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Program {
162162
},
163163
},
164164
],
165+
"final": false,
165166
"kind": "classconstant",
166167
"leadingComments": [
167168
CommentLine {
@@ -229,6 +230,7 @@ Program {
229230
},
230231
},
231232
],
233+
"final": false,
232234
"kind": "classconstant",
233235
"leadingComments": [
234236
CommentLine {
@@ -1334,6 +1336,7 @@ Program {
13341336
},
13351337
},
13361338
],
1339+
"final": false,
13371340
"kind": "classconstant",
13381341
"visibility": "",
13391342
},
@@ -1443,6 +1446,7 @@ Program {
14431446
},
14441447
},
14451448
],
1449+
"final": false,
14461450
"kind": "classconstant",
14471451
"visibility": "",
14481452
},

test/snapshot/__snapshots__/classconstant.test.js.snap

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`classconstant final 1`] = `
4+
Program {
5+
"children": [
6+
Class {
7+
"attrGroups": [],
8+
"body": [
9+
ClassConstant {
10+
"attrGroups": [],
11+
"constants": [
12+
Constant {
13+
"kind": "constant",
14+
"name": Identifier {
15+
"kind": "identifier",
16+
"name": "CONSTANT",
17+
},
18+
"value": String {
19+
"isDoubleQuote": true,
20+
"kind": "string",
21+
"raw": ""Hello world!"",
22+
"unicode": false,
23+
"value": "Hello world!",
24+
},
25+
},
26+
],
27+
"final": true,
28+
"kind": "classconstant",
29+
"visibility": "public",
30+
},
31+
],
32+
"extends": null,
33+
"implements": null,
34+
"isAbstract": false,
35+
"isAnonymous": false,
36+
"isFinal": false,
37+
"isReadonly": false,
38+
"kind": "class",
39+
"name": Identifier {
40+
"kind": "identifier",
41+
"name": "Foo",
42+
},
43+
},
44+
],
45+
"errors": [],
46+
"kind": "program",
47+
}
48+
`;
49+
350
exports[`classconstant multiple 1`] = `
451
Program {
552
"children": [
@@ -38,6 +85,7 @@ Program {
3885
},
3986
},
4087
],
88+
"final": false,
4189
"kind": "classconstant",
4290
"visibility": "",
4391
},
@@ -84,6 +132,7 @@ Program {
84132
},
85133
},
86134
],
135+
"final": false,
87136
"kind": "classconstant",
88137
"visibility": "private",
89138
},
@@ -130,6 +179,7 @@ Program {
130179
},
131180
},
132181
],
182+
"final": false,
133183
"kind": "classconstant",
134184
"visibility": "protected",
135185
},
@@ -176,6 +226,7 @@ Program {
176226
},
177227
},
178228
],
229+
"final": false,
179230
"kind": "classconstant",
180231
"visibility": "public",
181232
},
@@ -222,6 +273,7 @@ Program {
222273
},
223274
},
224275
],
276+
"final": false,
225277
"kind": "classconstant",
226278
"visibility": "",
227279
},

test/snapshot/__snapshots__/enum.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Program {
3636
},
3737
},
3838
],
39+
"final": false,
3940
"kind": "classconstant",
4041
"visibility": "public",
4142
},

test/snapshot/__snapshots__/heredoc.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,7 @@ FOOBAR",
17091709
},
17101710
},
17111711
],
1712+
"final": false,
17121713
"kind": "classconstant",
17131714
"visibility": "",
17141715
},

test/snapshot/__snapshots__/interface.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Program {
6565
},
6666
},
6767
],
68+
"final": false,
6869
"kind": "classconstant",
6970
"visibility": "",
7071
},

test/snapshot/__snapshots__/nowdoc.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ FOOBAR",
216216
},
217217
},
218218
],
219+
"final": false,
219220
"kind": "classconstant",
220221
"visibility": "",
221222
},

test/snapshot/classconstant.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ describe("classconstant", () => {
3030
parser.parseEval('class Foo { private const CONSTANT = "Hello world!"; }')
3131
).toMatchSnapshot();
3232
});
33+
it("final", () => {
34+
expect(
35+
parser.parseEval(
36+
'class Foo { final public const CONSTANT = "Hello world!"; }'
37+
)
38+
).toMatchSnapshot();
39+
});
3340
});

0 commit comments

Comments
 (0)