Skip to content

Commit

Permalink
(PHP 8.1) Added support for class constants with FINAL keyword (glayz…
Browse files Browse the repository at this point in the history
…zle#1110)

* Added support for class constants with FINAL keyword

* updated ts definition

* reformat types with prettier to fix but in CI
  • Loading branch information
cseufert authored Jun 5, 2023
1 parent 4f57b13 commit 2d39b04
Show file tree
Hide file tree
Showing 12 changed files with 1,455 additions and 1,400 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build": "webpack --config webpack.config.js",
"-postbuild": "yarpm run build-docs",
"build-docs": "jsdoc -c .jsdoc.json",
"build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src -d .",
"build-types": "jsdoc -t node_modules/tsd-jsdoc/dist -r src -d . && prettier -w types.d.ts",
"publish-docs": "git subtree push --prefix docs origin gh-pages",
"benchmark": "node test/benchmark.js"
},
Expand Down Expand Up @@ -81,5 +81,6 @@
"webpack": "^5.76.2",
"webpack-cli": "^5.0.1",
"yarpm": "^1.2.0"
}
},
"dependencies": {}
}
2 changes: 2 additions & 0 deletions src/ast/classconstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const IS_PRIVATE = "private";
* @memberOf module:php-parser
* @extends {ConstantStatement}
* @property {string} visibility
* @property {bool} final
* @property {AttrGroup[]} attrGroups
*/
const ClassConstant = ConstantStatement.extends(
Expand Down Expand Up @@ -51,6 +52,7 @@ ClassConstant.prototype.parseFlags = function (flags) {
} else if (flags[0] === 2) {
this.visibility = IS_PRIVATE;
}
this.final = flags[2] === 2;
};

module.exports = ClassConstant;
1 change: 1 addition & 0 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"loc": Location {
"end": Position {
Expand Down
2 changes: 2 additions & 0 deletions test/snapshot/__snapshots__/attributes.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down Expand Up @@ -521,6 +522,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down
4 changes: 4 additions & 0 deletions test/snapshot/__snapshots__/class.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"leadingComments": [
CommentLine {
Expand Down Expand Up @@ -229,6 +230,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"leadingComments": [
CommentLine {
Expand Down Expand Up @@ -1334,6 +1336,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down Expand Up @@ -1443,6 +1446,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down
52 changes: 52 additions & 0 deletions test/snapshot/__snapshots__/classconstant.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`classconstant final 1`] = `
Program {
"children": [
Class {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"constants": [
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "CONSTANT",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Hello world!"",
"unicode": false,
"value": "Hello world!",
},
},
],
"final": true,
"kind": "classconstant",
"visibility": "public",
},
],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
"name": "Foo",
},
},
],
"errors": [],
"kind": "program",
}
`;

exports[`classconstant multiple 1`] = `
Program {
"children": [
Expand Down Expand Up @@ -38,6 +85,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down Expand Up @@ -84,6 +132,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "private",
},
Expand Down Expand Up @@ -130,6 +179,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "protected",
},
Expand Down Expand Up @@ -176,6 +226,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "public",
},
Expand Down Expand Up @@ -222,6 +273,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down
1 change: 1 addition & 0 deletions test/snapshot/__snapshots__/enum.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "public",
},
Expand Down
1 change: 1 addition & 0 deletions test/snapshot/__snapshots__/heredoc.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,7 @@ FOOBAR",
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down
1 change: 1 addition & 0 deletions test/snapshot/__snapshots__/interface.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Program {
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down
1 change: 1 addition & 0 deletions test/snapshot/__snapshots__/nowdoc.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ FOOBAR",
},
},
],
"final": false,
"kind": "classconstant",
"visibility": "",
},
Expand Down
7 changes: 7 additions & 0 deletions test/snapshot/classconstant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ describe("classconstant", () => {
parser.parseEval('class Foo { private const CONSTANT = "Hello world!"; }')
).toMatchSnapshot();
});
it("final", () => {
expect(
parser.parseEval(
'class Foo { final public const CONSTANT = "Hello world!"; }'
)
).toMatchSnapshot();
});
});
Loading

0 comments on commit 2d39b04

Please sign in to comment.